1
>
/*---------------------------------------------------------------------------------------------
observableMemento.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 { 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> => {
31
return new ObservableMemento<T>(opts, scope, target, storageService);
32
};
33
}
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>,
44
private readonly storageScope: StorageScope,