diff options
author | siikamiika <siikamiika@users.noreply.github.com> | 2019-11-11 20:54:23 +0200 |
---|---|---|
committer | siikamiika <siikamiika@users.noreply.github.com> | 2019-11-23 17:50:46 +0200 |
commit | 1f2eee449e2c1e0baf20dba038da7eaf3424aefe (patch) | |
tree | 68a13baccf2d958daf3966f586702f44bd3c9f41 /ext/bg/js/mecab.js | |
parent | f97877a2097c8b27b75099e489b93db255cb68be (diff) |
mecab refactoring and bugfix
Diffstat (limited to 'ext/bg/js/mecab.js')
-rw-r--r-- | ext/bg/js/mecab.js | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/ext/bg/js/mecab.js b/ext/bg/js/mecab.js index b9f2d0b3..ada96945 100644 --- a/ext/bg/js/mecab.js +++ b/ext/bg/js/mecab.js @@ -34,15 +34,7 @@ class Mecab { startListener() { if (this.port !== null) { return; } this.port = chrome.runtime.connectNative('yomichan_mecab'); - this.port.onMessage.addListener((message) => { - const {sequence, data} = message; - const {callback, timer} = this.listeners[sequence] || {}; - if (timer) { - clearTimeout(timer); - delete this.listeners[sequence]; - callback(data); - } - }); + this.port.onMessage.addListener(this.onNativeMessage.bind(this)); } stopListener() { @@ -53,6 +45,15 @@ class Mecab { 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) { return new Promise((resolve, reject) => { const sequence = this.sequence++; |