/** * TraceRecorder — per-channel ring buffer for oscilloscope capture. * * Responsibility split: this module owns capture (time-series storage, * monotonic-time enforcement, ring-buffer trimming) so the UI component * owns only presentation (uPlot, RAF loop, React state). Stage 5 Wave A. * * Design constraint: zero React/zustand/DOM imports. This file must remain * importable from workers, tests, and server-side code without side effects. * * No React state is mutated per tick. The subscription closure calls * record() directly into these plain arrays; the RAF loop reads them * without going through React at all. */ export interface TraceSnapshot { /** Monotonically increasing time values, one entry per sample. */ t: number[]; /** * One entry per channel, in the same order as the keys passed to * setChannels(). Each inner array is a deep copy independent of the * recorder's live buffers. */ channels: Array<{ key: string; values: number[]; }>; } export declare class TraceRecorder { private readonly maxPoints; private keys; private acquisitionId; private _revision; /** Parallel arrays: t[i] is the time of sample i, ch[k][i] its value. */ private t; private ch; constructor(options?: { maxPoints?: number; }); /** * Replace the active channel set, reconciling columns in place so that * selecting or deselecting one channel does NOT throw away the others' * history. Shared channels keep their full buffer (and keep rolling), removed * channels are dropped, and a newly-added channel is back-filled with NaN for * the already-buffered samples so it simply starts drawing from "now" (uPlot * renders NaN as a gap). Identical key lists (same order) are a no-op so * in-flight data is preserved; callers rely on that contract in tests. * * Full buffer resets happen at an explicit worker acquisition-generation * boundary. record()'s backward-time guard remains as a compatibility * fallback for callers that do not have generation metadata. */ setChannels(keys: readonly string[]): void; get channelKeys(): readonly string[]; /** * Mark the worker acquisition generation that subsequent samples belong to. * A changed generation clears every retained channel by default. A display * that can represent discontinuities may preserve history and insert a gap * across a warm reload. Returns true when the generation changed so stateful * front ends can reset their own history either way. */ startAcquisition(acquisitionId: number, options?: { preserveHistory?: boolean; }): boolean; /** * Append one sample. Guards: * - mismatched values length → ignored (mirrors today's buf.vs.length !== nets.length check) * - backward time jump by more than 1e-9 → buffers cleared first, then * the sample is recorded (today's reload guard, previously a local var * in the subscription closure; moving it here keeps it working even * when no React effect re-runs on reload, e.g. during hot reload or * fast circuit swaps) * - ring-buffer cap enforced by a single splice (same as today) */ record(t: number, values: readonly number[]): void; /** * Empty all buffers but keep the channel configuration. * * Backward-jump detection derives from the last buffered sample, so * after clear() the next record() is always accepted. The pre-extraction * code kept `lastT` in the subscription closure across clears, but the * observable outcome was identical: a backward jump after a clear just * cleared an already-empty buffer and recorded the sample. */ clear(): void; /** * The timestamp of the most-recently recorded sample, or null when the * buffer is empty. Used by the RAF loop to compute tEnd without exposing * the internal array. */ lastTime(): number | null; /** * The timestamp of the OLDEST buffered sample, or null when empty. With * lastTime() this bounds the buffered time span — used by the scope's * horizontal-pan control to know how far back acquisition memory reaches. */ firstTime(): number | null; get pointCount(): number; /** Monotonic invalidation token for renderers; not tied to React state. */ get revision(): number; /** * Return aligned time + value slices starting at the first index whose * t >= tStart. Uses binary search — O(log n) vs. O(n). * * Returns null when fewer than 2 points are buffered (matches the RAF * loop's `buf.t.length > 1` guard — with only one point uPlot has no * segment to draw). tStart before the first sample returns the full * buffer; tStart beyond the last sample returns null (nothing in the * window — unreachable from the scope's RAF loop, where tStart is * always lastTime() minus a positive time window). */ windowFrom(tStart: number): { t: number[]; values: number[][]; } | null; /** * Return aligned time + value slices for the closed range [tStart, tStop]. * Like windowFrom() but bounded on BOTH ends — used for horizontal pan, where * the visible window is a fixed slice somewhere inside acquisition memory * rather than the trailing window. Returns null when the range contains fewer * than 2 samples (uPlot has no segment to draw) or when tStop < tStart. */ windowRange(tStart: number, tStop: number): { t: number[]; values: number[][]; } | null; /** * Deep copy of the entire buffer. Mutations to the returned snapshot * never affect the recorder, and subsequent record() calls never mutate * a previously returned snapshot (each snapshot is fully independent). */ snapshot(): TraceSnapshot; private _resetBuffers; }