diff options
author | Alex Yatskov <alex@foosoft.net> | 2020-01-26 11:29:30 -0800 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2020-01-26 11:29:30 -0800 |
commit | 0c5b9b1fa1599cbf769d96cdebc226310f9dd8bc (patch) | |
tree | e734e2c3005078dbc248b541d357a934baa8a116 /ext/bg/js/mecab.js | |
parent | 2a12036ca305044291f1f4105d6a8d249848b210 (diff) | |
parent | 0cf1cf3aa094585bd6db8db2c1f229ba0ea37b6e (diff) |
Merge branch 'master' into testing
Diffstat (limited to 'ext/bg/js/mecab.js')
-rw-r--r-- | ext/bg/js/mecab.js | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/ext/bg/js/mecab.js b/ext/bg/js/mecab.js index 8bcbb91c..34ecd728 100644 --- a/ext/bg/js/mecab.js +++ b/ext/bg/js/mecab.js @@ -20,7 +20,7 @@ class Mecab { constructor() { this.port = null; - this.listeners = {}; + this.listeners = new Map(); this.sequence = 0; } @@ -55,17 +55,18 @@ class Mecab { if (this.port === null) { return; } this.port.disconnect(); this.port = null; - this.listeners = {}; + this.listeners.clear(); this.sequence = 0; } onNativeMessage({sequence, data}) { - if (hasOwn(this.listeners, sequence)) { - const {callback, timer} = this.listeners[sequence]; - clearTimeout(timer); - callback(data); - delete this.listeners[sequence]; - } + const listener = this.listeners.get(sequence); + if (typeof listener === 'undefined') { return; } + + const {callback, timer} = listener; + clearTimeout(timer); + callback(data); + this.listeners.delete(sequence); } invoke(action, params) { @@ -75,13 +76,13 @@ class Mecab { return new Promise((resolve, reject) => { const sequence = this.sequence++; - this.listeners[sequence] = { + this.listeners.set(sequence, { callback: resolve, timer: setTimeout(() => { - delete this.listeners[sequence]; + this.listeners.delete(sequence); reject(new Error(`Mecab invoke timed out in ${Mecab.timeout} ms`)); }, Mecab.timeout) - }; + }); this.port.postMessage({action, params, sequence}); }); |