From 7015b804699498999f68374a8c568665d4a24e3a Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 6 May 2016 21:34:06 -0700 Subject: Async pooling --- ext/bg/js/yomichan.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'ext') 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); }); -- cgit v1.2.3