2019-11-03 03:08:57 +00:00
|
|
|
/*
|
2020-04-10 18:06:55 +00:00
|
|
|
* Copyright (C) 2019-2020 Yomichan Authors
|
2019-11-03 03:08:57 +00:00
|
|
|
*
|
|
|
|
* 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
|
2020-01-01 17:00:31 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-11-03 03:08:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
class Mecab {
|
|
|
|
constructor() {
|
2019-11-07 22:49:20 +00:00
|
|
|
this.port = null;
|
2020-01-25 03:02:53 +00:00
|
|
|
this.listeners = new Map();
|
2019-11-03 03:08:57 +00:00
|
|
|
this.sequence = 0;
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:18:29 +00:00
|
|
|
onError(error) {
|
2019-11-23 17:25:11 +00:00
|
|
|
logError(error, false);
|
2019-11-23 17:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async checkVersion() {
|
|
|
|
try {
|
|
|
|
const {version} = await this.invoke('get_version', {});
|
|
|
|
if (version !== Mecab.version) {
|
|
|
|
this.stopListener();
|
|
|
|
throw new Error(`Unsupported MeCab native messenger version ${version}. Yomichan supports version ${Mecab.version}.`);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.onError(error);
|
2019-11-07 22:49:20 +00:00
|
|
|
}
|
2019-11-23 17:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async parseText(text) {
|
2019-11-03 03:08:57 +00:00
|
|
|
return await this.invoke('parse_text', {text});
|
|
|
|
}
|
|
|
|
|
|
|
|
startListener() {
|
2019-11-07 22:49:20 +00:00
|
|
|
if (this.port !== null) { return; }
|
2019-11-03 23:25:32 +00:00
|
|
|
this.port = chrome.runtime.connectNative('yomichan_mecab');
|
2019-11-11 18:54:23 +00:00
|
|
|
this.port.onMessage.addListener(this.onNativeMessage.bind(this));
|
2019-11-23 17:18:29 +00:00
|
|
|
this.checkVersion();
|
2019-11-03 03:08:57 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 22:49:20 +00:00
|
|
|
stopListener() {
|
|
|
|
if (this.port === null) { return; }
|
|
|
|
this.port.disconnect();
|
|
|
|
this.port = null;
|
2020-01-25 03:02:53 +00:00
|
|
|
this.listeners.clear();
|
2019-11-07 22:49:20 +00:00
|
|
|
this.sequence = 0;
|
|
|
|
}
|
|
|
|
|
2019-11-11 18:54:23 +00:00
|
|
|
onNativeMessage({sequence, data}) {
|
2020-01-25 03:02:53 +00:00
|
|
|
const listener = this.listeners.get(sequence);
|
|
|
|
if (typeof listener === 'undefined') { return; }
|
|
|
|
|
|
|
|
const {callback, timer} = listener;
|
|
|
|
clearTimeout(timer);
|
|
|
|
callback(data);
|
|
|
|
this.listeners.delete(sequence);
|
2019-11-11 18:54:23 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 03:08:57 +00:00
|
|
|
invoke(action, params) {
|
2019-11-23 17:18:29 +00:00
|
|
|
if (this.port === null) {
|
2019-11-24 00:34:16 +00:00
|
|
|
return Promise.resolve({});
|
2019-11-23 17:18:29 +00:00
|
|
|
}
|
2019-11-03 03:08:57 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const sequence = this.sequence++;
|
|
|
|
|
2020-01-25 03:02:53 +00:00
|
|
|
this.listeners.set(sequence, {
|
2019-11-11 00:28:00 +00:00
|
|
|
callback: resolve,
|
2019-11-03 03:08:57 +00:00
|
|
|
timer: setTimeout(() => {
|
2020-01-25 03:02:53 +00:00
|
|
|
this.listeners.delete(sequence);
|
2019-11-11 00:28:00 +00:00
|
|
|
reject(new Error(`Mecab invoke timed out in ${Mecab.timeout} ms`));
|
2019-11-03 15:06:02 +00:00
|
|
|
}, Mecab.timeout)
|
2020-01-25 03:02:53 +00:00
|
|
|
});
|
2019-11-03 03:08:57 +00:00
|
|
|
|
|
|
|
this.port.postMessage({action, params, sequence});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:56:45 +00:00
|
|
|
Mecab.timeout = 5000;
|
2019-11-23 17:18:29 +00:00
|
|
|
Mecab.version = 1;
|