// The labels this process has emitted, in sequence order. // // A subscriber that connects with a cursor gets every frame after it, then // waits for new ones. Frames are the unit subscribeLabels carries; keeping a // bounded history is enough for a client that reconnects after a short gap. const MAX_FRAMES = 10_000 export class LabelStore { constructor() { /** Every signed label, negations included, for queryLabels. */ this.all = [] /** Frame history, oldest first. */ this.frames = [] this.seq = 0 this.listeners = new Set() } /** Append one frame. Returns it, or undefined when there is nothing to say. */ append(labels) { if (!labels || labels.length === 0) return undefined const frame = { seq: ++this.seq, labels } this.frames.push(frame) this.all.push(...labels) if (this.frames.length > MAX_FRAMES) this.frames.shift() for (const listener of this.listeners) listener(frame) return frame } /** Frames after `cursor`, in order. */ since(cursor) { return this.frames.filter((frame) => frame.seq > cursor) } /** Listen for new frames. Returns the unsubscribe function. */ subscribe(listener) { this.listeners.add(listener) return () => this.listeners.delete(listener) } }