diff options
Diffstat (limited to 'ext/js/language')
-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 |
4 files changed, 182 insertions, 182 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; } } |