Providing DOM API to Worker Threads
Warning
This post is more than a year old. Information may be outdated.
Objective
- Provide synchronous DOM API Access to Web Worker Threads
- Part of Brane
Start
The type of e is MessageEvent.
MessageEvent - Web APIs.
Synchronous Call from Main
To enable SharedArrayBuffer, we need a secure context crossOriginIsolated.

To set crossOriginIsolated to true, we need two headers.
- Cross-Origin Opener Policy.
same-origin - Cross-Origin Embedder Policy.
require-corp
Tim already built a super easy toolkit that sets both headers to the desired values. Then we only need to
npx serve-isolated .- braneproject/serve-isolated: Serve static contents under the
crossOriginIsolatedmode.
This is so cool.
Save Point 1
- Sends SAB when clicking the Send SAB to Worker button.
[0]is set to zero, so the heartbeat function waits.
- If Increment button is clicked, SAB
[0]is no longer 0.- We then notify any function waiting at
[0].
- We then notify any function waiting at
heartstarts beating, incrementing SAB[0].
Remote Procedure Call
- See Remote procedure call
- RPC is a request-response protocol. An RPC is initiated by the client, which sends a request message to a known remote server to execute a specified procedure with supplied parameters. The remote server sends a response to the client, and the application continues its process. While the server is processing the call, the client is blocked (it waits until the server has finished processing before resuming execution) unless the client sends an asynchronous request to the server, such as an XMLHttpRequest.
Note that
mainonlynotify().mainneverwait().workerwill yield towait().
Complete
Voila! I have created a synchronous polyfill layer for window.prompt and window.innerHeight functions inside a worker.