yomichan/ext/bg/js/mecab.js

93 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-11-03 03:08:57 +00:00
/*
2020-01-01 17:00:00 +00:00
* Copyright (C) 2019-2020 Alex Yatskov <alex@foosoft.net>
2019-11-03 03:08:57 +00:00
* Author: Alex Yatskov <alex@foosoft.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
class Mecab {
constructor() {
this.port = null;
2019-11-03 03:08:57 +00:00
this.listeners = {};
this.sequence = 0;
}
2019-11-23 17:18:29 +00:00
onError(error) {
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-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() {
if (this.port !== null) { return; }
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
}
stopListener() {
if (this.port === null) { return; }
this.port.disconnect();
this.port = null;
this.listeners = {};
this.sequence = 0;
}
2019-11-11 18:54:23 +00:00
onNativeMessage({sequence, data}) {
if (hasOwn(this.listeners, sequence)) {
2019-11-11 18:54:23 +00:00
const {callback, timer} = this.listeners[sequence];
clearTimeout(timer);
callback(data);
delete this.listeners[sequence];
}
}
2019-11-03 03:08:57 +00:00
invoke(action, params) {
2019-11-23 17:18:29 +00:00
if (this.port === null) {
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++;
this.listeners[sequence] = {
2019-11-11 00:28:00 +00:00
callback: resolve,
2019-11-03 03:08:57 +00:00
timer: setTimeout(() => {
delete this.listeners[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)
2019-11-26 17:50:46 +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;