Skip to content
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

Disparity in loadend event dispatch between spec, implementation & tests #175

Closed
lucacasonato opened this issue Aug 12, 2021 · 4 comments
Closed

Comments

@lucacasonato
Copy link
Contributor

Disclaimer: I might be wrong!

According to the spec, the loadend event is synchronously fired right after the load event is dispatched. This all happens inside a single "queue a task". This means that the load and loadend events should be fired in the same turn of the event loop. There is no separate "queue a task" for the dispatching of the loadend event.

The WPT test for this does not agree however. It tests that the loadend event is dispatched one turn of the event loop later (note the await point between eventWatcher.wait_for('load') and eventWatcher.wait_for('loadend')):

  // wpt/FileAPI/reading-data-section/filereader_events.any.js
  var reader = new FileReader();
  var eventWatcher = new EventWatcher(t, reader, ['loadstart', 'progress', 'abort', 'error', 'load', 'loadend']);
  reader.readAsText(new Blob(['a']));
  await eventWatcher.wait_for('loadstart');
  await eventWatcher.wait_for('progress');
  await eventWatcher.wait_for('load');
  await eventWatcher.wait_for('loadend');

The test that would agree with spec behaviour would be the following:

  var reader = new FileReader();
  var eventWatcher = new EventWatcher(t, reader, ['loadstart', 'progress', 'abort', 'error', 'load', 'loadend']);
  reader.readAsText(new Blob(['a']));
  await eventWatcher.wait_for('loadstart');
  await eventWatcher.wait_for('progress');
  await eventWatcher.wait_for(['load', 'loadend']);

All of Chrome, Firefox and Safari implement this according to the current WPT test: https://wpt.fyi/results/FileAPI/reading-data-section/filereader_events.any.html?label=master&label=experimental&product=chrome&product=firefox&product=safari&aligned

So my question: as all browsers agree to disagree here, should the spec be updated? Alternatively I can file issues with all vendors to get this aligned to spec (and correct the test).

@lucacasonato
Copy link
Contributor Author

Actually, nevermind. I guess microtasks are run after every event dispatch...

@lucacasonato
Copy link
Contributor Author

Actually, no they are not:

const target = new EventTarget()
target.addEventListener("f", () => {
    console.log("f");
    queueMicrotask(() => console.log("f mt"));
});
target.addEventListener("x", () => {
    console.log("x");
    queueMicrotask(() => console.log("x mt"));
});
target.dispatchEvent(new Event("f"));
target.dispatchEvent(new Event("x"));

@lucacasonato lucacasonato reopened this Aug 12, 2021
@domenic
Copy link
Contributor

domenic commented Aug 12, 2021

Microtasks run whenever the call stack is empty of JS code. See the latter parts of https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/

@lucacasonato
Copy link
Contributor Author

Deeper digging has revealed Deno bugs. Thanks for the input @domenic :-)

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

No branches or pull requests

2 participants