Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

1.0 KB
Raw Blame
type Subscribe = (notify: () => void) => () => void;

/** A stable subscription boundary: covering a route must not itself render all
 * its rows. Resume asks each subscriber to check its snapshot; React updates
 * only consumers whose selected value actually changed. */
export class PausableSubscriptions {
  private entries = new Set<{ subscribe: Subscribe; notify: () => void; stop?: () => void }>();

  constructor(public enabled: boolean) {}

  setEnabled(enabled: boolean): void {
    if (this.enabled === enabled) return;
    this.enabled = enabled;
    for (const entry of this.entries) {
      if (enabled) {
        entry.stop = entry.subscribe(entry.notify);
        entry.notify();
      } else {
        entry.stop?.();
        entry.stop = undefined;
      }
    }
  }

  subscribe(subscribe: Subscribe, notify: () => void): () => void {
    const entry = { subscribe, notify, stop: this.enabled ? subscribe(notify) : undefined };
    this.entries.add(entry);
    return () => {
      this.entries.delete(entry);
      entry.stop?.();
    };
  }
}