perf(settings): Cache proxies

This commit is contained in:
Vendicated 2022-11-28 15:44:53 +01:00
parent a0a1a4d139
commit 3b4879f9d9
No known key found for this signature in database
GPG key ID: EC781ADFB93EFFA3

View file

@ -48,16 +48,18 @@ try {
var settings = JSON.parse(VencordNative.ipc.sendSync(IpcEvents.GET_SETTINGS)) as Settings; var settings = JSON.parse(VencordNative.ipc.sendSync(IpcEvents.GET_SETTINGS)) as Settings;
mergeDefaults(settings, DefaultSettings); mergeDefaults(settings, DefaultSettings);
} catch (err) { } catch (err) {
console.error("Corrupt settings file. ", err);
var settings = mergeDefaults({} as Settings, DefaultSettings); var settings = mergeDefaults({} as Settings, DefaultSettings);
logger.error("An error occurred while loading the settings. Corrupt settings file?\n", err);
} }
type SubscriptionCallback = ((newValue: any, path: string) => void) & { _path?: string; }; type SubscriptionCallback = ((newValue: any, path: string) => void) & { _path?: string; };
const subscriptions = new Set<SubscriptionCallback>(); const subscriptions = new Set<SubscriptionCallback>();
const proxyCache = {} as Record<string, any>;
// Wraps the passed settings object in a Proxy to nicely handle change listeners and default values // Wraps the passed settings object in a Proxy to nicely handle change listeners and default values
function makeProxy(settings: any, root = settings, path = ""): Settings { function makeProxy(settings: any, root = settings, path = ""): Settings {
return new Proxy(settings, { return proxyCache[path] ??= new Proxy(settings, {
get(target, p: string) { get(target, p: string) {
const v = target[p]; const v = target[p];
@ -67,7 +69,7 @@ function makeProxy(settings: any, root = settings, path = ""): Settings {
if (path === "plugins" && p in plugins) if (path === "plugins" && p in plugins)
return target[p] = makeProxy({ return target[p] = makeProxy({
enabled: plugins[p].required ?? false enabled: plugins[p].required ?? false
}, root, `plugins/${p}`); }, root, `plugins.${p}`);
// Since the property is not set, check if this is a plugin's setting and if so, try to resolve // Since the property is not set, check if this is a plugin's setting and if so, try to resolve
// the default value. // the default value.