From 185963899b4176b31a14ab141f1335c17a2de9c4 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 19:38:29 -0400 Subject: Use promiseTimeout --- ext/fg/js/frontend.js | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'ext') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index c8a7d254..897c7b73 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -20,7 +20,7 @@ class Frontend { constructor(popup, ignoreNodes) { this.popup = popup; - this.popupTimer = null; + this.popupTimerPromise = null; this.textSourceLast = null; this.pendingLookup = false; this.options = null; @@ -74,7 +74,7 @@ class Frontend { } onMouseOver(e) { - if (e.target === this.popup.container && this.popupTimer !== null) { + if (e.target === this.popup.container) { this.popupTimerClear(); } } @@ -99,14 +99,17 @@ class Frontend { } const search = async () => { + if (scanningModifier === 'none') { + if (!await this.popupTimerWait()) { + // Aborted + return; + } + } + await this.searchAt(e.clientX, e.clientY, 'mouse'); }; - if (scanningModifier === 'none') { - this.popupTimerSet(search); - } else { - search(); - } + search(); } onMouseDown(e) { @@ -293,19 +296,19 @@ class Frontend { await this.popup.setOptions(this.options); } - popupTimerSet(callback) { + async popupTimerWait() { const delay = this.options.scanning.delay; - if (delay > 0) { - this.popupTimer = window.setTimeout(callback, delay); - } else { - Promise.resolve().then(callback); + this.popupTimerPromise = promiseTimeout(delay, true); + try { + return await this.popupTimerPromise; + } finally { + this.popupTimerPromise = null; } } popupTimerClear() { - if (this.popupTimer !== null) { - window.clearTimeout(this.popupTimer); - this.popupTimer = null; + if (this.popupTimerPromise !== null) { + this.popupTimerPromise.resolve(false); } } -- cgit v1.2.3