The best way to control Claude Code, Codex and more from any computer or phone

main
ts 107 lines 3.16 KB
Raw
1 import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";
2
3 export interface FileSaveCoordinatorOptions<A, E> {
4 readonly debounceMs: number;
5 readonly canPersist?: () => boolean;
6 readonly persist: (contents: string) => Promise<AtomCommandResult<A, E>>;
7 readonly onPendingChange: (pending: boolean) => void;
8 /** Return false when another editor has newer unsaved contents. */
9 readonly onConfirmed: (contents: string) => boolean | void;
10 /** Called when a write pass ends with nothing further scheduled. */
11 readonly onSettled?: () => void;
12 }
13
14 export class FileSaveCoordinator<A = unknown, E = unknown> {
15 private timer: ReturnType<typeof setTimeout> | null = null;
16 private latestContents = "";
17 private latestRevision = 0;
18 private confirmedRevision = 0;
19 private lastChangeAt = 0;
20 private saving = false;
21 private disposed = false;
22 private flushing = false;
23
24 constructor(private readonly options: FileSaveCoordinatorOptions<A, E>) {}
25
26 change(contents: string): void {
27 if (this.disposed) return;
28 this.latestContents = contents;
29 this.latestRevision += 1;
30 this.lastChangeAt = Date.now();
31 this.options.onPendingChange(true);
32 this.schedule(this.options.debounceMs);
33 }
34
35 /** The newest contents not yet written, which is the last write this coordinator will make. */
36 get unconfirmedContents(): string | null {
37 return this.latestRevision === this.confirmedRevision ? null : this.latestContents;
38 }
39
40 /** Writes the newest draft now instead of after the debounce, then any draft queued behind it. */
41 flush(): void {
42 this.clearTimer();
43 this.flushing = true;
44 void this.persistLatest();
45 }
46
47 dispose(): void {
48 this.disposed = true;
49 this.clearTimer();
50 if (this.latestRevision > 0) void this.persistLatest();
51 }
52
53 private schedule(delay: number): void {
54 this.clearTimer();
55 this.timer = setTimeout(() => {
56 this.timer = null;
57 void this.persistLatest();
58 }, delay);
59 }
60
61 private clearTimer(): void {
62 if (this.timer === null) return;
63 clearTimeout(this.timer);
64 this.timer = null;
65 }
66
67 private async persistLatest(): Promise<void> {
68 if (this.saving) return;
69 if (this.latestRevision === this.confirmedRevision || this.options.canPersist?.() === false) {
70 this.settle();
71 return;
72 }
73
74 this.saving = true;
75 const contents = this.latestContents;
76 const revision = this.latestRevision;
77 const result = await this.options.persist(contents);
78 const succeeded = result._tag === "Success";
79 let confirmed = false;
80 if (succeeded) {
81 this.confirmedRevision = revision;
82 confirmed = this.options.onConfirmed(contents) !== false;
83 }
84
85 this.saving = false;
86 if (revision === this.latestRevision) {
87 if (confirmed) this.options.onPendingChange(false);
88 this.settle();
89 return;
90 }
91
92 const remainingDebounce = Math.max(
93 0,
94 this.options.debounceMs - (Date.now() - this.lastChangeAt),
95 );
96 if (this.disposed || this.flushing) {
97 void this.persistLatest();
98 } else {
99 this.schedule(remainingDebounce);
100 }
101 }
102
103 private settle(): void {
104 this.flushing = false;
105 this.options.onSettled?.();
106 }
107 }