UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

62 lines • 1.89 kB
import { redactJobWidgetState } from "./view-redact.js"; export const PUBLISH_INTERVAL_MS = 1000; function displayKey(state) { return JSON.stringify(state.jobs.map(({ updatedAtMs: _updatedAtMs, ...shown }) => shown)); } const EMPTY_KEY = "[]"; export class JobWidgetStatePublisher { #send; #redact; #sentKey = EMPTY_KEY; #sentAt; #latest; #timer; #disposed = false; constructor(send, redact) { this.#send = send; this.#redact = redact; } /** `state` as it may leave the engine: every view string redacted. */ redacted(state) { return redactJobWidgetState(state, this.#redact); } /** The newest state; sent now, at the end of the current interval, or not at all when unchanged. */ offer(state) { if (this.#disposed) return; this.#latest = state; if (this.#timer !== undefined) return; const wait = this.#sentAt === undefined ? 0 : this.#sentAt + PUBLISH_INTERVAL_MS - Date.now(); if (wait <= 0) { this.#flush(); return; } this.#timer = setTimeout(() => { this.#timer = undefined; this.#flush(); }, wait); this.#timer.unref?.(); } /** Forgets what was sent, so the next offer goes out even if unchanged (a new listener needs it). */ reset() { this.#sentKey = ""; } dispose() { this.#disposed = true; clearTimeout(this.#timer); this.#timer = undefined; } #flush() { if (!this.#latest) return; const state = this.redacted(this.#latest); const key = displayKey(state); if (key === this.#sentKey) return; this.#sentKey = key; this.#sentAt = Date.now(); this.#send(state); } } //# sourceMappingURL=state-publisher.js.map