forked from Sv443/Userscript.ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGMStorageEngine.ts
More file actions
48 lines (41 loc) · 1.69 KB
/
Copy pathGMStorageEngine.ts
File metadata and controls
48 lines (41 loc) · 1.69 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
47
48
import { DataStoreEngine, type DataStoreData, type DataStoreEngineDSOptions, type SerializableVal } from "@sv443-network/coreutils";
/** Options for the {@linkcode GMStorageEngine} class */
export type GMStorageEngineOptions = {
/**
* Specifies the necessary options for storing data.
* - ⚠️ Only specify this if you are using this instance standalone! The parent `DataStore` will set this automatically.
*/
dataStoreOptions?: DataStoreEngineDSOptions<DataStoreData>;
};
/**
* Storage engine for the `DataStore` class that uses the GM (GreaseMonkey) storage API.
*
* - ⚠️ Don't reuse engine instances, always create a new one for each {@linkcode DataStore} instance
*/
export class GMStorageEngine<TData extends DataStoreData> extends DataStoreEngine<TData> {
protected options: GMStorageEngineOptions;
/**
* Creates an instance of `GMStorageEngine`.
*
* - ⚠️ Don't reuse engine instances, always create a new one for each {@linkcode DataStore} instance
*/
constructor(options?: GMStorageEngineOptions) {
super(options?.dataStoreOptions);
this.options = {
...options,
};
}
//#region storage api
/** Fetches a value from persistent storage */
public async getValue<TValue extends SerializableVal = string>(name: string, defaultValue: TValue): Promise<string | TValue> {
return GM.getValue(name, defaultValue);
}
/** Sets a value in persistent storage */
public async setValue(name: string, value: SerializableVal): Promise<void> {
await GM.setValue(name, value as GM.Value);
}
/** Deletes a value from persistent storage */
public async deleteValue(name: string): Promise<void> {
await GM.deleteValue(name);
}
}