diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-08-28 14:22:16 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-28 14:22:16 -0400 |
commit | 74709296e557dfeab2e465f8bd53681934fe8040 (patch) | |
tree | 7b5603ad6da96697edc8934159a3d7b42d466a66 /ext/js | |
parent | 93f193c89eea381046f00f3a586beb66b2de0b19 (diff) |
Dictionary database worker refactoring (#1913)
* Rename DictionaryWorker => DictionaryWorkerHandler
* Rename var
* Rename file
* Simplify
* Rename DictionaryDatabaseModifier => DictionaryWorker
* Rename dictionary-database-modifier.js => dictionary-worker.js
Diffstat (limited to 'ext/js')
-rw-r--r-- | ext/js/language/dictionary-database-modifier.js | 130 | ||||
-rw-r--r-- | ext/js/language/dictionary-worker-handler.js | 95 | ||||
-rw-r--r-- | ext/js/language/dictionary-worker-main.js | 8 | ||||
-rw-r--r-- | ext/js/language/dictionary-worker.js | 131 | ||||
-rw-r--r-- | ext/js/pages/settings/dictionary-controller.js | 5 | ||||
-rw-r--r-- | ext/js/pages/settings/dictionary-import-controller.js | 5 |
6 files changed, 186 insertions, 188 deletions
diff --git a/ext/js/language/dictionary-database-modifier.js b/ext/js/language/dictionary-database-modifier.js deleted file mode 100644 index 055c4769..00000000 --- a/ext/js/language/dictionary-database-modifier.js +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Copyright (C) 2021 Yomichan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. - */ - -/* global - * DictionaryImporterMediaLoader - */ - -class DictionaryDatabaseModifier { - constructor() { - this._dictionaryImporterMediaLoader = new DictionaryImporterMediaLoader(); - } - - importDictionary(archiveContent, details, onProgress) { - return this._invoke( - 'importDictionary', - {details, archiveContent}, - [archiveContent], - onProgress, - this._formatimportDictionaryResult.bind(this) - ); - } - - deleteDictionary(dictionaryTitle, onProgress) { - return this._invoke('deleteDictionary', {dictionaryTitle}, [], onProgress); - } - - // Private - - _invoke(action, params, transfer, onProgress, formatResult) { - return new Promise((resolve, reject) => { - const worker = new Worker('/js/language/dictionary-worker-main.js', {}); - const details = { - complete: false, - worker, - resolve, - reject, - onMessage: null, - onProgress, - formatResult - }; - const onMessage = this._onMessage.bind(this, details); - details.onMessage = onMessage; - worker.addEventListener('message', onMessage); - worker.postMessage({action, params}, transfer); - }); - } - - _onMessage(details, e) { - if (details.complete) { return; } - const {action, params} = e.data; - switch (action) { - case 'complete': - { - const {worker, resolve, reject, onMessage, formatResult} = details; - details.complete = true; - details.worker = null; - details.resolve = null; - details.reject = null; - details.onMessage = null; - details.onProgress = null; - details.formatResult = null; - worker.removeEventListener('message', onMessage); - worker.terminate(); - this._onMessageComplete(params, resolve, reject, formatResult); - } - break; - case 'progress': - this._onMessageProgress(params, details.onProgress); - break; - case 'getImageResolution': - this._onMessageGetImageResolution(params, details.worker); - break; - } - } - - _onMessageComplete(params, resolve, reject, formatResult) { - const {error} = params; - if (typeof error !== 'undefined') { - reject(deserializeError(error)); - } else { - let {result} = params; - try { - if (typeof formatResult === 'function') { - result = formatResult(result); - } - } catch (e) { - reject(e); - return; - } - resolve(result); - } - } - - _onMessageProgress(params, onProgress) { - if (typeof onProgress !== 'function') { return; } - const {args} = params; - onProgress(...args); - } - - async _onMessageGetImageResolution(params, worker) { - const {id, mediaType, content} = params; - let response; - try { - const result = await this._dictionaryImporterMediaLoader.getImageResolution(mediaType, content); - response = {id, result}; - } catch (e) { - response = {id, error: serializeError(e)}; - } - worker.postMessage({action: 'getImageResolution.response', params: response}); - } - - _formatimportDictionaryResult(result) { - result.errors = result.errors.map((error) => deserializeError(error)); - return result; - } -} diff --git a/ext/js/language/dictionary-worker-handler.js b/ext/js/language/dictionary-worker-handler.js new file mode 100644 index 00000000..88150f50 --- /dev/null +++ b/ext/js/language/dictionary-worker-handler.js @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2021 Yomichan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +/* global + * DictionaryDatabase + * DictionaryImporter + * DictionaryWorkerMediaLoader + */ + +class DictionaryWorkerHandler { + constructor() { + this._mediaLoader = new DictionaryWorkerMediaLoader(); + } + + prepare() { + self.addEventListener('message', this._onMessage.bind(this), false); + } + + // Private + + _onMessage(e) { + const {action, params} = e.data; + switch (action) { + case 'importDictionary': + this._onMessageWithProgress(params, this._importDictionary.bind(this)); + break; + case 'deleteDictionary': + this._onMessageWithProgress(params, this._deleteDictionary.bind(this)); + break; + case 'getImageResolution.response': + this._mediaLoader.handleMessage(params); + break; + } + } + + async _onMessageWithProgress(params, handler) { + const onProgress = (...args) => { + self.postMessage({ + action: 'progress', + params: {args} + }); + }; + let response; + try { + const result = await handler(params, onProgress); + response = {result}; + } catch (e) { + response = {error: serializeError(e)}; + } + self.postMessage({action: 'complete', params: response}); + } + + async _importDictionary({details, archiveContent}, onProgress) { + const dictionaryDatabase = await this._getPreparedDictionaryDatabase(); + try { + const dictionaryImporter = new DictionaryImporter(this._mediaLoader, onProgress); + const {result, errors} = await dictionaryImporter.importDictionary(dictionaryDatabase, archiveContent, details); + return { + result, + errors: errors.map((error) => serializeError(error)) + }; + } finally { + dictionaryDatabase.close(); + } + } + + async _deleteDictionary({dictionaryTitle}, onProgress) { + const dictionaryDatabase = await this._getPreparedDictionaryDatabase(); + try { + return await dictionaryDatabase.deleteDictionary(dictionaryTitle, {rate: 1000}, onProgress); + } finally { + dictionaryDatabase.close(); + } + } + + async _getPreparedDictionaryDatabase() { + const dictionaryDatabase = new DictionaryDatabase(); + await dictionaryDatabase.prepare(); + return dictionaryDatabase; + } +} diff --git a/ext/js/language/dictionary-worker-main.js b/ext/js/language/dictionary-worker-main.js index e113b568..fbeaea1e 100644 --- a/ext/js/language/dictionary-worker-main.js +++ b/ext/js/language/dictionary-worker-main.js @@ -16,7 +16,7 @@ */ /* global - * DictionaryWorker + * DictionaryWorkerHandler */ self.importScripts( @@ -27,15 +27,15 @@ self.importScripts( '/js/general/cache-map.js', '/js/language/dictionary-database.js', '/js/language/dictionary-importer.js', - '/js/language/dictionary-worker.js', + '/js/language/dictionary-worker-handler.js', '/js/language/dictionary-worker-media-loader.js', '/js/media/media-util.js' ); (() => { try { - const dictionaryImporterWorker = new DictionaryWorker(); - dictionaryImporterWorker.prepare(); + const dictionaryWorkerHandler = new DictionaryWorkerHandler(); + dictionaryWorkerHandler.prepare(); } catch (e) { log.error(e); } diff --git a/ext/js/language/dictionary-worker.js b/ext/js/language/dictionary-worker.js index dc546657..4adfdaec 100644 --- a/ext/js/language/dictionary-worker.js +++ b/ext/js/language/dictionary-worker.js @@ -16,80 +16,115 @@ */ /* global - * DictionaryDatabase - * DictionaryImporter - * DictionaryWorkerMediaLoader + * DictionaryImporterMediaLoader */ class DictionaryWorker { constructor() { - this._mediaLoader = new DictionaryWorkerMediaLoader(); + this._dictionaryImporterMediaLoader = new DictionaryImporterMediaLoader(); } - prepare() { - self.addEventListener('message', this._onMessage.bind(this), false); + importDictionary(archiveContent, details, onProgress) { + return this._invoke( + 'importDictionary', + {details, archiveContent}, + [archiveContent], + onProgress, + this._formatimportDictionaryResult.bind(this) + ); + } + + deleteDictionary(dictionaryTitle, onProgress) { + return this._invoke('deleteDictionary', {dictionaryTitle}, [], onProgress); } // Private - _onMessage(e) { + _invoke(action, params, transfer, onProgress, formatResult) { + return new Promise((resolve, reject) => { + const worker = new Worker('/js/language/dictionary-worker-main.js', {}); + const details = { + complete: false, + worker, + resolve, + reject, + onMessage: null, + onProgress, + formatResult + }; + const onMessage = this._onMessage.bind(this, details); + details.onMessage = onMessage; + worker.addEventListener('message', onMessage); + worker.postMessage({action, params}, transfer); + }); + } + + _onMessage(details, e) { + if (details.complete) { return; } const {action, params} = e.data; switch (action) { - case 'importDictionary': - this._onMessageWithProgress(params, this._importDictionary.bind(this)); + case 'complete': + { + const {worker, resolve, reject, onMessage, formatResult} = details; + details.complete = true; + details.worker = null; + details.resolve = null; + details.reject = null; + details.onMessage = null; + details.onProgress = null; + details.formatResult = null; + worker.removeEventListener('message', onMessage); + worker.terminate(); + this._onMessageComplete(params, resolve, reject, formatResult); + } break; - case 'deleteDictionary': - this._onMessageWithProgress(params, this._deleteDictionary.bind(this)); + case 'progress': + this._onMessageProgress(params, details.onProgress); break; - case 'getImageResolution.response': - this._mediaLoader.handleMessage(params); + case 'getImageResolution': + this._onMessageGetImageResolution(params, details.worker); break; } } - async _onMessageWithProgress(params, handler) { - const onProgress = (...args) => { - self.postMessage({ - action: 'progress', - params: {args} - }); - }; - let response; - try { - const result = await handler(params, onProgress); - response = {result}; - } catch (e) { - response = {error: serializeError(e)}; + _onMessageComplete(params, resolve, reject, formatResult) { + const {error} = params; + if (typeof error !== 'undefined') { + reject(deserializeError(error)); + } else { + let {result} = params; + try { + if (typeof formatResult === 'function') { + result = formatResult(result); + } + } catch (e) { + reject(e); + return; + } + resolve(result); } - self.postMessage({action: 'complete', params: response}); } - async _importDictionary({details, archiveContent}, onProgress) { - const dictionaryDatabase = await this._getPreparedDictionaryDatabase(); - try { - const dictionaryImporter = new DictionaryImporter(this._mediaLoader, onProgress); - const {result, errors} = await dictionaryImporter.importDictionary(dictionaryDatabase, archiveContent, details); - return { - result, - errors: errors.map((error) => serializeError(error)) - }; - } finally { - dictionaryDatabase.close(); - } + _onMessageProgress(params, onProgress) { + if (typeof onProgress !== 'function') { return; } + const {args} = params; + onProgress(...args); } - async _deleteDictionary({dictionaryTitle}, onProgress) { - const dictionaryDatabase = await this._getPreparedDictionaryDatabase(); + async _onMessageGetImageResolution(params, worker) { + const {id, mediaType, content} = params; + let response; try { - return await dictionaryDatabase.deleteDictionary(dictionaryTitle, {rate: 1000}, onProgress); - } finally { - dictionaryDatabase.close(); + const result = await this._dictionaryImporterMediaLoader.getImageResolution(mediaType, content); + response = {id, result}; + } catch (e) { + response = {id, error: serializeError(e)}; } + worker.postMessage({action: 'getImageResolution.response', params: response}); } - async _getPreparedDictionaryDatabase() { - const dictionaryDatabase = new DictionaryDatabase(); - await dictionaryDatabase.prepare(); - return dictionaryDatabase; + _formatimportDictionaryResult(result) { + result.errors = result.errors.map((error) => deserializeError(error)); + return result; } } diff --git a/ext/js/pages/settings/dictionary-controller.js b/ext/js/pages/settings/dictionary-controller.js index a041e791..2a44f9ad 100644 --- a/ext/js/pages/settings/dictionary-controller.js +++ b/ext/js/pages/settings/dictionary-controller.js @@ -16,7 +16,7 @@ */ /* global - * DictionaryDatabaseModifier + * DictionaryWorker */ class DictionaryEntry { @@ -679,8 +679,7 @@ class DictionaryController { } async _deleteDictionaryInternal(dictionaryTitle, onProgress) { - const dictionaryDatabaseModifier = new DictionaryDatabaseModifier(); - await dictionaryDatabaseModifier.deleteDictionary(dictionaryTitle, onProgress); + await new DictionaryWorker().deleteDictionary(dictionaryTitle, onProgress); yomichan.api.triggerDatabaseUpdated('dictionary', 'delete'); } diff --git a/ext/js/pages/settings/dictionary-import-controller.js b/ext/js/pages/settings/dictionary-import-controller.js index 2716756d..b6700c78 100644 --- a/ext/js/pages/settings/dictionary-import-controller.js +++ b/ext/js/pages/settings/dictionary-import-controller.js @@ -17,7 +17,7 @@ /* global * DictionaryController - * DictionaryDatabaseModifier + * DictionaryWorker */ class DictionaryImportController { @@ -213,8 +213,7 @@ class DictionaryImportController { async _importDictionary(file, importDetails, onProgress) { const archiveContent = await this._readFile(file); - const dictionaryDatabaseModifier = new DictionaryDatabaseModifier(); - const {result, errors} = await dictionaryDatabaseModifier.importDictionary(archiveContent, importDetails, onProgress); + const {result, errors} = await new DictionaryWorker().importDictionary(archiveContent, importDetails, onProgress); yomichan.api.triggerDatabaseUpdated('dictionary', 'import'); const errors2 = await this._addDictionarySettings(result.sequenced, result.title); |