src/vs/platform/state/node/stateService.ts

204 LOC · 181 covered · 23 uncovered · 27 ranges · 7 concepts · 7 introducers · 7 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the filesrc/vs/base/node/pfs.ts · 866 LOCnode/pfs.tssrc/vs/platform/files/common/fileService.ts · 1491 LOCcommon/fileService.tssrc/vs/platform/files/node/diskFileSystemProvider.ts · 898 LOCnode/diskFileSystemProvi…stateService.ts ×2 · 7 introduced LOCstateService.ts ×2stateService.ts ×1 · 2 introduced LOCstateService.ts ×1stateService.ts ×2 · 14 introduced LOCstateService.ts ×2stateService.ts ×1 · 2 introduced LOCstateService.ts ×1diskFileSystemProvider.ts ×9 · 73 introduced LOCdiskFileSystemProvider.t…stateService.ts ×1 · 2 introduced LOCstateService.ts ×1stateService.ts ×16 · 141 introduced LOCstateService.ts ×16state.test|title=StateService Basics (delayed strategy)|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/platform/state/test/node/state.test|title=StateService Basics (delayed strategy)|occurrence=1state.test|title=StateSe…state.test|title=StateService Basics (immediate strategy)|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/platform/state/test/node/state.test|title=StateService Basics (immediate strategy)|occurrence=1state.test|title=StateSe…state.test|title=StateService Closed before init|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/platform/state/test/node/state.test|title=StateService Closed before init|occurrence=1state.test|title=StateSe…state.test|title=StateService Multiple ops (Immediate Strategy)|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/platform/state/test/node/state.test|title=StateService Multiple ops (Immediate Strategy)|occurrence=1state.test|title=StateSe…state.test|title=StateService Multiple ops are buffered and applied|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/platform/state/test/node/state.test|title=StateService Multiple ops are buffered and applied|occurrence=1state.test|title=StateSe…state.test|title=StateService Used after close|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/platform/state/test/node/state.test|title=StateService Used after close|occurrence=1state.test|title=StateSe…state.test|title=StateService Used before init|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/platform/state/test/node/state.test|title=StateService Used before init|occurrence=1state.test|title=StateSe…Focused file · src/vs/platform/state/node/stateService.ts · 204 LOCnode/stateService.ts

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- stateService.ts ×16
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) { diskFileSystemProvider.ts ×9
46 > this.initializing = this.doInit();
47 > }
48 >
49 > return this.initializing;
50 > }
52 > private async doInit(): Promise<void> {
54 > this.lastSavedStorageContents = (await this.fileService.readFile(this.storagePath)).value.toString();
55 > this.storage = JSON.parse(this.lastSavedStorageContents);
56 > } catch (error) {
57 > if ((<FileOperationError>error).fileOperationResult !== FileOperationResult.FILE_NOT_FOUND) {
58 > this.logService.error(error);
59 > }
60 > }
61 > }
63 > getItem<T>(key: string, defaultValue: T): T;
64 > getItem<T>(key: string, defaultValue?: T): T | undefined;
65 > getItem<T>(key: string, defaultValue?: T): T | undefined {
66 > const res = this.storage[key]; stateService.ts ×2
67 > if (isUndefinedOrNull(res)) {
68 > return defaultValue;
69 > }
70 >
71 > return res as T;
72 > }
74 > setItem(key: string, data?: object | string | number | boolean | undefined | null): void {
75 > this.setItems([{ key, data }]);
76 > }
77 >
78 > setItems(items: readonly { key: string; data?: object | string | number | boolean | undefined | null }[]): void {
79 > let save = false;
80 >
81 > for (const { key, data } of items) {
82 >
83 > // Shortcut for data that did not change
84 > if (this.storage[key] === data) {
85 > continue; stateService.ts ×2
86 > }
88 > // Remove items when they are undefined or null
89 > if (isUndefinedOrNull(data)) {
90 > if (!isUndefined(this.storage[key])) { stateService.ts ×2
91 > this.storage[key] = undefined;
92 > save = true;
93 > }
94 > }
96 > // Otherwise add an item
97 > else {
98 > this.storage[key] = data;
99 > save = true;
100 > }
101 > }
102 >
103 > if (save) {
104 > this.save();
105 > }
106 > }
107 >
108 > removeItem(key: string): void {
110 > // Only update if the key is actually present (not undefined)
111 > if (!isUndefined(this.storage[key])) {
112 > this.storage[key] = undefined;
113 > this.save();
114 > }
115 > }
117 > private async save(): Promise<void> {
118 > if (this.closing) {
119 > return; // already about to close stateService.ts ×1
120 > }
122 > return this.flushDelayer.trigger(() => this.doSave());
123 > }
124 >
125 > private async doSave(): Promise<void> {
126 > if (!this.initializing) {
127 > return; // if we never initialized, we should not save our state stateService.ts ×1
128 > }
130 > // Make sure to wait for init to finish first
131 > await this.initializing;
132 >
133 > // Return early if the database has not changed
134 > const serializedDatabase = JSON.stringify(this.storage, null, 4);
135 > if (serializedDatabase === this.lastSavedStorageContents) {
136 > return; stateService.ts ×1
137 > }
139 > // Write to disk
140 > try {
141 > await this.fileService.writeFile(this.storagePath, VSBuffer.fromString(serializedDatabase), { atomic: { postfix: '.vsctmp' } });
142 > this.lastSavedStorageContents = serializedDatabase;
143 > } catch (error) {
144 this.logService.error(error);
145 }
147 >
148 > async close(): Promise<void> {
149 > if (!this.closing) {
150 > this.closing = this.flushDelayer.trigger(() => this.doSave(), 0 /* as soon as possible */);
151 > }
152 >
153 > return this.closing;
154 > }
155 > }
156 >
157 > export class StateReadonlyService extends Disposable implements IStateReadService {
158 >
159 > declare readonly _serviceBrand: undefined;
160 >
161 > protected readonly fileStorage: FileStorage;
162 >
163 > constructor(
164 saveStrategy: SaveStrategy,
165 @IEnvironmentService environmentService: IEnvironmentService,
166 @ILogService logService: ILogService,
167 @IFileService fileService: IFileService
168 ) {
169 super();
170
171 this.fileStorage = this._register(new FileStorage(environmentService.stateResource, saveStrategy, logService, fileService));
172 }
174 > async init(): Promise<void> {
175 await this.fileStorage.init();
176 }
178 > getItem<T>(key: string, defaultValue: T): T;
179 > getItem<T>(key: string, defaultValue?: T): T | undefined;
180 > getItem<T>(key: string, defaultValue?: T): T | undefined {
181 return this.fileStorage.getItem(key, defaultValue);
182 }
184 >
185 > export class StateService extends StateReadonlyService implements IStateService {
186 >
187 > declare readonly _serviceBrand: undefined;
188 >
189 > setItem(key: string, data?: object | string | number | boolean | undefined | null): void {
190 this.fileStorage.setItem(key, data);
191 }
193 > setItems(items: readonly { key: string; data?: object | string | number | boolean | undefined | null }[]): void {
194 this.fileStorage.setItems(items);
195 }
197 > removeItem(key: string): void {
198 this.fileStorage.removeItem(key);
199 }
201 > close(): Promise<void> {
202 return this.fileStorage.close();
203 }