Skip to main content

createTextRingBuffer

function createTextRingBuffer(options): TextRingBuffer;

Creates a bounded store of text lines, backed by a ring of pre-allocated Uint8Array segments which lines are encoded into with TextEncoder.encodeInto.

Lines are written into the active segment until one does not fit, at which point the segment is sealed and the next one becomes active. Once every segment is in use the oldest is recycled, dropping its lines. Memory usage is therefore bounded by segmentByteSize * segmentCount, regardless of how much is logged.

Because lines never straddle segments, a sealed segment is a self-contained block of complete lines, which makes it a natural unit to hand over to persistent storage via onSegmentSealed.

Example usage:

const buffer = createTextRingBuffer({segmentByteSize: 1024, segmentCount: 2});
buffer.push('first\n');
buffer.push('second\n');
buffer.toString(); // 'first\nsecond\n'

Parameters​

ParameterTypeDescription
optionsTextRingBufferOptionsSee TextRingBufferOptions

Returns​

TextRingBuffer

TextRingBuffer