Pop-up dictionary browser extension for language learning. Successor to Yomichan. (PERSONAL FORK)
at lambda-fork/main 60 lines 2.1 kB view raw
1/* 2 * Copyright (C) 2024-2025 Yomitan Authors 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18import {ExtensionError} from '../core/extension-error.js'; 19import {log} from '../core/log.js'; 20import {DictionaryDatabase} from './dictionary-database.js'; 21 22export class DictionaryDatabaseWorkerHandler { 23 constructor() { 24 /** @type {DictionaryDatabase?} */ 25 this._dictionaryDatabase = null; 26 } 27 28 /** 29 * 30 */ 31 async prepare() { 32 this._dictionaryDatabase = new DictionaryDatabase(); 33 try { 34 await this._dictionaryDatabase.prepare(); 35 } catch (e) { 36 log.error(e); 37 } 38 self.addEventListener('message', this._onMessage.bind(this), false); 39 self.addEventListener('messageerror', (event) => { 40 const error = new ExtensionError('DictionaryDatabaseWorkerHandler: Error receiving message from main thread'); 41 error.data = event; 42 log.error(error); 43 }); 44 } 45 // Private 46 47 /** 48 * @param {MessageEvent<import('dictionary-database-worker-handler').MessageToWorker>} event 49 */ 50 _onMessage(event) { 51 const {action} = event.data; 52 switch (action) { 53 case 'connectToDatabaseWorker': 54 void this._dictionaryDatabase?.connectToDatabaseWorker(event.ports[0]); 55 break; 56 default: 57 log.error(`Unknown action: ${action}`); 58 } 59 } 60}