src/vs/workbench/common/memento.ts
164 LOC · 117 covered · 47 uncovered · 33 ranges · 379 concepts · 7 introducers · 192 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.
/*---------------------------------------------------------------------------------------------
memento.ts ×12
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IStorageService, IStorageValueChangeEvent, StorageScope, StorageTarget } from '../../platform/storage/common/storage.js';
import { isEmptyObject } from '../../base/common/types.js';
import { onUnexpectedError } from '../../base/common/errors.js';
import { DisposableStore } from '../../base/common/lifecycle.js';
import { Event } from '../../base/common/event.js';
export class Memento<T extends object> {
private static readonly applicationMementos = new Map<string, ScopedMemento<unknown>>();
private static readonly applicationSharedMementos = new Map<string, ScopedMemento<unknown>>();
private static readonly profileMementos = new Map<string, ScopedMemento<unknown>>();
private static readonly workspaceMementos = new Map<string, ScopedMemento<unknown>>();
private static readonly COMMON_PREFIX = 'memento/';
private readonly id: string;
constructor(id: string, private storageService: IStorageService) {
}
getMemento(scope: StorageScope, target: StorageTarget): Partial<T> {
case StorageScope.WORKSPACE: {
let workspaceMemento = Memento.workspaceMementos.get(this.id);
if (!workspaceMemento) {
workspaceMemento = new ScopedMemento(this.id, scope, target, this.storageService);
Memento.workspaceMementos.set(this.id, workspaceMemento);
}
return workspaceMemento.getMemento();
}
case StorageScope.PROFILE: {
if (!profileMemento) {
profileMemento = new ScopedMemento(this.id, scope, target, this.storageService);
Memento.profileMementos.set(this.id, profileMemento);
}
return profileMemento.getMemento();
}
case StorageScope.APPLICATION: {
if (!applicationMemento) {
applicationMemento = new ScopedMemento(this.id, scope, target, this.storageService);
Memento.applicationMementos.set(this.id, applicationMemento);
}
return applicationMemento.getMemento();
}
case StorageScope.APPLICATION_SHARED: {
let applicationSharedMemento = Memento.applicationSharedMementos.get(this.id);
if (!applicationSharedMemento) {
applicationSharedMemento = new ScopedMemento(this.id, scope, target, this.storageService);
Memento.applicationSharedMementos.set(this.id, applicationSharedMemento);
}
return applicationSharedMemento.getMemento();
}
}
onDidChangeValue(scope: StorageScope, disposables: DisposableStore): Event<IStorageValueChangeEvent> {
return this.storageService.onDidChangeValue(scope, this.id, disposables);
}
saveMemento(): void {
Memento.profileMementos.get(this.id)?.save();
Memento.applicationMementos.get(this.id)?.save();
Memento.applicationSharedMementos.get(this.id)?.save();
}
reloadMemento(scope: StorageScope): void {
let memento: ScopedMemento<unknown> | undefined;
switch (scope) {
case StorageScope.APPLICATION_SHARED:
memento = Memento.applicationSharedMementos.get(this.id);
break;
case StorageScope.APPLICATION:
memento = Memento.applicationMementos.get(this.id);
break;
case StorageScope.PROFILE:
memento = Memento.profileMementos.get(this.id);
break;
case StorageScope.WORKSPACE:
memento = Memento.workspaceMementos.get(this.id);
break;
}
memento?.reload();
}
static clear(scope: StorageScope): void {
case StorageScope.WORKSPACE:
Memento.workspaceMementos.clear();
break;
case StorageScope.PROFILE:
break;
break;
Memento.applicationSharedMementos.clear();
break;
}
class ScopedMemento<T> {
private mementoObj: Partial<T>;
constructor(private id: string, private scope: StorageScope, private target: StorageTarget, private storageService: IStorageService) {
}
private doLoad(): Partial<T> {
return this.storageService.getObject(this.id, this.scope, {});
} catch (error) {
// Seeing reports from users unable to open editors
// from memento parsing exceptions. Log the contents
// to diagnose further
// https://github.com/microsoft/vscode/issues/102251
onUnexpectedError(`[memento]: failed to parse contents: ${error} (id: ${this.id}, scope: ${this.scope}, contents: ${this.storageService.get(this.id, this.scope)})`);
}
return {};
getMemento(): Partial<T> {
}
reload(): void {
// Clear old
for (const name of Object.getOwnPropertyNames(this.mementoObj)) {
delete this.mementoObj[name as keyof Partial<T>];
}
// Assign new
Object.assign(this.mementoObj, this.doLoad());
}
save(): void {
this.storageService.store(this.id, this.mementoObj, this.scope, this.target);
} else {
}