-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreferences.ts
84 lines (79 loc) · 2.47 KB
/
preferences.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { store } from "./store";
/**
* Returns the value of the specified preference for the specified plugin.
*
* @param pluginName The name of the plugin.
* @param preferenceKey The key of the preference.
* @returns A promise that resolves to the value of the preference.
*/
function get(pluginName: string, preferenceKey: string): Promise<any> {
return store
.createCollection("preferences", {})
.then(() => store.findOne("preferences", `${pluginName}.${preferenceKey}`))
.then((doc) => doc?.value ?? "");
}
/**
* Sets the value of the specified preference for the specified plugin.
*
* @param pluginName The name of the plugin.
* @param preferenceKey The key of the preference.
* @param value The value of the preference.
* @returns A promise that resolves when the preference has been set.
*/
function set(pluginName: string, preferenceKey: string, value: any): Promise<any> {
return store
.createCollection("preferences", {})
.then(() =>
store
.findOne("preferences", `${pluginName}.${preferenceKey}`)
.then((doc) =>
doc
? store.updateOne("preferences", `${pluginName}.${preferenceKey}`, { value })
: store.insertOne("preferences", { _id: `${pluginName}.${preferenceKey}`, value })
)
);
}
/**
* Clears all preferences for the specified plugin.
*
* @param pluginName The name of the plugin.
* @returns A promise that resolves when the preferences have been cleared.
*/
function clear(pluginName: string): Promise<void> {
return Promise.resolve();
}
/**
* Registers a preference with the specified default value.
*
* @param register The function to use for registering the preference.
* @param pluginName The name of the plugin.
* @param preferenceKey The key of the preference.
* @param preferenceName The name of the preference.
* @param preferenceDescription The description of the preference.
* @param defaultValue The default value of the preference.
*/
function registerPreferences<T>(
register: Function,
pluginName: string,
preferenceKey: string,
preferenceName: string,
preferenceDescription: string,
defaultValue: T
) {
register("PluginPreferences", `${pluginName}.${preferenceKey}`, () => ({
pluginName,
preferenceKey,
preferenceName,
preferenceDescription,
defaultValue,
}));
}
/**
* An object that provides methods for getting, setting, and clearing preferences.
*/
export const preferences = {
get,
set,
clear,
registerPreferences,
};