src/vs/platform/observable/common/observableMemento.ts

88 LOC · 73 covered · 15 uncovered · 15 ranges · 852 concepts · 4 introducers · 520 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.

1 > /*--------------------------------------------------------------------------------------------- observableMemento.ts ×5
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 { strictEquals } from '../../../base/common/equals.js';
7 > import { DisposableStore, IDisposable } from '../../../base/common/lifecycle.js';
8 > import { DebugLocation } from '../../../base/common/observable.js';
9 > // eslint-disable-next-line local/code-no-deep-import-of-internal
10 > import { DebugNameData } from '../../../base/common/observableInternal/debugName.js';
11 > // eslint-disable-next-line local/code-no-deep-import-of-internal
12 > import { ObservableValue } from '../../../base/common/observableInternal/observables/observableValue.js';
13 > import { IStorageService, StorageScope, StorageTarget } from '../../storage/common/storage.js';
14 >
15 > interface IObservableMementoOpts<T> {
16 > defaultValue: T;
17 > key: string;
18 > toStorage: (value: T) => string;
19 > fromStorage: (value: string) => T;
20 > }
21 >
22 > /**
23 > * Defines an observable memento. Returns a function that can be called with
24 > * the specific storage scope, target, and service to use in a class.
25 > *
26 > * Note that the returned Observable is a disposable, because it interacts
27 > * with storage service events, and must be tracked appropriately.
28 > */
29 > export function observableMemento<T>(opts: IObservableMementoOpts<T>) {
30 > return (scope: StorageScope, target: StorageTarget, storageService: IStorageService): ObservableMemento<T> => { observableMemento.ts ×2
31 > return new ObservableMemento<T>(opts, scope, target, storageService); observableMemento.ts ×5
32 > };
35 > /**
36 > * A value that is stored, and is also observable. Note: T should be readonly.
37 > */
38 > export class ObservableMemento<T> extends ObservableValue<T> implements IDisposable {
39 > private readonly _store = new DisposableStore();
40 > private _noStorageUpdateNeeded = false;
41 >
42 > constructor(
43 > private readonly opts: IObservableMementoOpts<T>, observableMemento.ts ×5
44 > private readonly storageScope: StorageScope,
45 > private readonly storageTarget: StorageTarget,
46 > @IStorageService private readonly storageService: IStorageService,
47 > ) {
48 > const getStorageValue = (): T => {
49 > const fromStorage = storageService.get(opts.key, storageScope);
50 > if (fromStorage !== undefined) {
51 try {
52 return opts.fromStorage(fromStorage);
53 } catch {
54 return opts.defaultValue;
55 }
56 }
57 > return opts.defaultValue; observableMemento.ts ×5
58 > };
59 >
60 > const initialValue = getStorageValue();
61 > super(new DebugNameData(undefined, `storage/${opts.key}`, undefined), initialValue, strictEquals, DebugLocation.ofCaller());
62 >
63 > const didChange = storageService.onDidChangeValue(storageScope, opts.key, this._store);
64 > this._store.add(didChange((e) => {
65 > if (e.external && e.key === opts.key) { observableMemento.ts ×3
66 this._noStorageUpdateNeeded = true;
67 try {
68 this.set(getStorageValue(), undefined);
69 } finally {
70 this._noStorageUpdateNeeded = false;
71 }
72 }
74 > }
76 > protected override _setValue(newValue: T): void {
77 > super._setValue(newValue); observableMemento.ts ×3
78 > if (this._noStorageUpdateNeeded) {
79 return;
80 }
81 > const valueToStore = this.opts.toStorage(this.get()); observableMemento.ts ×3
82 > this.storageService.store(this.opts.key, valueToStore, this.storageScope, this.storageTarget);
83 > }
85 > dispose(): void {
86 > this._store.dispose(); observableMemento.ts ×5
87 > }