UNPKG

@thi.ng/buffers

Version:

General purpose and generic read/write buffer implementations with different behaviors/orderings

29 lines (28 loc) 584 B
import { FIFOBuffer } from "./fifo.js"; const sliding = (cap) => new SlidingBuffer(cap); class SlidingBuffer extends FIFOBuffer { copy() { const buf = new SlidingBuffer(1); buf.buf = this.buf.slice(); buf.rpos = this.rpos; buf.wpos = this.wpos; buf.len = this.len; return buf; } writable() { return true; } write(x) { if (!super.writable()) { const { buf, rpos } = this; buf[rpos] = void 0; this.rpos = (rpos + 1) % buf.length; this.len--; } return super.write(x); } } export { SlidingBuffer, sliding };