I'm wondering what happens when a user closes the tab which spawned the worker thread, while the thread is still working. Does it halt everything? If so, is there a way to run the thread in the background even when the tab is closed?
Yes it halts everything, a (dedicated) worker can't outlive its owner. If you use a shared worker, which can have multiple owners, the worker will only stay alive as long as at least one owner is alive. This is the case even if you pass on the entangled MessagePort
to another window (i.e. owner of the message port is not the owner of the worker).
So, with shared workers you can "transfer" the ownership by opening a new window that establishes its own connection with the worker (with new SharedWorker(...)
) and then close the old window. But one window must always remain open.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With