@@ -3,6 +3,8 @@ import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";
33export interface FileSaveCoordinatorOptions < A , E > {
44 readonly debounceMs : number ;
55 readonly canPersist ?: ( ) => boolean ;
6+ /** Refuse stale close-time retries after another editor replaces the draft. */
7+ readonly shouldRetryAfterClose ?: ( contents : string ) => boolean ;
68 readonly persist : ( contents : string ) => Promise < AtomCommandResult < A , E > > ;
79 readonly onPendingChange : ( pending : boolean ) => void ;
810 /** Return false when another editor has newer unsaved contents. */
@@ -17,6 +19,7 @@ export class FileSaveCoordinator<A = unknown, E = unknown> {
1719 private lastChangeAt = 0 ;
1820 private saving = false ;
1921 private disposed = false ;
22+ private closeRetryPending = false ;
2023
2124 constructor ( private readonly options : FileSaveCoordinatorOptions < A , E > ) { }
2225
@@ -55,6 +58,14 @@ export class FileSaveCoordinator<A = unknown, E = unknown> {
5558 return ;
5659 }
5760
61+ if (
62+ this . closeRetryPending &&
63+ this . options . shouldRetryAfterClose ?.( this . latestContents ) === false
64+ ) {
65+ return ;
66+ }
67+ this . closeRetryPending = false ;
68+
5869 this . saving = true ;
5970 const contents = this . latestContents ;
6071 const revision = this . latestRevision ;
@@ -73,6 +84,7 @@ export class FileSaveCoordinator<A = unknown, E = unknown> {
7384 } else if ( ! succeeded && this . disposed ) {
7485 // Closing the editor must not discard an edit whose final save failed.
7586 // Retry after the normal debounce; a successful write stops the retry.
87+ this . closeRetryPending = true ;
7688 this . schedule ( this . options . debounceMs ) ;
7789 }
7890 return ;
0 commit comments