Skip to content

Commit 32f174c

Browse files
committed
Class to access extension state storage
1 parent 8c43ca8 commit 32f174c

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

src/util/localStorageService.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* ---------------------------------------------------------------------------------------------
2+
* Copyright (c) Applied Eng & Design All rights reserved.
3+
* Licensed under the MIT License. See License.md in the project root for license information.
4+
* -------------------------------------------------------------------------------------------- */
5+
'use strict';
6+
7+
export type LSSState = 'global' | 'workspace';
8+
9+
import { ExtensionContext, Memento } from 'vscode';
10+
11+
export class LocalStorageService {
12+
private _wsState: Memento;
13+
private _globalState: Memento;
14+
15+
constructor(context: ExtensionContext) {
16+
this._wsState = context.workspaceState;
17+
this._globalState = context.globalState;
18+
}
19+
20+
public getValue<T>(key: string, loc: LSSState): T | undefined {
21+
if (loc === 'workspace') {
22+
return this._wsState.get<T>(key);
23+
} else {
24+
return this._globalState.get<T>(key);
25+
}
26+
}
27+
28+
public async setValue<T>(key: string, value: T, loc: LSSState) {
29+
if (loc === 'workspace') {
30+
await this._wsState.update(key, value);
31+
} else {
32+
await this._globalState.update(key, value);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)