diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-05-06 21:34:06 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-05-06 21:34:06 -0700 |
commit | 7015b804699498999f68374a8c568665d4a24e3a (patch) | |
tree | 392f350bbc576a55d220866fb4a703a6d8fb101a /ext | |
parent | f7db707dc249cabc14d72728ad1eb0176390593a (diff) |
Async pooling
Diffstat (limited to 'ext')
-rw-r--r-- | ext/bg/js/yomichan.js | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 062e5e72..4a9fd35c 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -34,6 +34,7 @@ class Yomichan { }); this.translator = new Translator(); + this.asyncPools = {}; this.setState('disabled'); loadOptions((opts) => { @@ -50,7 +51,7 @@ class Yomichan { onMessage(request, sender, callback) { const {action, params} = request, handlers = { - canAddNotes: (definitions) => this.ankiInvoke('canAddNotes', definitions, callback), + canAddNotes: (definitions) => this.ankiInvoke('canAddNotes', definitions, 'notes', callback), findKanji: (text) => callback(this.translator.findKanji(text)), findTerm: (text) => callback(this.translator.findTerm(text)), getOptions: () => callback(this.options), @@ -101,10 +102,17 @@ class Yomichan { Yomichan.notifyChange('options', this.options); } - ankiInvoke(action, params, callback) { - if (this.options.enableAnkiConnect) { + ankiInvoke(action, params, pool, callback) { + if (pool !== null && this.asyncPools.hasOwnProperty(pool)) { + this.asyncPools[pool].abort(); + callback(null); + } else if (this.options.enableAnkiConnect) { const xhr = new XMLHttpRequest(); xhr.addEventListener('loadend', () => { + if (pool !== null) { + delete this.asyncPools[pool]; + } + const resp = xhr.responseText; callback(resp ? JSON.parse(resp) : null); }); |