| 9 | * Used to store values that survive the lifetime of the extension. |
| 10 | */ |
| 11 | export 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() |
| 42 | export class PersistentStateFactory implements IPersistentStateFactory { |
nothing calls this directly
no outgoing calls
no test coverage detected