Social Media Photo by bady abbas on Unsplash
An Atomics based Proxy to simplify, and synchronize, Worker related tasks.
This is the latest iteration of this module where everything is explained in the related merge request and it can be summarized as such:
- there is one default encoder/decoder that brings the best of all worlds out of the box
- there are more utilities that helps reducing roundtrips
- views and buffers are compatible and fast by default
- the whole FFI is now 100% code covered in its dedicated, dependency-free, project
import coincident from 'coincident/main';
const {
// the Worker to be used (this extends the global one and add proxy)
Worker:globalThis.Worker & { proxy: Proxy },
// true if SharedArrayBuffer and sync operations are usable
native:boolean,
// a utility to transfer buffers directly via `postMessage`
// use this at the end of any proxied function signature/call
transfer:(...buffers:ArrayBuffer[]) => buffers,
} = coincident({
// an optional way to transform values before sending these elsewhere
transform: value => any,
// an optional way to encode any value as binary
// reflected-ffi/encoder as default
encoder: reflectedFFIEncoder,
// if `false` disable/ignore the transfer ability (perf boost)
transfer:boolean,
});
import coincident from 'coincident/worker';
const {
// the proxy to invoke sync or async main thread exposed utility
// it can expose utilities itself too that the main can invoke
proxy: Proxy,
// true if SharedArrayBuffer and sync operations are usable
native:boolean,
// a utility to transfer buffers directly via `postMessage`
// use this at the end of any proxied function signature/call
transfer:(...buffers:ArrayBuffer[]) => buffers,
// a way to directly transfer a value as it is
direct: value => value,
} = coincident({
// an optional way to transform values before sending these elsewhere
transform: value => any,
// an optional way to decode any bonary as value
// reflected-ffi/decoder as default
decoder: reflectedFFIDirectDecoder,
// if `false` disable/ignore the transfer ability (perf boost)
transfer:boolean,
// optional minimum SharedArrayBuffer size
minByteLength: 0x7FFF,
// optional maximum SharedArrayBuffer size
maxByteLength: 0x1000000,
});
It returns as part of the object literal also window
, usable only when native
is true
, and isWindowProxy
which returns true
or false
accordingly if the tested reference is from the main thread or not.
It returns as part of the object literal what window/worker
returns but also server
, usable only when native
is true
, and isServerProxy
which returns true
or false
accordingly if the tested reference is from the backend or not.
Following the description of all different imports to use either on the main or the worker thread.
This is the import that provides the ability to expose main thread's callbacks to the worker thread and to also await callbacks exposed via the worker code.
import coincident from 'coincident/main';
const {
// the Worker to be used (this extends the global one)
Worker,
// a boolean indicating if shared array buffer is supported
native,
// a utility to transfer values directly via `postMessage`
// (...args: Transferable[]) => Transferable[]
transfer,
} = coincident({
// an optional utility to transform values (FFI / Proxy related)
transform: value => value,
});