feat : refactor to use autosave.ts for stygian
This commit is contained in:
+17
-4
@@ -48,6 +48,10 @@ export class AutosaveCoordinator<Draft, Payload, Snapshot = unknown> {
|
||||
return this.version;
|
||||
}
|
||||
|
||||
updateVersion(version: number): void {
|
||||
this.version = Math.max(this.version, version);
|
||||
}
|
||||
|
||||
schedule(draft: Draft): void {
|
||||
if (this.disposed) return;
|
||||
this.pending = draft;
|
||||
@@ -61,6 +65,14 @@ export class AutosaveCoordinator<Draft, Payload, Snapshot = unknown> {
|
||||
);
|
||||
}
|
||||
|
||||
cancelPending(): void {
|
||||
this.pending = undefined;
|
||||
this.generation += 1;
|
||||
this.clearDebounce();
|
||||
if (this.retryTimer !== undefined) this.saving = false;
|
||||
this.clearRetry();
|
||||
}
|
||||
|
||||
async flush(): Promise<void> {
|
||||
this.clearDebounce();
|
||||
if (this.disposed || this.saving || this.pending === undefined) return;
|
||||
@@ -101,11 +113,12 @@ export class AutosaveCoordinator<Draft, Payload, Snapshot = unknown> {
|
||||
|
||||
if (this.disposed) return;
|
||||
if (response.status === "saved") {
|
||||
this.version = response.version;
|
||||
this.updateVersion(response.version);
|
||||
this.saving = false;
|
||||
this.options.onSaved?.(response.snapshot, response.version);
|
||||
this.emit({ state: "saved", version: response.version, savedAt: new Date() });
|
||||
if (this.pending !== undefined) void this.flush();
|
||||
if (Object.is(this.pending, draft)) this.cancelPending();
|
||||
this.options.onSaved?.(response.snapshot, this.version);
|
||||
this.emit({ state: "saved", version: this.version, savedAt: new Date() });
|
||||
if (this.pending !== undefined && this.debounceTimer === undefined) void this.flush();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user