aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-05-24 13:50:34 -0400
committerGitHub <noreply@github.com>2020-05-24 13:50:34 -0400
commit66e1185686f98f1cc4493298b5b1d4e0be7d826a (patch)
tree7d67a8bbbfd3d32235dcc5fbfe94c34e61132f3e /ext/bg/js/backend.js
parentc800444a0d4aa0177242da51e0f9716ebe882587 (diff)
Settings binder (#542)
* Fix _modifySetting being async * Return values for modifySettings's set and splice actions * Add apiGetSettings * Create a class which can accumulate tasks to run in bulk * Create a class which binds input elements to customizable sources * Create class which binds input elements to settings * Add support for value transforms * Remove redundant ObjectPropertyAccessor.getPathArray * Fix not using correct types for input.min/max/step * Fix wrong condition * Use api object
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r--ext/bg/js/backend.js90
1 files changed, 56 insertions, 34 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 8df4fd9d..90895737 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -119,7 +119,8 @@ class Backend {
['log', {async: false, contentScript: true, handler: this._onApiLog.bind(this)}],
['logIndicatorClear', {async: false, contentScript: true, handler: this._onApiLogIndicatorClear.bind(this)}],
['createActionPort', {async: false, contentScript: true, handler: this._onApiCreateActionPort.bind(this)}],
- ['modifySettings', {async: true, contentScript: true, handler: this._onApiModifySettings.bind(this)}]
+ ['modifySettings', {async: true, contentScript: true, handler: this._onApiModifySettings.bind(this)}],
+ ['getSettings', {async: false, contentScript: true, handler: this._onApiGetSettings.bind(this)}]
]);
this._messageHandlersWithProgress = new Map([
['importDictionaryArchive', {async: true, contentScript: false, handler: this._onApiImportDictionaryArchive.bind(this)}],
@@ -831,8 +832,8 @@ class Backend {
const results = [];
for (const target of targets) {
try {
- this._modifySetting(target);
- results.push({result: true});
+ const result = this._modifySetting(target);
+ results.push({result: utilIsolate(result)});
} catch (e) {
results.push({error: errorToJson(e)});
}
@@ -841,6 +842,19 @@ class Backend {
return results;
}
+ _onApiGetSettings({targets}) {
+ const results = [];
+ for (const target of targets) {
+ try {
+ const result = this._getSetting(target);
+ results.push({result: utilIsolate(result)});
+ } catch (e) {
+ results.push({error: errorToJson(e)});
+ }
+ }
+ return results;
+ }
+
// Command handlers
_createActionListenerPort(port, sender, handlers) {
@@ -1017,45 +1031,53 @@ class Backend {
}
}
- async _modifySetting(target) {
+ _getSetting(target) {
+ const options = this._getModifySettingObject(target);
+ const accessor = new ObjectPropertyAccessor(options);
+ const {path} = target;
+ if (typeof path !== 'string') { throw new Error('Invalid path'); }
+ return accessor.get(ObjectPropertyAccessor.getPathArray(path));
+ }
+
+ _modifySetting(target) {
const options = this._getModifySettingObject(target);
const accessor = new ObjectPropertyAccessor(options);
const action = target.action;
switch (action) {
case 'set':
- {
- const {path, value} = target;
- if (typeof path !== 'string') { throw new Error('Invalid path'); }
- accessor.set(ObjectPropertyAccessor.getPathArray(path), value);
- }
- break;
+ {
+ const {path, value} = target;
+ if (typeof path !== 'string') { throw new Error('Invalid path'); }
+ const pathArray = ObjectPropertyAccessor.getPathArray(path);
+ accessor.set(pathArray, value);
+ return accessor.get(pathArray);
+ }
case 'delete':
- {
- const {path} = target;
- if (typeof path !== 'string') { throw new Error('Invalid path'); }
- accessor.delete(ObjectPropertyAccessor.getPathArray(path));
- }
- break;
+ {
+ const {path} = target;
+ if (typeof path !== 'string') { throw new Error('Invalid path'); }
+ accessor.delete(ObjectPropertyAccessor.getPathArray(path));
+ return true;
+ }
case 'swap':
- {
- const {path1, path2} = target;
- if (typeof path1 !== 'string') { throw new Error('Invalid path1'); }
- if (typeof path2 !== 'string') { throw new Error('Invalid path2'); }
- accessor.swap(ObjectPropertyAccessor.getPathArray(path1), ObjectPropertyAccessor.getPathArray(path2));
- }
- break;
+ {
+ const {path1, path2} = target;
+ if (typeof path1 !== 'string') { throw new Error('Invalid path1'); }
+ if (typeof path2 !== 'string') { throw new Error('Invalid path2'); }
+ accessor.swap(ObjectPropertyAccessor.getPathArray(path1), ObjectPropertyAccessor.getPathArray(path2));
+ return true;
+ }
case 'splice':
- {
- const {path, start, deleteCount, items} = target;
- if (typeof path !== 'string') { throw new Error('Invalid path'); }
- if (typeof start !== 'number' || Math.floor(start) !== start) { throw new Error('Invalid start'); }
- if (typeof deleteCount !== 'number' || Math.floor(deleteCount) !== deleteCount) { throw new Error('Invalid deleteCount'); }
- if (!Array.isArray(items)) { throw new Error('Invalid items'); }
- const array = accessor.get(ObjectPropertyAccessor.getPathArray(path));
- if (!Array.isArray(array)) { throw new Error('Invalid target type'); }
- array.splice(start, deleteCount, ...items);
- }
- break;
+ {
+ const {path, start, deleteCount, items} = target;
+ if (typeof path !== 'string') { throw new Error('Invalid path'); }
+ if (typeof start !== 'number' || Math.floor(start) !== start) { throw new Error('Invalid start'); }
+ if (typeof deleteCount !== 'number' || Math.floor(deleteCount) !== deleteCount) { throw new Error('Invalid deleteCount'); }
+ if (!Array.isArray(items)) { throw new Error('Invalid items'); }
+ const array = accessor.get(ObjectPropertyAccessor.getPathArray(path));
+ if (!Array.isArray(array)) { throw new Error('Invalid target type'); }
+ return array.splice(start, deleteCount, ...items);
+ }
default:
throw new Error(`Unknown action: ${action}`);
}