Is there a blocking ArrayBuffer that is filled by a worker thread?

208 Views Asked by At

I want to use ion-js but it a sync library over strings or ArrayBuffers, not a Stream or ReadableStream.

I thought to use worker_threads, SharedArrayBuffer, and Atomics to create a ArrayBuffer that is filled in a background thread and blocks waiting for bytes.

class BlockingArrayBuffer implements ArrayBuffer {
  constructor(
    private readonly dataBuffer: Uint8Array,
    private readonly availableBytes: Int32Array,
  ) {  }

  get byteLength(): number {
    return this.dataBuffer.byteLength;
  }

  slice(begin: number, end: number = this.byteLength): ArrayBuffer {
    // calls Atomics.wait till availableBytes is greater than begin and end.
    this.blockTillAvailable(Math.max(begin, end));
    return this.dataBuffer.slice(begin, end);
  }
}

There's a worker thread not shown that reads the stream and updates the two shared array buffers backing dataBuffer and availableBytes.

This actually works when i call slice directly.

const {dataBuffer, availableBytes} = await makeWorker();
const blocking = new BlockingArrayBuffer(dataBuffer, availableBytes);
blocking.slice(0, 10); // completes quickly.
blocking.slice(0);     // blocks till buffer is full.

However it doesn't work when I wrap the blocking array with a typed array

console.log(blocking.slice(0)); // prints  filled array
console.log(new Uint8Array(blocking).slice(0)); // Uint8Array(0) []
0

There are 0 best solutions below