@@ -16,6 +16,8 @@ class FsAdapter implements PersistAdapter {
1616 private readonly root : string ;
1717 private readonly maxVersions : number ;
1818 private errorHandlers : Array < ( e : PersistErrorEvent ) => void > = [ ] ;
19+ private readonly inflightWrites = new Set < Promise < void > > ( ) ;
20+ private versionCounter = 0 ;
1921
2022 constructor ( opts : FsAdapterOptions ) {
2123 this . root = opts . root ;
@@ -32,6 +34,16 @@ class FsAdapter implements PersistAdapter {
3234 }
3335
3436 async write ( path : string , content : string ) : Promise < void > {
37+ const p = this . doWrite ( path , content ) ;
38+ this . inflightWrites . add ( p ) ;
39+ try {
40+ await p ;
41+ } finally {
42+ this . inflightWrites . delete ( p ) ;
43+ }
44+ }
45+
46+ private async doWrite ( path : string , content : string ) : Promise < void > {
3547 try {
3648 const abs = this . abs ( path ) ;
3749 await mkdir ( dirname ( abs ) , { recursive : true } ) ;
@@ -42,7 +54,9 @@ class FsAdapter implements PersistAdapter {
4254 }
4355 }
4456
45- async flush ( ) : Promise < void > { }
57+ async flush ( ) : Promise < void > {
58+ await Promise . all ( [ ...this . inflightWrites ] ) ;
59+ }
4660
4761 async listVersions ( path : string ) : Promise < PersistVersionEntry [ ] > {
4862 const dir = this . versionsDir ( path ) ;
@@ -92,7 +106,8 @@ class FsAdapter implements PersistAdapter {
92106 private async appendVersion ( path : string , content : string ) : Promise < void > {
93107 const dir = this . versionsDir ( path ) ;
94108 await mkdir ( dir , { recursive : true } ) ;
95- const key = String ( Date . now ( ) ) ;
109+ // Pad counter to 6 digits so lexicographic sort = insertion order within same ms.
110+ const key = `${ Date . now ( ) } _${ String ( this . versionCounter ++ ) . padStart ( 6 , "0" ) } ` ;
96111 await writeFile ( join ( dir , `${ key } .html` ) , content , "utf8" ) ;
97112 // prune oldest beyond maxVersions
98113 const all = ( await readdir ( dir ) ) . filter ( ( f ) => f . endsWith ( ".html" ) ) . sort ( ) ;
0 commit comments