add mecab version check

This commit is contained in:
siikamiika 2019-11-23 19:18:29 +02:00
parent 2577d4054e
commit 7bf2c8048d

View File

@ -24,10 +24,23 @@ class Mecab {
this.sequence = 0; this.sequence = 0;
} }
async parseText(text) { onError(error) {
if (this.port === null) { logError(error, true);
return {}; }
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);
} }
}
async parseText(text) {
return await this.invoke('parse_text', {text}); return await this.invoke('parse_text', {text});
} }
@ -35,6 +48,7 @@ class Mecab {
if (this.port !== null) { return; } if (this.port !== null) { return; }
this.port = chrome.runtime.connectNative('yomichan_mecab'); this.port = chrome.runtime.connectNative('yomichan_mecab');
this.port.onMessage.addListener(this.onNativeMessage.bind(this)); this.port.onMessage.addListener(this.onNativeMessage.bind(this));
this.checkVersion();
} }
stopListener() { stopListener() {
@ -55,6 +69,9 @@ class Mecab {
} }
invoke(action, params) { invoke(action, params) {
if (this.port === null) {
return {};
}
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const sequence = this.sequence++; const sequence = this.sequence++;
@ -72,3 +89,4 @@ class Mecab {
} }
Mecab.timeout = 5000; Mecab.timeout = 5000;
Mecab.version = 1;