forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate-manager.ts
More file actions
46 lines (39 loc) · 1.4 KB
/
Copy pathstate-manager.ts
File metadata and controls
46 lines (39 loc) · 1.4 KB
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
/**
* This class exists only to simplify Jest testing of the real implementation
* which is in StateManagerImpl class.
*/
import {StateManagerImpl} from './state-manager-impl';
import {isNonPersistent} from './platform';
export class StateManager<T extends Record<string, unknown>> {
private stateManager: StateManagerImpl<T> | null;
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
public constructor(localStorageKey: string, parent: any, defaults: T, logWarn: (log: string) => void){
if (isNonPersistent) {
function addListener(listener: (data: T) => void) {
chrome.storage.local.onChanged.addListener((changes) => {
if (localStorageKey in changes) {
listener(changes[localStorageKey].newValue);
}
});
}
this.stateManager = new StateManagerImpl(
localStorageKey,
parent,
defaults,
chrome.storage.local,
addListener,
logWarn,
);
}
}
public async saveState(): Promise<void> {
if (this.stateManager) {
return this.stateManager.saveState();
}
}
public async loadState(): Promise<void> {
if (this.stateManager) {
return this.stateManager.loadState();
}
}
}