1
>
/*---------------------------------------------------------------------------------------------
stateService.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import { ThrottledDelayer } from '../../../base/common/async.js';
7
>
import { VSBuffer } from '../../../base/common/buffer.js';
8
>
import { Disposable } from '../../../base/common/lifecycle.js';
9
>
import { isUndefined, isUndefinedOrNull } from '../../../base/common/types.js';
10
>
import { URI } from '../../../base/common/uri.js';
11
>
import { IEnvironmentService } from '../../environment/common/environment.js';
12
>
import { FileOperationError, FileOperationResult, IFileService } from '../../files/common/files.js';
13
>
import { ILogService } from '../../log/common/log.js';
14
>
import { IStateReadService, IStateService } from './state.js';
15
>
16
>
type StorageDatabase = { [key: string]: unknown };
17
>
18
>
export const enum SaveStrategy {
19
>
IMMEDIATE,
20
>
DELAYED
21
>
}
22
>
23
>
export class FileStorage extends Disposable {
24
>
25
>
private storage: StorageDatabase = Object.create(null);
26
>
private lastSavedStorageContents = '';
27
>
28
>
private readonly flushDelayer: ThrottledDelayer<void>;
29
>
30
>
private initializing: Promise<void> | undefined = undefined;
31
>
private closing: Promise<void> | undefined = undefined;
32
>
33
>
constructor(
34
>
private readonly storagePath: URI,
35
>
saveStrategy: SaveStrategy,
36
>
private readonly logService: ILogService,
37
>
private readonly fileService: IFileService,
38
>
) {
39
>
super();
40
>
41
>
this.flushDelayer = this._register(new ThrottledDelayer<void>(saveStrategy === SaveStrategy.IMMEDIATE ? 0 : 100 /* buffer saves over a short time */));
42
>
}
43
>
44
>
init(): Promise<void> {
45
if (!this.initializing) {
46
this.initializing = this.doInit();