Open Alexendoo opened 5 years ago
I'm interested in this as well, although I think it would be better as a separate API rather than sharing isInputPending
. Probably something like .isMessagePending
.
This would even be potentially useful on the main thread as tabs can communicate via window.postMessage(...)
, however it's likely to be much lower priority than .isInputPending
on the main thread.
Ideally all of these would work as it would be useful to be able to yield to any messages from any sources:
window.isMessagePending;
// In a worker
self.isMessagePending;
// On a Worker or MessagePort
messagePort.isMessagePending;
worker.isMessagePending;
// EventSource/WebSocket
eventSource.isMessagePending;
webSocket.isMessagePending;
This would probably need to be a bit more granular actually, for example one might want to only yield for messages that are directly sent to you (e.g. worker.isMessagePending
), but you might also want to yield for say other workers to receive their messages (e.g. maybe self.isAnyMessagePending
).
In workers it's often desired to write long running synchronous code, but then the issue pops up where you want to cancel or otherwise interrupt if a new message comes in. There's a couple workarounds I've used but it typically ends up similar to the situation in the README; it slows things down.
It was briefly possible to use a
SharedArrayBuffer
to inform the Worker a new message is pending without yielding to the event loop, but unfortunately that ran into the whole spectre fiasco. Even still it was not the most pleasant workaround when it did work.It would be great if
isInputPending
could be added to theWorkerNavigator
, with support for detecting a pendingmessage
event.Another case to cover would be to detect a
message
event in the main window coming from a Worker.