MCPcopy Create free account
hub / github.com/deepnote/vscode-deepnote / PersistentState

Class PersistentState

src/platform/common/persistentState.ts:11–39  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9 * Used to store values that survive the lifetime of the extension.
10 */
11export class PersistentState<T> implements IPersistentState<T> {
12 constructor(
13 private storage: Memento,
14 private key: string,
15 private defaultValue?: T,
16 private expiryDurationMs?: number
17 ) {}
18
19 public get value(): T {
20 if (this.expiryDurationMs) {
21 const cachedData = this.storage.get<{ data?: T; expiry?: number }>(this.key, { data: this.defaultValue! });
22 if (!cachedData || !cachedData.expiry || cachedData.expiry < Date.now()) {
23 return this.defaultValue!;
24 } else {
25 return cachedData.data!;
26 }
27 } else {
28 return this.storage.get<T>(this.key, this.defaultValue!);
29 }
30 }
31
32 public async updateValue(newValue: T): Promise<void> {
33 if (this.expiryDurationMs) {
34 await this.storage.update(this.key, { data: newValue, expiry: Date.now() + this.expiryDurationMs });
35 } else {
36 await this.storage.update(this.key, newValue);
37 }
38 }
39}
40
41@injectable()
42export class PersistentStateFactory implements IPersistentStateFactory {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected