From d88635cbb28862e0efd79080e5c67865d0410238 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 22 Mar 2020 04:55:16 +0200 Subject: temporary listener abstraction --- ext/mixed/js/core.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'ext/mixed/js') diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index fd762e97..f21bac23 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -312,6 +312,42 @@ const yomichan = (() => { this.trigger('orphaned', {error}); } + getTemporaryListenerResult(eventHandler, userCallback, timeout=30000) { + let resolved = false; + let resolve; + let reject; + const listenerPromise = new Promise((_resolve, _reject) => { + resolve = _resolve; + reject = _reject; + }); + + if (eventHandler === chrome.runtime.onMessage) { + const runtimeMessageCallback = ({action, params}, sender, sendResponse) => { + const cleanupResolve = (value) => { + resolved = true; + eventHandler.removeListener(runtimeMessageCallback); + sendResponse(); + resolve(value); + }; + + setTimeout(() => { + if (!resolved) { + reject(new Error(`Listener timed out in ${timeout} ms`)); + eventHandler.removeListener(runtimeMessageCallback); + } + }, timeout); + + userCallback({action, params}, {resolve: cleanupResolve, sender}); + }; + + eventHandler.addListener(runtimeMessageCallback); + } else { + throw new Error('Event handler type not supported'); + } + + return listenerPromise; + } + // Private _onMessage({action, params}, sender, callback) { -- cgit v1.2.3 From fa65ab351293fa6af882479e0fad6aaca3152569 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 22 Mar 2020 12:45:58 +0200 Subject: refactor getTemporaryListenerResult timeout --- ext/fg/js/frame-offset-forwarder.js | 3 ++- ext/mixed/js/core.js | 23 +++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) (limited to 'ext/mixed/js') diff --git a/ext/fg/js/frame-offset-forwarder.js b/ext/fg/js/frame-offset-forwarder.js index 781a9018..38fc0b03 100644 --- a/ext/fg/js/frame-offset-forwarder.js +++ b/ext/fg/js/frame-offset-forwarder.js @@ -50,7 +50,8 @@ class FrameOffsetForwarder { if (action === 'frameOffset' && isObject(params) && params.uniqueId === uniqueId) { resolve(params); } - } + }, + 5000 ); window.parent.postMessage({ diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index f21bac23..616f48b8 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -312,8 +312,7 @@ const yomichan = (() => { this.trigger('orphaned', {error}); } - getTemporaryListenerResult(eventHandler, userCallback, timeout=30000) { - let resolved = false; + getTemporaryListenerResult(eventHandler, userCallback, timeout=null) { let resolve; let reject; const listenerPromise = new Promise((_resolve, _reject) => { @@ -323,20 +322,24 @@ const yomichan = (() => { if (eventHandler === chrome.runtime.onMessage) { const runtimeMessageCallback = ({action, params}, sender, sendResponse) => { + let timeoutId = null; + if (timeout !== null) { + timeoutId = window.setTimeout(() => { + timeoutId = null; + reject(new Error(`Listener timed out in ${timeout} ms`)); + eventHandler.removeListener(runtimeMessageCallback); + }, timeout); + } + const cleanupResolve = (value) => { - resolved = true; + if (timeoutId !== null) { + window.clearTimeout(timeoutId); + } eventHandler.removeListener(runtimeMessageCallback); sendResponse(); resolve(value); }; - setTimeout(() => { - if (!resolved) { - reject(new Error(`Listener timed out in ${timeout} ms`)); - eventHandler.removeListener(runtimeMessageCallback); - } - }, timeout); - userCallback({action, params}, {resolve: cleanupResolve, sender}); }; -- cgit v1.2.3 From c171503d77bbae21ddfa2eb49c6a571440f41763 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Wed, 25 Mar 2020 04:17:17 +0200 Subject: use getTemporaryListenerResult in other places --- ext/fg/js/popup.js | 64 ++++++++++++++++++++++++---------------------------- ext/mixed/js/core.js | 15 ++++++------ 2 files changed, 38 insertions(+), 41 deletions(-) (limited to 'ext/mixed/js') diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 47e32963..fb3e56b4 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -203,27 +203,38 @@ class Popup { this._messageToken = await apiGetMessageToken(); } - return new Promise((resolve) => { - const parentFrameId = (typeof this._frameId === 'number' ? this._frameId : null); - this._container.setAttribute('src', chrome.runtime.getURL('/fg/float.html')); - this._container.addEventListener('load', () => { - this._listenForDisplayPrepareCompleted(resolve); - - this._invokeApi('prepare', { - popupInfo: { - id: this._id, - depth: this._depth, - parentFrameId - }, - url: this.url, - childrenSupported: this._childrenSupported, - scale: this._contentScale - }); + const popupPreparedPromise = yomichan.getTemporaryListenerResult( + chrome.runtime.onMessage, + ({action, params}, {resolve}) => { + if ( + action === 'popupPrepareCompleted' && + isObject(params) && + params.targetPopupId === this._id + ) { + resolve(); + } + } + ); + + const parentFrameId = (typeof this._frameId === 'number' ? this._frameId : null); + this._container.setAttribute('src', chrome.runtime.getURL('/fg/float.html')); + this._container.addEventListener('load', () => { + this._invokeApi('prepare', { + popupInfo: { + id: this._id, + depth: this._depth, + parentFrameId + }, + url: this.url, + childrenSupported: this._childrenSupported, + scale: this._contentScale }); - this._observeFullscreen(true); - this._onFullscreenChanged(); - this._injectStyles(); }); + this._observeFullscreen(true); + this._onFullscreenChanged(); + this._injectStyles(); + + return popupPreparedPromise; } async _injectStyles() { @@ -358,21 +369,6 @@ class Popup { contentWindow.postMessage({action, params, token}, this._targetOrigin); } - _listenForDisplayPrepareCompleted(resolve) { - const runtimeMessageCallback = ({action, params}, sender, callback) => { - if ( - action === 'popupPrepareCompleted' && - isObject(params) && - params.targetPopupId === this._id - ) { - chrome.runtime.onMessage.removeListener(runtimeMessageCallback); - callback(); - resolve(); - } - }; - chrome.runtime.onMessage.addListener(runtimeMessageCallback); - } - static _getFullscreenElement() { return ( document.fullscreenElement || diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index 616f48b8..ca37a26a 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -278,11 +278,16 @@ const yomichan = (() => { constructor() { super(); - this._isBackendPreparedResolve = null; - this._isBackendPreparedPromise = new Promise((resolve) => (this._isBackendPreparedResolve = resolve)); + this._isBackendPreparedPromise = this.getTemporaryListenerResult( + chrome.runtime.onMessage, + ({action}, {resolve}) => { + if (action === 'backendPrepared') { + resolve(); + } + } + ); this._messageHandlers = new Map([ - ['backendPrepared', this._onBackendPrepared.bind(this)], ['getUrl', this._onMessageGetUrl.bind(this)], ['optionsUpdated', this._onMessageOptionsUpdated.bind(this)], ['zoomChanged', this._onMessageZoomChanged.bind(this)] @@ -362,10 +367,6 @@ const yomichan = (() => { return false; } - _onBackendPrepared() { - this._isBackendPreparedResolve(); - } - _onMessageGetUrl() { return {url: window.location.href}; } -- cgit v1.2.3 From 067c26982e4b374fbb51fa49d9930ddf0c8e00fd Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 4 Apr 2020 16:53:01 +0300 Subject: setTimeout refactoring --- ext/mixed/js/core.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'ext/mixed/js') diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index ca37a26a..4314926c 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -331,14 +331,15 @@ const yomichan = (() => { if (timeout !== null) { timeoutId = window.setTimeout(() => { timeoutId = null; - reject(new Error(`Listener timed out in ${timeout} ms`)); eventHandler.removeListener(runtimeMessageCallback); + reject(new Error(`Listener timed out in ${timeout} ms`)); }, timeout); } const cleanupResolve = (value) => { if (timeoutId !== null) { window.clearTimeout(timeoutId); + timeoutId = null; } eventHandler.removeListener(runtimeMessageCallback); sendResponse(); -- cgit v1.2.3 From 123e725049e5c6b5c3b7b53ee37aaf93e7bb0c61 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 4 Apr 2020 16:59:17 +0300 Subject: use duck typing for event listener --- ext/mixed/js/core.js | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) (limited to 'ext/mixed/js') diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index 4314926c..7fb3c32e 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -325,34 +325,37 @@ const yomichan = (() => { reject = _reject; }); - if (eventHandler === chrome.runtime.onMessage) { - const runtimeMessageCallback = ({action, params}, sender, sendResponse) => { - let timeoutId = null; - if (timeout !== null) { - timeoutId = window.setTimeout(() => { - timeoutId = null; - eventHandler.removeListener(runtimeMessageCallback); - reject(new Error(`Listener timed out in ${timeout} ms`)); - }, timeout); - } + if ( + typeof eventHandler.addListener === 'undefined' || + typeof eventHandler.removeListener === 'undefined' + ) { + throw new Error('Event handler type not supported'); + } - const cleanupResolve = (value) => { - if (timeoutId !== null) { - window.clearTimeout(timeoutId); - timeoutId = null; - } + const runtimeMessageCallback = ({action, params}, sender, sendResponse) => { + let timeoutId = null; + if (timeout !== null) { + timeoutId = window.setTimeout(() => { + timeoutId = null; eventHandler.removeListener(runtimeMessageCallback); - sendResponse(); - resolve(value); - }; + reject(new Error(`Listener timed out in ${timeout} ms`)); + }, timeout); + } - userCallback({action, params}, {resolve: cleanupResolve, sender}); + const cleanupResolve = (value) => { + if (timeoutId !== null) { + window.clearTimeout(timeoutId); + timeoutId = null; + } + eventHandler.removeListener(runtimeMessageCallback); + sendResponse(); + resolve(value); }; - eventHandler.addListener(runtimeMessageCallback); - } else { - throw new Error('Event handler type not supported'); - } + userCallback({action, params}, {resolve: cleanupResolve, sender}); + }; + + eventHandler.addListener(runtimeMessageCallback); return listenerPromise; } -- cgit v1.2.3 From 867902523f43929393500574076f793c4f62c225 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 4 Apr 2020 17:02:54 +0300 Subject: simplify promise --- ext/mixed/js/core.js | 51 ++++++++++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) (limited to 'ext/mixed/js') diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index 7fb3c32e..5f9e172d 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -318,13 +318,6 @@ const yomichan = (() => { } getTemporaryListenerResult(eventHandler, userCallback, timeout=null) { - let resolve; - let reject; - const listenerPromise = new Promise((_resolve, _reject) => { - resolve = _resolve; - reject = _reject; - }); - if ( typeof eventHandler.addListener === 'undefined' || typeof eventHandler.removeListener === 'undefined' @@ -332,32 +325,32 @@ const yomichan = (() => { throw new Error('Event handler type not supported'); } - const runtimeMessageCallback = ({action, params}, sender, sendResponse) => { - let timeoutId = null; - if (timeout !== null) { - timeoutId = window.setTimeout(() => { - timeoutId = null; - eventHandler.removeListener(runtimeMessageCallback); - reject(new Error(`Listener timed out in ${timeout} ms`)); - }, timeout); - } - - const cleanupResolve = (value) => { - if (timeoutId !== null) { - window.clearTimeout(timeoutId); - timeoutId = null; + return new Promise((resolve, reject) => { + const runtimeMessageCallback = ({action, params}, sender, sendResponse) => { + let timeoutId = null; + if (timeout !== null) { + timeoutId = window.setTimeout(() => { + timeoutId = null; + eventHandler.removeListener(runtimeMessageCallback); + reject(new Error(`Listener timed out in ${timeout} ms`)); + }, timeout); } - eventHandler.removeListener(runtimeMessageCallback); - sendResponse(); - resolve(value); - }; - userCallback({action, params}, {resolve: cleanupResolve, sender}); - }; + const cleanupResolve = (value) => { + if (timeoutId !== null) { + window.clearTimeout(timeoutId); + timeoutId = null; + } + eventHandler.removeListener(runtimeMessageCallback); + sendResponse(); + resolve(value); + }; - eventHandler.addListener(runtimeMessageCallback); + userCallback({action, params}, {resolve: cleanupResolve, sender}); + }; - return listenerPromise; + eventHandler.addListener(runtimeMessageCallback); + }); } // Private -- cgit v1.2.3 From 5596a8f6f7e23f6dfe45dc83bf077b0b688b84fb Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 5 Apr 2020 19:24:13 +0300 Subject: strict object property type check --- ext/mixed/js/core.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'ext/mixed/js') diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index 5f9e172d..6d0a5f0e 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -318,10 +318,10 @@ const yomichan = (() => { } getTemporaryListenerResult(eventHandler, userCallback, timeout=null) { - if ( - typeof eventHandler.addListener === 'undefined' || - typeof eventHandler.removeListener === 'undefined' - ) { + if (!( + typeof eventHandler.addListener === 'function' || + typeof eventHandler.removeListener === 'function' + )) { throw new Error('Event handler type not supported'); } -- cgit v1.2.3 From 22a97d916fc6ecab1200b0ffea18cf2d5c9923d4 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 5 Apr 2020 20:42:23 +0300 Subject: fix NOT (a OR B) to NOT (a AND b) --- ext/mixed/js/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ext/mixed/js') diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index 6d0a5f0e..db7fc69b 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -319,7 +319,7 @@ const yomichan = (() => { getTemporaryListenerResult(eventHandler, userCallback, timeout=null) { if (!( - typeof eventHandler.addListener === 'function' || + typeof eventHandler.addListener === 'function' && typeof eventHandler.removeListener === 'function' )) { throw new Error('Event handler type not supported'); -- cgit v1.2.3