From 6a1cfbaad62bc60e8415821039e028432d64b549 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 9 Dec 2019 21:07:18 -0500 Subject: Move apiOptionsSet implementation into Backend --- ext/bg/js/api.js | 40 ++-------------------------------------- ext/bg/js/backend.js | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 95ac782e..4c55c6e3 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -21,44 +21,8 @@ function apiOptionsGet(optionsContext) { return utilBackend()._onApiOptionsGet({optionsContext}); } -async function apiOptionsSet(changedOptions, optionsContext, source) { - const options = await apiOptionsGet(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 apiOptionsSave(source); +function apiOptionsSet(changedOptions, optionsContext, source) { + return utilBackend()._onApiOptionsSet({changedOptions, optionsContext, source}); } function apiOptionsGetFull() { 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}) { -- cgit v1.2.3