Skip to main content

Function: transferable()

@arolariu/website


@arolariu/website / workers / transferable

Function: transferable()

transferable<T>(value, transfers): T

Defined in: workers/index.ts:54

Mark a value carrying transferables (e.g. ArrayBuffer, MessagePort, OffscreenCanvas) so the worker host transfers ownership instead of structured-cloning. Wraps Comlink's transfer() under a name that doesn't tie consumers to the underlying RPC library.

Type Parameters

T

T

The value being transferred. Returned unchanged to the caller; Comlink reads the transfer list from a hidden marker on the value during postMessage, so pass the wrapper as a normal argument to your worker method — Comlink unwraps it on the other side.

Parameters

value

T

The payload (often an object that contains the transferable, e.g. {buffer}).

transfers

Transferable[]

The transferable objects to move. After the call settles, the originals are detached on the parent side and unusable.

Returns

T

Example

// Move `buffer` into the worker (zero copy). The parent's `buffer`
// becomes a detached, zero-byte ArrayBuffer once the call is dispatched.
await host.api.process(transferable(buffer, [buffer]));
// was this page useful?