Autoloader: Fixed and improved callbacks#1935
Merged
mAAdhaTTah merged 3 commits intoPrismJS:masterfrom Jun 30, 2019
Merged
Conversation
mAAdhaTTah
reviewed
Jun 29, 2019
mAAdhaTTah
approved these changes
Jun 30, 2019
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes and improves the internal Autoloader callbacks.
Bug
Callbacks were stored in
success_callbacksanderror_callbacksand not removed after execution meaning once added, a callback was executed every time the language for which the callback was added for was requested (either directly or as a dependency).This bug wasn't very visible because of how the Autoloader is usually used. Usually, the Autoloader is used to load the languages for the static code in a page. This means that the Autoloader gets asked for a bunch of languages, loads them asynchronously and then executes the callbacks with this line (
successis this function). Because it takes a little while until the language is loaded (certainly until the next tick), all callbacks will be registered before theonloadof the script is called meaning that this never gets executed.Thus the bug doesn't show up. (And even if it does, the success callback is usually just a
Prism.highlightElementcall.)I discovered this bug when I used Autoloader to highlight C like code and then after that was done (some ticks later), I used Autoloader to highlight JavaScript code. What followed was that the synchronously called callbacks caused a stack overflow. Just a bit of good ol' infinite recursion.
I could have also fixed it by deferring callbacks to the next tick but I decided to rework the callback structure because this change of behavior might break some code. Autoloader invokes the callbacks of already loaded languages (and failed ones) synchronously while all other callbacks are called asynchronously. This fix keeps this behavior.
(It's another question as to whether we should call all callbacks asynchronously. Yes, we should IMO.)
Improvements
datais completely unnecessary and was removed.lang_data.