@xylabs/threads
Version:
Web workers & worker threads as simple as a function call
49 lines (46 loc) • 1.97 kB
TypeScript
declare const $errors: unique symbol;
declare const $events: unique symbol;
declare const $terminate: unique symbol;
declare const $transferable: unique symbol;
declare const $worker: unique symbol;
interface TransferDescriptor<T = any> {
[$transferable]: true;
send: T;
transferables: Transferable[];
}
/**
* Mark a transferable object as such, so it will no be serialized and
* deserialized on messaging with the main thread, but to transfer
* ownership of it to the receiving thread.
*
* Only works with array buffers, message ports and few more special
* types of objects, but it's much faster than serializing and
* deserializing them.
*
* Note:
* The transferable object cannot be accessed by this thread again
* unless the receiving thread transfers it back again!
*
* @param transferable Array buffer, message port or similar.
* @see <https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast>
*/
declare function Transfer(transferable: Transferable): TransferDescriptor;
/**
* Mark transferable objects within an arbitrary object or array as
* being a transferable object. They will then not be serialized
* and deserialized on messaging with the main thread, but ownership
* of them will be tranferred to the receiving thread.
*
* Only array buffers, message ports and few more special types of
* objects can be transferred, but it's much faster than serializing and
* deserializing them.
*
* Note:
* The transferable object cannot be accessed by this thread again
* unless the receiving thread transfers it back again!
*
* @param transferable Array buffer, message port or similar.
* @see <https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast>
*/
declare function Transfer<T>(payload: T, transferables: Transferable[]): TransferDescriptor;
export { $errors as $, type TransferDescriptor as T, Transfer as a, $events as b, $terminate as c, $worker as d };