mecab refactoring and bugfix

This commit is contained in:
siikamiika 2019-11-11 20:54:23 +02:00
parent f97877a209
commit 1f2eee449e
3 changed files with 12 additions and 24 deletions

View File

@ -101,6 +101,8 @@ class Backend {
if (options.parsing.enableMecabParser) { if (options.parsing.enableMecabParser) {
this.mecab.startListener(); this.mecab.startListener();
} else {
this.mecab.stopListener();
} }
} }

View File

@ -34,15 +34,7 @@ class Mecab {
startListener() { startListener() {
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((message) => { this.port.onMessage.addListener(this.onNativeMessage.bind(this));
const {sequence, data} = message;
const {callback, timer} = this.listeners[sequence] || {};
if (timer) {
clearTimeout(timer);
delete this.listeners[sequence];
callback(data);
}
});
} }
stopListener() { stopListener() {
@ -53,6 +45,15 @@ class Mecab {
this.sequence = 0; this.sequence = 0;
} }
onNativeMessage({sequence, data}) {
if (this.listeners.hasOwnProperty(sequence)) {
const {callback, timer} = this.listeners[sequence];
clearTimeout(timer);
callback(data);
delete this.listeners[sequence];
}
}
invoke(action, params) { invoke(action, params) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const sequence = this.sequence++; const sequence = this.sequence++;

View File

@ -154,7 +154,6 @@ async function formWrite(options) {
function formSetupEventListeners() { function formSetupEventListeners() {
$('input, select, textarea').not('.anki-model').not('.ignore-form-changes *').change(utilAsync(onFormOptionsChanged)); $('input, select, textarea').not('.anki-model').not('.ignore-form-changes *').change(utilAsync(onFormOptionsChanged));
$('#parsing-mecab-enable').change(onParseMecabChanged);
$('.anki-model').change(utilAsync(onAnkiModelChanged)); $('.anki-model').change(utilAsync(onAnkiModelChanged));
} }
@ -441,20 +440,6 @@ function onMessage({action, params}, sender, callback) {
} }
/*
* Text parsing
*/
function onParseMecabChanged(e) {
const mecab = utilBackend().mecab;
if (e.target.checked) {
mecab.startListener();
} else {
mecab.stopListener();
}
}
/* /*
* Anki * Anki
*/ */