Transferables

Transferables are special objects which are allowed to be moved between threads, rather than copied.

Transferables are essentially a performance optimization. While serialization may work fine in most cases, it can cause a noticeable, even multi-second stutter for very sizable objects (like large bitmaps).

Transferables allow the developer to tell the browser that you want to take a large object and send it across threads rather than copy it. After a transferable operation is complete, the original value is considered dead in the thread sending it.

Not every object can be transferred. In order to constrain users from sending unsupported types, we require users create a Transferables instance using a Transferables.Builder.

Unlike default JS APIs, which just send an array of transferables, we require the user to specify the name of each object, as an added layer of safety.

// Sender
worker.postMessage(message, Transferables { add("bitmap", bitmap) })

// Receiver
internal class TransferableWorkerFactory : WorkerFactory<String, String> {
override fun createStrategy(postOutput: OutputDispatcher<String> -> Unit) = WorkerStrategy<String> { input ->
val bitmap = transferables.getImageBitmap("bitmap")
...
}

override fun createIOSerializer() = createPassThroughSerializer()
}

Not every object in the web is transferable, so the add methods and get methods are appropriately typed to constraint what you can register and fetch.

Note that arrays are actually not transferable, but we add support for them here anyway, doing the conversion to their underlying ArrayBuffer ourselves to avoid the users having to do a bunch of boilerplate.

See also

Types

Link copied to clipboard
class Builder
Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun getArrayBuffer(key: String): ArrayBuffer?
Link copied to clipboard
fun getFloat32Array(key: String): Float32Array?
Link copied to clipboard
fun getFloat64Array(key: String): Float64Array?
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun getInt16Array(key: String): Int16Array?
Link copied to clipboard
fun getInt32Array(key: String): Int32Array?
Link copied to clipboard
fun getInt8Array(key: String): Int8Array?
Link copied to clipboard
Link copied to clipboard
fun getUint16Array(key: String): Uint16Array?
Link copied to clipboard
fun getUint32Array(key: String): Uint32Array?
Link copied to clipboard
fun getUint8Array(key: String): Uint8Array?
Link copied to clipboard
fun getUint8ClampedArray(key: String): Uint8ClampedArray?
Link copied to clipboard
fun toJson(): Json
Link copied to clipboard