Dictionary database worker refactoring (#1913)

* Rename DictionaryWorker => DictionaryWorkerHandler

* Rename var

* Rename file

* Simplify

* Rename DictionaryDatabaseModifier => DictionaryWorker

* Rename dictionary-database-modifier.js => dictionary-worker.js
This commit is contained in:
toasted-nutbread 2021-08-28 14:22:16 -04:00 committed by GitHub
parent 93f193c89e
commit 74709296e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 193 additions and 195 deletions

View File

@ -231,7 +231,7 @@
"ext/js/general/cache-map.js",
"ext/js/language/dictionary-database.js",
"ext/js/language/dictionary-importer.js",
"ext/js/language/dictionary-worker.js",
"ext/js/language/dictionary-worker-handler.js",
"ext/js/language/dictionary-worker-media-loader.js",
"ext/js/media/media-util.js"
],

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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}
});
};
_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 handler(params, onProgress);
response = {result};
const result = await this._dictionaryImporterMediaLoader.getImageResolution(mediaType, content);
response = {id, result};
} catch (e) {
response = {error: serializeError(e)};
response = {id, error: serializeError(e)};
}
self.postMessage({action: 'complete', params: response});
worker.postMessage({action: 'getImageResolution.response', 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;
_formatimportDictionaryResult(result) {
result.errors = result.errors.map((error) => deserializeError(error));
return result;
}
}

View File

@ -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');
}

View File

@ -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);

View File

@ -3483,8 +3483,8 @@
<script src="/js/general/object-property-accessor.js"></script>
<script src="/js/general/task-accumulator.js"></script>
<script src="/js/input/hotkey-util.js"></script>
<script src="/js/language/dictionary-database-modifier.js"></script>
<script src="/js/language/dictionary-importer-media-loader.js"></script>
<script src="/js/language/dictionary-worker.js"></script>
<script src="/js/language/sandbox/dictionary-data-util.js"></script>
<script src="/js/language/sandbox/japanese-util.js"></script>
<script src="/js/media/audio-system.js"></script>

View File

@ -405,8 +405,8 @@
<script src="/js/general/object-property-accessor.js"></script>
<script src="/js/general/task-accumulator.js"></script>
<script src="/js/input/hotkey-util.js"></script>
<script src="/js/language/dictionary-database-modifier.js"></script>
<script src="/js/language/dictionary-importer-media-loader.js"></script>
<script src="/js/language/dictionary-worker.js"></script>
<script src="/js/media/media-util.js"></script>
<script src="/js/pages/settings/dictionary-controller.js"></script>
<script src="/js/pages/settings/dictionary-import-controller.js"></script>

View File

@ -97,7 +97,7 @@ function testServiceWorker() {
function testWorkers() {
testWorker(
'js/language/dictionary-worker-main.js',
{DictionaryWorker: StubClass}
{DictionaryWorkerHandler: StubClass}
);
}