-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Messages are dispatched while microtask queue is not empty #2216
Comments
That's because this is what happens under the hood. queueMicrotask(function () {
console.log('received: something1');
Promise.resolve().then(() => {
console.log('microtask something1');
return Promise.resolve().then(() => {
console.log('microtask-nested something1');
});
});
queueMicrotask(function () {
console.log('received: something2');
Promise.resolve().then(() => {
console.log('microtask something2');
return Promise.resolve().then(() => {
console.log('microtask-nested something2');
});
});
});
}); We can get the expected behavior by resuming the parser when the microtask queue drains, for example by using |
The WebSocket spec defines one event to be dispatched per task and not per microtask so setImmediate sounds appropriate. I am not sure if that degrades the performance though. |
Isn't a task in that context/spec a microtask?
It almost certainly does but there is the |
It is a task but it is on a different queue, a microtask queue, in the html spec. The spec also says that a microtask queue is not a task queue so I am not sure. I think people also refer to tasks/global tasks as macro tasks as opposed to micro tasks. |
https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-task definitely puts a task on a task queue and not a microtask queue though. |
It looks like browsers do not dispatch one event per microtask so I guess I misunderstood the spec in #2160. |
Is there an existing issue for this?
Description
I have previously reported #2159 which was fixed but it looks like the solution might not have been complete.
With the latest version of ws the microtasks scheduled in the event callback are handled first but the microtasks created by those microtasks are not. This is different from the WebSocket client behavior in the browser.
Server code:
Client code:
ws version
8.16.0
Node.js Version
v20.10.0
System
System:
OS: macOS 14.4.1
CPU: (10) arm64 Apple M1 Max
Memory: 12.88 GB / 64.00 GB
Shell: 5.9 - /bin/zsh
Expected result
Actual result
Attachments
No response
The text was updated successfully, but these errors were encountered: