Skip to content

Autoloader: Fixed and improved callbacks#1935

Merged
mAAdhaTTah merged 3 commits intoPrismJS:masterfrom
RunDevelopment:autoloader-callbacks
Jun 30, 2019
Merged

Autoloader: Fixed and improved callbacks#1935
mAAdhaTTah merged 3 commits intoPrismJS:masterfrom
RunDevelopment:autoloader-callbacks

Conversation

@RunDevelopment
Copy link
Copy Markdown
Member

This PR fixes and improves the internal Autoloader callbacks.

Bug

Callbacks were stored in success_callbacks and error_callbacks and 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 (success is this function). Because it takes a little while until the language is loaded (certainly until the next tick), all callbacks will be registered before the onload of 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.highlightElement call.)

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

  1. This declaration and usage of data is completely unnecessary and was removed.
  2. Typings for lang_data.

@mAAdhaTTah mAAdhaTTah merged commit b19f512 into PrismJS:master Jun 30, 2019
@RunDevelopment RunDevelopment deleted the autoloader-callbacks branch June 30, 2019 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants