Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 8 commits into from
Mar 12, 2021

Conversation

keithamus
Copy link
Contributor

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 🤷

@keithamus keithamus requested a review from a team as a code owner March 9, 2021 17:59
Copy link
Contributor

@koddsson koddsson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!

Comment on lines +24 to 27
// Functional stand in for the W3 spec "queue a task" paradigm
function task(): Promise<void> {
return new Promise(resolve => setTimeout(resolve, 0))
}
Copy link
Contributor

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!

Comment on lines +150 to +153
// 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()
Copy link
Contributor

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
Comment on lines 449 to 456
// 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
Copy link
Contributor

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?

@keithamus keithamus merged commit f936aeb into main Mar 12, 2021
@keithamus keithamus deleted the fix-guarantee-order-of-load-events branch March 12, 2021 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

loadstart sometimes doesn't fire until the load is complete
3 participants