purescript-web/purescript-web-workers

Specify what `Transferable` objects are

JordanMartinez opened this issue · 4 comments

Description

Replace Transferable opaque type with type class based on: https://html.spec.whatwg.org/multipage/structured-data.html#transferable-objects

A related issue is that ArrayBuffer are transferable and a transfer sets the byteLength to 0.
Thus the signature of byteLength must be

byteLength :: ArrayBuffer -> Effect ByteLength

https://pursuit.purescript.org/packages/purescript-arraybuffer/13.1.1/docs/Data.ArrayBuffer.ArrayBuffer

A related issue is that ArrayBuffer are transferable and a transfer sets the byteLength to 0.

Where does the spec say that?

https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Transferable_objects
In the first example:

// Create an 8MB "file" and fill it. 8MB = 1024 * 1024 * 8 B
const uInt8Array = new Uint8Array(1024 * 1024 * 8).map((v, i) => i);
console.log(uInt8Array.byteLength); // 8388608

// Transfer the underlying buffer to a worker
worker.postMessage(uInt8Array, [uInt8Array.buffer]);
console.log(uInt8Array.byteLength); // 0

The example is for uInt8Array but it is also true for ArrayBuffer.

😭

Well that throws a wrench into things...