UNPKG

@ndn/util

Version:
29 lines (28 loc) 818 B
/** Reorder items according to their index numbers. */ export class Reorder { next; buffer = new Map(); constructor(first = 0) { this.next = first; } /** Return number of items in buffer. */ get size() { return this.buffer.size; } /** Determine whether buffer is empty, i.e. all items emitted. */ get empty() { return this.buffer.size === 0; } /** Add a new item. */ push(index, obj) { if (index >= this.next) { this.buffer.set(index, obj); } } /** Return and remove in-order items. */ shift() { const result = []; while (this.buffer.has(this.next)) { result.push(this.buffer.get(this.next)); this.buffer.delete(this.next); ++this.next; } return result; } }