-
Notifications
You must be signed in to change notification settings - Fork 44
fix: guarantee order of load events #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great!
// Functional stand in for the W3 spec "queue a task" paradigm | ||
function task(): Promise<void> { | ||
return new Promise(resolve => setTimeout(resolve, 0)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a lot cleaner!
// We mimic the same event order as <img>, including the spec | ||
// which states events must be dispatched after "queue a task". | ||
// https://www.w3.org/TR/html52/semantics-embedded-content.html#the-img-element | ||
return task() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really clear now.
test/test.js
Outdated
// Emulate some kind of timer clamping | ||
const originalSetTimeout = window.setTimeout | ||
let i = 60 | ||
window.setTimeout = (fn, ms, ...rest) => originalSetTimeout.call(window, fn, ms + (i -= 20), ...rest) | ||
|
||
document.body.appendChild(elem) | ||
return Promise.all(events).then(() => { | ||
window.setTimeout = originalSetTimeout |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move the timer clamping emulation and restoration into before
and after
hooks so that we don't end up with an emulated setTimeout
if the tests fail?
Fixes #21
This refactors
fire
to return a Promise, so that we can determine when an event has been fired (as we're not firing event synchronously).The test is pretty contrived and implementation dependant, but it red-greens consistently and race condition tests are hard to otherwise verify 🤷