diff options
-rw-r--r-- | ext/bg/js/backend.js | 11 | ||||
-rw-r--r-- | ext/fg/js/popup.js | 15 | ||||
-rw-r--r-- | ext/mixed/js/api.js | 11 | ||||
-rw-r--r-- | ext/mixed/js/core.js | 41 |
4 files changed, 35 insertions, 43 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 59a3de45..5ca84cab 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -78,10 +78,10 @@ class Backend { this._isPrepared = false; this._prepareError = false; this._preparePromise = null; - this._prepareCompletePromise = new Promise((resolve, reject) => { - this._prepareCompleteResolve = resolve; - this._prepareCompleteReject = reject; - }); + const {promise, resolve, reject} = deferPromise(); + this._prepareCompletePromise = promise; + this._prepareCompleteResolve = resolve; + this._prepareCompleteReject = reject; this._defaultBrowserActionTitle = null; this._badgePrepareDelayTimer = null; @@ -1269,8 +1269,7 @@ class Backend { static async _findTab(timeout, checkUrl) { // This function works around the need to have the "tabs" permission to access tab.url. const tabs = await new Promise((resolve) => chrome.tabs.query({}, resolve)); - let matchPromiseResolve = null; - const matchPromise = new Promise((resolve) => { matchPromiseResolve = resolve; }); + const {promise: matchPromise, resolve: matchPromiseResolve} = deferPromise(); const checkTabUrl = ({tab, url}) => { if (checkUrl(url, tab)) { diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 5ee62c9b..931813de 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -229,12 +229,11 @@ class Popup { return new Promise((resolve, reject) => { const tokenMap = new Map(); let timer = null; - let frameLoadedResolve = null; - let frameLoadedReject = null; - const frameLoaded = new Promise((resolve2, reject2) => { - frameLoadedResolve = resolve2; - frameLoadedReject = reject2; - }); + let { + promise: frameLoadedPromise, + resolve: frameLoadedResolve, + reject: frameLoadedReject + } = deferPromise(); const postMessage = (action, params) => { const contentWindow = frame.contentWindow; @@ -261,7 +260,7 @@ class Popup { if (!isObject(message)) { return; } const {action, params} = message; if (!isObject(params)) { return; } - await frameLoaded; + await frameLoadedPromise; if (timer === null) { return; } // Done switch (action) { @@ -331,7 +330,7 @@ class Popup { frame.addEventListener('load', onLoad); // Prevent unhandled rejections - frameLoaded.catch(() => {}); // NOP + frameLoadedPromise.catch(() => {}); // NOP setupFrame(frame); }); diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js index 5c17d50e..1e421147 100644 --- a/ext/mixed/js/api.js +++ b/ext/mixed/js/api.js @@ -212,12 +212,11 @@ const api = (() => { _createActionPort(timeout=5000) { return new Promise((resolve, reject) => { let timer = null; - let portNameResolve; - let portNameReject; - const portNamePromise = new Promise((resolve2, reject2) => { - portNameResolve = resolve2; - portNameReject = reject2; - }); + const { + promise: portNamePromise, + resolve: portNameResolve, + reject: portNameReject + } = deferPromise(); const onConnect = async (port) => { try { diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index e22be33d..fec26a0e 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -197,6 +197,16 @@ const clone = (() => { * Async utilities */ +function deferPromise() { + let resolve; + let reject; + const promise = new Promise((resolve2, reject2) => { + resolve = resolve2; + reject = reject2; + }); + return {promise, resolve, reject}; +} + function promiseTimeout(delay, resolveValue) { if (delay <= 0) { const promise = Promise.resolve(resolveValue); @@ -206,8 +216,7 @@ function promiseTimeout(delay, resolveValue) { } let timer = null; - let promiseResolve = null; - let promiseReject = null; + let {promise, resolve, reject} = deferPromise(); const complete = (callback, value) => { if (callback === null) { return; } @@ -215,39 +224,25 @@ function promiseTimeout(delay, resolveValue) { clearTimeout(timer); timer = null; } - promiseResolve = null; - promiseReject = null; + resolve = null; + reject = null; callback(value); }; - const resolve = (value) => complete(promiseResolve, value); - const reject = (value) => complete(promiseReject, value); + const resolveWrapper = (value) => complete(resolve, value); + const rejectWrapper = (value) => complete(reject, value); - const promise = new Promise((resolve2, reject2) => { - promiseResolve = resolve2; - promiseReject = reject2; - }); timer = setTimeout(() => { timer = null; - resolve(resolveValue); + resolveWrapper(resolveValue); }, delay); - promise.resolve = resolve; - promise.reject = reject; + promise.resolve = resolveWrapper; + promise.reject = rejectWrapper; return promise; } -function deferPromise() { - let resolve; - let reject; - const promise = new Promise((resolve2, reject2) => { - resolve = resolve2; - reject = reject2; - }); - return {promise, resolve, reject}; -} - /* * Common events |