-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsettings-utils.js
96 lines (85 loc) · 2.99 KB
/
settings-utils.js
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
85
86
87
88
89
90
91
92
93
94
95
96
import { extension_settings } from "../../../extensions.js";
import { ByRef } from "./ByRef.js";
/** @template TSettings */
export class STExtensionSettingsWrapper {
#settings;
/**
*
* @param {TSettings} settings
*/
constructor(settings) {
if (settings === null || settings === undefined) {
throw new Error("Settings object is null or undefined; pass in a valid settings object.");
}
this.#settings = settings;
}
get settings() {
return this.#settings;
}
/**
* Gets the value of the specified key.
*
* @param {keyof TSettings} key
* @returns {TSettings[keyof TSettings]}
*/
get(key) {
return this.#settings[key];
}
/**
* Sets a setting to the specified value.
*
* @param {keyof TSettings} key
* @param {TSettings[keyof TSettings]} value
*/
set(key, value) {
this.#settings[key] = value;
}
/**
* Deletes the specified setting.
*
* @param {keyof TSettings} key
*/
delete(key) {
delete this.#settings[key];
}
/**
* Create a new settings wrapper by using a {@linkcode ByRef} to initialize the wrapped settings object.
*
* @template TSettings
* @param {ByRef<TSettings>} refSettings A reference passed-by-reference to a settings object.
* @param {TSettings=} defaultSettings A default settings object to initialize the passed settings with, if necessary.
*/
static safeWrap(refSettings, defaultSettings) {
if (!(refSettings instanceof ByRef)) {
throw new TypeError("STExtensionSettingsWrapper.safeWrap() requires a ByRef.");
}
const savedSettings = refSettings.ref;
refSettings.ref = Object.assign({}, defaultSettings ?? {}, savedSettings);
return new this(refSettings.ref);
}
/**
* Wraps the extension settings object for the specified extension.
*
* @template TSettings
* @param {string} extensionName The name of the extension to wrap the settings of.
* @param {TSettings=} defaultSettings A default settings object to initialize the extension's settings with, if necessary.
*/
static wrapSettingsForExtension(extensionName, defaultSettings) {
/** @type {extension_settings[extensionName]} */
const byRefSettings = ByRef.createKeyAccessor(extension_settings, extensionName);
return this.safeWrap(byRefSettings, defaultSettings);
}
}
/**
* Ensures the settings dictionary for the specified extension is initialized and then returns it.
*
* @template TSettings
* @param {string} extensionName
* @param {TSettings=} defaultSettings
* @returns {TSettings}
*/
export function initializeSettings(extensionName, defaultSettings) {
const savedSettings = extension_settings[extensionName];
extension_settings[extensionName] = Object.assign({}, defaultSettings ? structuredClone(defaultSettings) : {}, savedSettings);
return extension_settings[extensionName];
}