Sha256: 9121b5fe656b62720933fc445c8d7989d72f5d1600a26a2b5c49d55e398e7b6a
Contents?: true
Size: 922 Bytes
Versions: 26
Compression:
Stored size: 922 Bytes
Contents
let buffer, bufferMax; export class BufferEmptyError extends Error { constructor(message) { super(); this.message = message || 'Buffer is empty.'; } }; export class BufferFullError extends Error { constructor(message) { super(); this.message = message || 'Buffer is full.'; } }; const read = () => { if (buffer.length === 0){ throw new BufferEmptyError(); } return buffer.splice(0,1)[0]; }; const write = (value) => { if (buffer.length === bufferMax){ throw new BufferFullError(); } value ? buffer.push(value) : null; }; const forceWrite = (value) => { if (buffer.length === bufferMax){ read(); } write(value); }; const clear = () => buffer = []; const CircularBuffer = (capacity) => { buffer = []; bufferMax = capacity; return { read: read, write: write, forceWrite: forceWrite, clear: clear }; }; export { CircularBuffer as default };
Version data entries
26 entries across 26 versions & 1 rubygems