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.
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.
/*---------------------------------------------------------------------------------------------
stateService.ts ×16
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ThrottledDelayer } from '../../../base/common/async.js';
import { VSBuffer } from '../../../base/common/buffer.js';
import { Disposable } from '../../../base/common/lifecycle.js';
import { isUndefined, isUndefinedOrNull } from '../../../base/common/types.js';
import { URI } from '../../../base/common/uri.js';
import { IEnvironmentService } from '../../environment/common/environment.js';
import { FileOperationError, FileOperationResult, IFileService } from '../../files/common/files.js';
import { ILogService } from '../../log/common/log.js';
import { IStateReadService, IStateService } from './state.js';
type StorageDatabase = { [key: string]: unknown };
export const enum SaveStrategy {
IMMEDIATE,
DELAYED
}
export class FileStorage extends Disposable {
private storage: StorageDatabase = Object.create(null);
private lastSavedStorageContents = '';
private readonly flushDelayer: ThrottledDelayer<void>;
private initializing: Promise<void> | undefined = undefined;
private closing: Promise<void> | undefined = undefined;
constructor(
private readonly storagePath: URI,
saveStrategy: SaveStrategy,
private readonly logService: ILogService,
private readonly fileService: IFileService,
) {
super();
this.flushDelayer = this._register(new ThrottledDelayer<void>(saveStrategy === SaveStrategy.IMMEDIATE ? 0 : 100 /* buffer saves over a short time */));
}
init(): Promise<void> {
this.initializing = this.doInit();
}
return this.initializing;
}
private async doInit(): Promise<void> {
this.lastSavedStorageContents = (await this.fileService.readFile(this.storagePath)).value.toString();
this.storage = JSON.parse(this.lastSavedStorageContents);
} catch (error) {
if ((<FileOperationError>error).fileOperationResult !== FileOperationResult.FILE_NOT_FOUND) {
this.logService.error(error);
}
}
}
getItem<T>(key: string, defaultValue: T): T;
getItem<T>(key: string, defaultValue?: T): T | undefined;
getItem<T>(key: string, defaultValue?: T): T | undefined {
if (isUndefinedOrNull(res)) {
return defaultValue;
}
return res as T;
}
setItem(key: string, data?: object | string | number | boolean | undefined | null): void {
this.setItems([{ key, data }]);
}
setItems(items: readonly { key: string; data?: object | string | number | boolean | undefined | null }[]): void {
let save = false;
for (const { key, data } of items) {
// Shortcut for data that did not change
if (this.storage[key] === data) {
}
// Remove items when they are undefined or null
if (isUndefinedOrNull(data)) {
this.storage[key] = undefined;
save = true;
}
}
// Otherwise add an item
else {
this.storage[key] = data;
save = true;
}
}
if (save) {
this.save();
}
}
removeItem(key: string): void {
// Only update if the key is actually present (not undefined)
if (!isUndefined(this.storage[key])) {
this.storage[key] = undefined;
this.save();
}
}
private async save(): Promise<void> {
if (this.closing) {
}
return this.flushDelayer.trigger(() => this.doSave());
}
private async doSave(): Promise<void> {
if (!this.initializing) {
}
// Make sure to wait for init to finish first
await this.initializing;
// Return early if the database has not changed
const serializedDatabase = JSON.stringify(this.storage, null, 4);
if (serializedDatabase === this.lastSavedStorageContents) {
}
// Write to disk
try {
await this.fileService.writeFile(this.storagePath, VSBuffer.fromString(serializedDatabase), { atomic: { postfix: '.vsctmp' } });
this.lastSavedStorageContents = serializedDatabase;
} catch (error) {
this.logService.error(error);
}
async close(): Promise<void> {
if (!this.closing) {
this.closing = this.flushDelayer.trigger(() => this.doSave(), 0 /* as soon as possible */);
}
return this.closing;
}
}
export class StateReadonlyService extends Disposable implements IStateReadService {
declare readonly _serviceBrand: undefined;
protected readonly fileStorage: FileStorage;
constructor(
saveStrategy: SaveStrategy,
@IEnvironmentService environmentService: IEnvironmentService,
@ILogService logService: ILogService,
@IFileService fileService: IFileService
) {
super();
this.fileStorage = this._register(new FileStorage(environmentService.stateResource, saveStrategy, logService, fileService));
}
async init(): Promise<void> {
await this.fileStorage.init();
}
getItem<T>(key: string, defaultValue: T): T;
getItem<T>(key: string, defaultValue?: T): T | undefined;
getItem<T>(key: string, defaultValue?: T): T | undefined {
return this.fileStorage.getItem(key, defaultValue);
}
export class StateService extends StateReadonlyService implements IStateService {
declare readonly _serviceBrand: undefined;
setItem(key: string, data?: object | string | number | boolean | undefined | null): void {
this.fileStorage.setItem(key, data);
}
setItems(items: readonly { key: string; data?: object | string | number | boolean | undefined | null }[]): void {
this.fileStorage.setItems(items);
}
removeItem(key: string): void {
this.fileStorage.removeItem(key);
}
close(): Promise<void> {
return this.fileStorage.close();
}