Use promiseTimeout

This commit is contained in:
toasted-nutbread 2019-10-24 19:38:29 -04:00
parent d608657495
commit 185963899b

View File

@ -20,7 +20,7 @@
class Frontend { class Frontend {
constructor(popup, ignoreNodes) { constructor(popup, ignoreNodes) {
this.popup = popup; this.popup = popup;
this.popupTimer = null; this.popupTimerPromise = null;
this.textSourceLast = null; this.textSourceLast = null;
this.pendingLookup = false; this.pendingLookup = false;
this.options = null; this.options = null;
@ -74,7 +74,7 @@ class Frontend {
} }
onMouseOver(e) { onMouseOver(e) {
if (e.target === this.popup.container && this.popupTimer !== null) { if (e.target === this.popup.container) {
this.popupTimerClear(); this.popupTimerClear();
} }
} }
@ -99,14 +99,17 @@ class Frontend {
} }
const search = async () => { const search = async () => {
if (scanningModifier === 'none') {
if (!await this.popupTimerWait()) {
// Aborted
return;
}
}
await this.searchAt(e.clientX, e.clientY, 'mouse'); await this.searchAt(e.clientX, e.clientY, 'mouse');
}; };
if (scanningModifier === 'none') { search();
this.popupTimerSet(search);
} else {
search();
}
} }
onMouseDown(e) { onMouseDown(e) {
@ -293,19 +296,19 @@ class Frontend {
await this.popup.setOptions(this.options); await this.popup.setOptions(this.options);
} }
popupTimerSet(callback) { async popupTimerWait() {
const delay = this.options.scanning.delay; const delay = this.options.scanning.delay;
if (delay > 0) { this.popupTimerPromise = promiseTimeout(delay, true);
this.popupTimer = window.setTimeout(callback, delay); try {
} else { return await this.popupTimerPromise;
Promise.resolve().then(callback); } finally {
this.popupTimerPromise = null;
} }
} }
popupTimerClear() { popupTimerClear() {
if (this.popupTimer !== null) { if (this.popupTimerPromise !== null) {
window.clearTimeout(this.popupTimer); this.popupTimerPromise.resolve(false);
this.popupTimer = null;
} }
} }