@@ -13,6 +13,7 @@ import { IFileService } from 'vs/platform/files/common/files';
1313import { IStorage , Storage } from 'vs/base/parts/storage/common/storage' ;
1414import { URI } from 'vs/base/common/uri' ;
1515import { joinPath } from 'vs/base/common/resources' ;
16+ import { runWhenIdle } from 'vs/base/common/async' ;
1617
1718export class BrowserStorageService extends Disposable implements IStorageService {
1819
@@ -37,6 +38,26 @@ export class BrowserStorageService extends Disposable implements IStorageService
3738 @IFileService private readonly fileService : IFileService
3839 ) {
3940 super ( ) ;
41+
42+ // In the browser we do not have support for long running unload sequences. As such,
43+ // we cannot ask for saving state in that moment, because that would result in a
44+ // long running operation.
45+ // Instead, periodically ask customers to save save. The library will be clever enough
46+ // to only save state that has actually changed.
47+ this . saveStatePeriodically ( ) ;
48+ }
49+
50+ private saveStatePeriodically ( ) : void {
51+ setTimeout ( ( ) => {
52+ runWhenIdle ( ( ) => {
53+
54+ // this event will potentially cause new state to be stored
55+ this . _onWillSaveState . fire ( { reason : WillSaveStateReason . NONE } ) ;
56+
57+ // repeat
58+ this . saveStatePeriodically ( ) ;
59+ } ) ;
60+ } , 5000 ) ;
4061 }
4162
4263 initialize ( payload : IWorkspaceInitializationPayload ) : Promise < void > {
@@ -66,8 +87,6 @@ export class BrowserStorageService extends Disposable implements IStorageService
6687 ] ) ;
6788 }
6889
69- //#region
70-
7190 get ( key : string , scope : StorageScope , fallbackValue : string ) : string ;
7291 get ( key : string , scope : StorageScope ) : string | undefined ;
7392 get ( key : string , scope : StorageScope , fallbackValue ?: string ) : string | undefined {
@@ -94,18 +113,6 @@ export class BrowserStorageService extends Disposable implements IStorageService
94113 this . getStorage ( scope ) . delete ( key ) ;
95114 }
96115
97- async close ( ) : Promise < void > {
98-
99- // Signal as event so that clients can still store data
100- this . _onWillSaveState . fire ( { reason : WillSaveStateReason . SHUTDOWN } ) ;
101-
102- // Do it
103- await Promise . all ( [
104- this . globalStorage . close ( ) ,
105- this . workspaceStorage . close ( )
106- ] ) ;
107- }
108-
109116 private getStorage ( scope : StorageScope ) : IStorage {
110117 return scope === StorageScope . GLOBAL ? this . globalStorage : this . workspaceStorage ;
111118 }
@@ -118,6 +125,4 @@ export class BrowserStorageService extends Disposable implements IStorageService
118125
119126 return logStorage ( result [ 0 ] , result [ 1 ] , this . globalStorageFile . toString ( ) , this . workspaceStorageFile . toString ( ) ) ;
120127 }
121-
122- //#endregion
123128}
0 commit comments