summaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-05-06 19:28:26 -0400
committerGitHub <noreply@github.com>2020-05-06 19:28:26 -0400
commit021ccb5ac3038f63d07ccc9575ee56480031a251 (patch)
tree2d83181aed20494c5c73943dc878f06bb89bb211 /ext/bg/js/backend.js
parent501281e887fb66b490f90e7593639112b058ab97 (diff)
Move util database modification functions (#499)
* Update onProgress callback to handle multiple arguments * Add apiImportDictionaryArchive * Add apiDeleteDictionary * Make onProgress the last argument for consistency * Remove deprecated util functions * Fix issue with missing progress args * Remove function calls which modify the database from Translator * Update tests * Fix errors not being serialized correctly in _createActionListenerPort
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r--ext/bg/js/backend.js23
1 files changed, 19 insertions, 4 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index c5173a2e..d454aa22 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -117,7 +117,10 @@ class Backend {
['logIndicatorClear', {handler: this._onApiLogIndicatorClear.bind(this), async: false}],
['createActionPort', {handler: this._onApiCreateActionPort.bind(this), async: false}]
]);
- this._messageHandlersWithProgress = new Map();
+ this._messageHandlersWithProgress = new Map([
+ ['importDictionaryArchive', {handler: this._onApiImportDictionaryArchive.bind(this), async: true}],
+ ['deleteDictionary', {handler: this._onApiDeleteDictionary.bind(this), async: true}]
+ ]);
this._commandHandlers = new Map([
['search', this._onCommandSearch.bind(this)],
@@ -771,7 +774,8 @@ class Backend {
async _onApiPurgeDatabase(params, sender) {
this._validatePrivilegedMessageSender(sender);
- return await this.translator.purgeDatabase();
+ this.translator.clearDatabaseCaches();
+ await this.database.purge();
}
async _onApiGetMedia({targets}) {
@@ -814,12 +818,23 @@ class Backend {
return portName;
}
+ async _onApiImportDictionaryArchive({archiveContent, details}, sender, onProgress) {
+ this._validatePrivilegedMessageSender(sender);
+ return await this.dictionaryImporter.import(this.database, archiveContent, details, onProgress);
+ }
+
+ async _onApiDeleteDictionary({dictionaryName}, sender, onProgress) {
+ this._validatePrivilegedMessageSender(sender);
+ this.translator.clearDatabaseCaches();
+ await this.database.deleteDictionary(dictionaryName, {rate: 1000}, onProgress);
+ }
+
// Command handlers
_createActionListenerPort(port, sender, handlers) {
let hasStarted = false;
- const onProgress = (data) => {
+ const onProgress = (...data) => {
try {
if (port === null) { return; }
port.postMessage({type: 'progress', data});
@@ -847,7 +862,7 @@ class Backend {
port.postMessage({type: 'complete', data: result});
} catch (e) {
if (port !== null) {
- port.postMessage({type: 'error', data: e});
+ port.postMessage({type: 'error', data: errorToJson(e)});
}
cleanup();
}