diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-12-09 21:07:18 -0500 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-12-13 23:11:56 -0500 |
commit | 6a1cfbaad62bc60e8415821039e028432d64b549 (patch) | |
tree | db0ab67790fe00a7c541e0afe03f7a0498336a3d /ext/bg/js/backend.js | |
parent | d6fe5c3e46bfba996e42f94b4303f1106a7fcdab (diff) |
Move apiOptionsSet implementation into Backend
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r-- | ext/bg/js/backend.js | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 6e3dfae6..74cd2ab4 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -194,8 +194,44 @@ class Backend { return this.getFullOptions(); } - _onApiOptionsSet({changedOptions, optionsContext, source}) { - return apiOptionsSet(changedOptions, optionsContext, source); + async _onApiOptionsSet({changedOptions, optionsContext, source}) { + const options = await this.getOptions(optionsContext); + + function getValuePaths(obj) { + const valuePaths = []; + const nodes = [{obj, path: []}]; + while (nodes.length > 0) { + const node = nodes.pop(); + for (const key of Object.keys(node.obj)) { + const path = node.path.concat(key); + const obj = node.obj[key]; + if (obj !== null && typeof obj === 'object') { + nodes.unshift({obj, path}); + } else { + valuePaths.push([obj, path]); + } + } + } + return valuePaths; + } + + function modifyOption(path, value, options) { + let pivot = options; + for (const key of path.slice(0, -1)) { + if (!hasOwn(pivot, key)) { + return false; + } + pivot = pivot[key]; + } + pivot[path[path.length - 1]] = value; + return true; + } + + for (const [value, path] of getValuePaths(changedOptions)) { + modifyOption(path, value, options); + } + + await this._optionsSave({source}); } _onApiOptionsSave({source}) { |