transfer(_:)

Transfers a JSObject to another thread.

JSSending.swift:152
iOS
13.0+
macOS
10.15+
tvOS
13.0+
watchOS
6.0+
static func transfer(_ object: JSObject) -> JSSending

Parameters

object

The JSObject to be transferred.

Returns

A JSSending instance that can be shared across threads.

The original JSObject is “transferred” to the receiving thread, which means its ownership is completely moved. After transferring, the original object becomes neutered (unusable) in the source thread.

This is more efficient than cloning for large objects like ArrayBuffer because no copying is involved, but the original object can no longer be accessed.

Only objects that implement the JavaScript Transferable interface can be transferred. Common transferable objects include:

  • ArrayBuffer

  • MessagePort

  • ImageBitmap

  • OffscreenCanvas

Example

let buffer = JSObject.global.Uint8Array.function!.new(100).buffer.object!
let transferring = JSSending.transfer(buffer)

// After transfer, the original buffer is neutered
// buffer.byteLength.number! will be 0