I needed to communicate between a window and a worker, where the worker wouldn't have enough time to process incoming messages (there was 100% utilisation via a WASM app in the worker).
Shared Array Buffers (SAB) allow a window and workers to share a region of memory, but that memory can is a fixed size that is defined at the creation time of the SAB
RingBuffer.create(byteCount)
is a static method that creates a Ring Buffer of length 1024 bytes (and some header information)
const rb = RingBuffer.create(1024)
RingBuffer.from(sab)
is a static method to initialize a new instance of RingBuffer from an existing SAB. This is needed because it's impossible to pass the class over postMessage
onst rb1 = RingBuffer.create(1024)
const rb2 = RingBuffer.from(rb1.buffer)
buffer
will give you access to the raw Shared Array Buffer. It is not intended for general use.
const rb = RingBuffer.create(1024)
rb.buffer
append(array, fillToMax = false)
adds data into the buffer. If there are more entries to append that space, an exception is thrown.
const rb = RingBuffer.create(5)
rb.append([1,2,3,4])
The Ring Buffer will contain 1,2,3,4, with 1 element unfilled
If the append
method is called with fillToMax: true
and the input array is too large, it add all the items it can and return what
was not added to the buffer.
const rb = RingBuffer.create(5)
rb.append([1,2,3,4]);
rb.append([5,6,7], true); // => [6, 7]
read()
reads a byte of data from the Ring Buffer, and advances the internal pointer to the next entry. Returns undefined
when there are no more entries to read.
const rb = RingBuffer.create(1024)
rb.append([1,2,3,4])
const byte = rb.read() // 1
const nextByte = rb.read() // 2
readToHead()
is a generator that returns an iterator of the bytes that have not yet been read from the buffer.
const rb = RingBuffer.create(1024)
rb.append([1,2,3,4])
const byte = rb.read() // 1
const bytes = [...rb.readToHead()] // [2, 3, 4]
clear()
will reset the internal readIndex and writeIndex, effectively emptying the Buffer.
const rb = RingBuffer.create(1024)
rb.append([1,2,3,4])
rb.clear()
const byte = rb.read() // undefined