Skip to content

Commit e7c77a3

Browse files
author
Benjamin Pasero
committed
storage - include a WillSaveStateReason when saving
1 parent 08d8cc1 commit e7c77a3

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

src/vs/platform/storage/common/storage.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ import { isUndefinedOrNull } from 'vs/base/common/types';
1010

1111
export const IStorageService = createDecorator<IStorageService>('storageService');
1212

13+
export enum WillSaveStateReason {
14+
NONE = 0,
15+
SHUTDOWN = 1
16+
}
17+
18+
export interface IWillSaveStateEvent {
19+
reason: WillSaveStateReason;
20+
}
21+
1322
export interface IStorageService {
1423
_serviceBrand: any;
1524

@@ -22,8 +31,12 @@ export interface IStorageService {
2231
* Emitted when the storage is about to persist. This is the right time
2332
* to persist data to ensure it is stored before the application shuts
2433
* down.
34+
*
35+
* The will save state event allows to optionally ask for the reason of
36+
* saving the state, e.g. to find out if the state is saved due to a
37+
* shutdown.
2538
*/
26-
readonly onWillSaveState: Event<void>;
39+
readonly onWillSaveState: Event<IWillSaveStateEvent>;
2740

2841
/**
2942
* Retrieve an element stored with the given key from storage. Use

src/vs/platform/storage/node/storageService.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { Disposable, IDisposable, dispose } from 'vs/base/common/lifecycle';
77
import { Event, Emitter } from 'vs/base/common/event';
88
import { ILogService, LogLevel } from 'vs/platform/log/common/log';
9-
import { IWorkspaceStorageChangeEvent, IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
9+
import { IWorkspaceStorageChangeEvent, IStorageService, StorageScope, IWillSaveStateEvent, WillSaveStateReason } from 'vs/platform/storage/common/storage';
1010
import { Storage, ISQLiteStorageDatabaseLoggingOptions, IStorage, StorageHint, IStorageDatabase, SQLiteStorageDatabase } from 'vs/base/node/storage';
1111
import { IStorageLegacyService, StorageLegacyScope } from 'vs/platform/storage/common/storageLegacyService';
1212
import { startsWith, endsWith } from 'vs/base/common/strings';
@@ -31,8 +31,8 @@ export class StorageService extends Disposable implements IStorageService {
3131
private _onDidChangeStorage: Emitter<IWorkspaceStorageChangeEvent> = this._register(new Emitter<IWorkspaceStorageChangeEvent>());
3232
get onDidChangeStorage(): Event<IWorkspaceStorageChangeEvent> { return this._onDidChangeStorage.event; }
3333

34-
private _onWillSaveState: Emitter<void> = this._register(new Emitter<void>());
35-
get onWillSaveState(): Event<void> { return this._onWillSaveState.event; }
34+
private _onWillSaveState: Emitter<IWillSaveStateEvent> = this._register(new Emitter<IWillSaveStateEvent>());
35+
get onWillSaveState(): Event<IWillSaveStateEvent> { return this._onWillSaveState.event; }
3636

3737
private _hasErrors = false;
3838
get hasErrors(): boolean { return this._hasErrors; }
@@ -373,7 +373,7 @@ export class StorageService extends Disposable implements IStorageService {
373373
this.workspaceStorage.beforeClose();
374374

375375
// Signal as event so that clients can still store data
376-
this._onWillSaveState.fire();
376+
this._onWillSaveState.fire({ reason: WillSaveStateReason.SHUTDOWN });
377377

378378
// Do it
379379
mark('willCloseGlobalStorage');
@@ -499,8 +499,8 @@ export class DelegatingStorageService extends Disposable implements IStorageServ
499499
private _onDidChangeStorage: Emitter<IWorkspaceStorageChangeEvent> = this._register(new Emitter<IWorkspaceStorageChangeEvent>());
500500
get onDidChangeStorage(): Event<IWorkspaceStorageChangeEvent> { return this._onDidChangeStorage.event; }
501501

502-
private _onWillSaveState: Emitter<void> = this._register(new Emitter<void>());
503-
get onWillSaveState(): Event<void> { return this._onWillSaveState.event; }
502+
private _onWillSaveState: Emitter<IWillSaveStateEvent> = this._register(new Emitter<IWillSaveStateEvent>());
503+
get onWillSaveState(): Event<IWillSaveStateEvent> { return this._onWillSaveState.event; }
504504

505505
private closed: boolean;
506506
private useLegacyWorkspaceStorage: boolean;
@@ -520,7 +520,7 @@ export class DelegatingStorageService extends Disposable implements IStorageServ
520520

521521
private registerListeners(): void {
522522
this._register(this.storageService.onDidChangeStorage(e => this._onDidChangeStorage.fire(e)));
523-
this._register(this.storageService.onWillSaveState(() => this._onWillSaveState.fire()));
523+
this._register(this.storageService.onWillSaveState(e => this._onWillSaveState.fire(e)));
524524

525525
const globalKeyMarker = 'storage://global/';
526526

0 commit comments

Comments
 (0)