Fix race condition during initialization #62
Merged
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.
The current init logic first checks whether
document.readyState === 'complete'
before callinginit()
, otherwise it starts listening for theDOMContentLoaded
event and setsinit()
as the event callback.According to the DOMContentLoaded documentation, it can fire while
document.readyState
is still'interactive'
. So, the current logic has a race condition where if theDOMContentLoaded
event has already fired before we can start listening to it, and thedocument.readyState
isn't'complete'
yet, theninit()
will never be called. This was preventing the polyfill from working for the demo page (and other simple pages on the web like www.example.com) in some cases, such as in the FireFox browser.The fix is to listen to the
readystatechange
event instead, because ifdocument.readyState
isn't'complete'
yet, then we are guaranteed that the finalreadystatechange
event hasn't fired yet, so it's safe to wait for it.Fixes #60