From 244e8227e7197e5330f83e5a4aaa6de3fac602b4 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Sat, 25 Jan 2025 21:17:20 +0100 Subject: [PATCH] Settings: make setProp() functional --- src/ext/lib/Settings.ts | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/ext/lib/Settings.ts b/src/ext/lib/Settings.ts index c212ca8..62be451 100644 --- a/src/ext/lib/Settings.ts +++ b/src/ext/lib/Settings.ts @@ -323,8 +323,38 @@ class Settings { this.active = activeSettings; } - async setProp(prop, value) { - this.active[prop] = value; + /** + * Sets value of a prop at given path. + * @param propPath path to property we want to set. If prop path does not exist, + * this function will recursively create it. It is assumed that uninitialized properties + * are objects. + * @param value + */ + async setProp(propPath: string | string[], value: any, options?: {forceReload?: boolean}, currentPath?: any) { + if (!Array.isArray(propPath)) { + propPath = propPath.split('.'); + } + + if (!currentPath) { + currentPath = this.active; + } + + const currentProp = propPath.shift(); + + if (propPath.length) { + if (!currentPath[currentProp]) { + currentPath[currentProp] = {}; + } + this.setProp(propPath, value, options, currentPath[currentProp]); + } else { + currentPath[currentProp] = value; + + if (options?.forceReload) { + this.save(); + } else { + this.saveWithoutReload(); + } + } } async save(options?) {