forked from LeetCode-OpenSource/vscode-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobalState.ts
55 lines (44 loc) · 1.49 KB
/
globalState.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) leo.zhao. All rights reserved.
// Licensed under the MIT license.
import * as vscode from "vscode";
const CookieKey = "leetcode-cookie";
const UserStatusKey = "leetcode-user-status";
export type UserDataType = {
isSignedIn: boolean;
isPremium: boolean;
username: string;
avatar: string;
isVerified?: boolean;
};
class GlobalState {
private context: vscode.ExtensionContext;
private _state: vscode.Memento;
private _cookie: string;
private _userStatus: UserDataType;
public initialize(context: vscode.ExtensionContext): void {
this.context = context;
this._state = this.context.globalState;
}
public setCookie(cookie: string): any {
this._cookie = cookie;
return this._state.update(CookieKey, this._cookie);
}
public getCookie(): string | undefined {
return this._cookie ?? this._state.get(CookieKey);
}
public setUserStatus(userStatus: UserDataType): any {
this._userStatus = userStatus;
return this._state.update(UserStatusKey, this._userStatus);
}
public getUserStatus(): UserDataType | undefined {
return this._userStatus ?? this._state.get(UserStatusKey);
}
public removeCookie(): void {
this._state.update(CookieKey, undefined);
}
public removeAll(): void {
this._state.update(CookieKey, undefined);
this._state.update(UserStatusKey, undefined);
}
}
export const globalState: GlobalState = new GlobalState();