@@ -9,6 +9,8 @@ export default class ElectronStorage implements Storage {
99 private readonly _tempFile : string ;
1010 private readonly _initialized : Promise < boolean > ;
1111 private _cache : Map < string , string > ;
12+ private _flushPending : boolean = false ;
13+ private _flushQueue : Promise < void > = Promise . resolve ( ) ;
1214
1315 constructor ( private readonly _logger ?: LDLogger ) {
1416 this . _storageFile = path . join ( electron . app . getPath ( 'userData' ) , 'ldcache' ) ;
@@ -58,7 +60,7 @@ export default class ElectronStorage implements Storage {
5860 } catch ( cleanupError ) {
5961 this . _logError ( 'Error cleaning up temporary file' , cleanupError ) ;
6062 }
61- throw error ; // Re-throw the original error
63+ throw error ;
6264 }
6365 }
6466
@@ -78,11 +80,8 @@ export default class ElectronStorage implements Storage {
7880 try {
7981 await this . _throwIfNotInitialized ( ) ;
8082 if ( this . _cache . has ( key ) ) {
81- const cacheCopy = new Map ( this . _cache ) ;
82- cacheCopy . delete ( key ) ;
83- await this . _atomicWriteToFile ( cacheCopy ) ;
84- // Only update cache if write was successful
85- this . _cache = cacheCopy ;
83+ this . _cache . delete ( key ) ;
84+ await this . _scheduleFlush ( ) ;
8685 }
8786 } catch ( error ) {
8887 this . _logError ( `Error clearing key from storage: ${ key } , reason` , error ) ;
@@ -103,15 +102,28 @@ export default class ElectronStorage implements Storage {
103102 async set ( key : string , value : string ) : Promise < void > {
104103 try {
105104 await this . _throwIfNotInitialized ( ) ;
106- const cacheCopy = new Map ( this . _cache ) ;
107- cacheCopy . set ( key , value ) ;
108- await this . _atomicWriteToFile ( cacheCopy ) ;
109- // Only update cache if write was successful
110- this . _cache = cacheCopy ;
105+ this . _cache . set ( key , value ) ;
106+ await this . _scheduleFlush ( ) ;
111107 } catch ( error ) {
112108 this . _logError ( `Error setting key in storage: ${ key } , reason` , error ) ;
113109 }
114110 }
111+
112+ private _scheduleFlush ( ) : Promise < void > {
113+ if ( this . _flushPending ) {
114+ return this . _flushQueue ;
115+ }
116+ this . _flushPending = true ;
117+ this . _flushQueue = this . _flushQueue
118+ . then ( async ( ) => {
119+ this . _flushPending = false ;
120+ await this . _atomicWriteToFile ( new Map ( this . _cache ) ) ;
121+ } )
122+ . catch ( ( error ) => {
123+ this . _logError ( 'Error flushing storage to disk' , error ) ;
124+ } ) ;
125+ return this . _flushQueue ;
126+ }
115127}
116128
117129// Storage should be a singleton to support multiple instances of the SDK. This should have
0 commit comments