Use deferPromise (#630)
* Use deferPromise * Move definition * Implement promiseTimeout using deferPromise
This commit is contained in:
parent
7b5dd5c310
commit
f2345b7d1c
@ -78,10 +78,10 @@ class Backend {
|
|||||||
this._isPrepared = false;
|
this._isPrepared = false;
|
||||||
this._prepareError = false;
|
this._prepareError = false;
|
||||||
this._preparePromise = null;
|
this._preparePromise = null;
|
||||||
this._prepareCompletePromise = new Promise((resolve, reject) => {
|
const {promise, resolve, reject} = deferPromise();
|
||||||
this._prepareCompleteResolve = resolve;
|
this._prepareCompletePromise = promise;
|
||||||
this._prepareCompleteReject = reject;
|
this._prepareCompleteResolve = resolve;
|
||||||
});
|
this._prepareCompleteReject = reject;
|
||||||
|
|
||||||
this._defaultBrowserActionTitle = null;
|
this._defaultBrowserActionTitle = null;
|
||||||
this._badgePrepareDelayTimer = null;
|
this._badgePrepareDelayTimer = null;
|
||||||
@ -1269,8 +1269,7 @@ class Backend {
|
|||||||
static async _findTab(timeout, checkUrl) {
|
static async _findTab(timeout, checkUrl) {
|
||||||
// This function works around the need to have the "tabs" permission to access tab.url.
|
// 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));
|
const tabs = await new Promise((resolve) => chrome.tabs.query({}, resolve));
|
||||||
let matchPromiseResolve = null;
|
const {promise: matchPromise, resolve: matchPromiseResolve} = deferPromise();
|
||||||
const matchPromise = new Promise((resolve) => { matchPromiseResolve = resolve; });
|
|
||||||
|
|
||||||
const checkTabUrl = ({tab, url}) => {
|
const checkTabUrl = ({tab, url}) => {
|
||||||
if (checkUrl(url, tab)) {
|
if (checkUrl(url, tab)) {
|
||||||
|
@ -229,12 +229,11 @@ class Popup {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const tokenMap = new Map();
|
const tokenMap = new Map();
|
||||||
let timer = null;
|
let timer = null;
|
||||||
let frameLoadedResolve = null;
|
let {
|
||||||
let frameLoadedReject = null;
|
promise: frameLoadedPromise,
|
||||||
const frameLoaded = new Promise((resolve2, reject2) => {
|
resolve: frameLoadedResolve,
|
||||||
frameLoadedResolve = resolve2;
|
reject: frameLoadedReject
|
||||||
frameLoadedReject = reject2;
|
} = deferPromise();
|
||||||
});
|
|
||||||
|
|
||||||
const postMessage = (action, params) => {
|
const postMessage = (action, params) => {
|
||||||
const contentWindow = frame.contentWindow;
|
const contentWindow = frame.contentWindow;
|
||||||
@ -261,7 +260,7 @@ class Popup {
|
|||||||
if (!isObject(message)) { return; }
|
if (!isObject(message)) { return; }
|
||||||
const {action, params} = message;
|
const {action, params} = message;
|
||||||
if (!isObject(params)) { return; }
|
if (!isObject(params)) { return; }
|
||||||
await frameLoaded;
|
await frameLoadedPromise;
|
||||||
if (timer === null) { return; } // Done
|
if (timer === null) { return; } // Done
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
@ -331,7 +330,7 @@ class Popup {
|
|||||||
frame.addEventListener('load', onLoad);
|
frame.addEventListener('load', onLoad);
|
||||||
|
|
||||||
// Prevent unhandled rejections
|
// Prevent unhandled rejections
|
||||||
frameLoaded.catch(() => {}); // NOP
|
frameLoadedPromise.catch(() => {}); // NOP
|
||||||
|
|
||||||
setupFrame(frame);
|
setupFrame(frame);
|
||||||
});
|
});
|
||||||
|
@ -212,12 +212,11 @@ const api = (() => {
|
|||||||
_createActionPort(timeout=5000) {
|
_createActionPort(timeout=5000) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let timer = null;
|
let timer = null;
|
||||||
let portNameResolve;
|
const {
|
||||||
let portNameReject;
|
promise: portNamePromise,
|
||||||
const portNamePromise = new Promise((resolve2, reject2) => {
|
resolve: portNameResolve,
|
||||||
portNameResolve = resolve2;
|
reject: portNameReject
|
||||||
portNameReject = reject2;
|
} = deferPromise();
|
||||||
});
|
|
||||||
|
|
||||||
const onConnect = async (port) => {
|
const onConnect = async (port) => {
|
||||||
try {
|
try {
|
||||||
|
@ -197,6 +197,16 @@ const clone = (() => {
|
|||||||
* Async utilities
|
* 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) {
|
function promiseTimeout(delay, resolveValue) {
|
||||||
if (delay <= 0) {
|
if (delay <= 0) {
|
||||||
const promise = Promise.resolve(resolveValue);
|
const promise = Promise.resolve(resolveValue);
|
||||||
@ -206,8 +216,7 @@ function promiseTimeout(delay, resolveValue) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let timer = null;
|
let timer = null;
|
||||||
let promiseResolve = null;
|
let {promise, resolve, reject} = deferPromise();
|
||||||
let promiseReject = null;
|
|
||||||
|
|
||||||
const complete = (callback, value) => {
|
const complete = (callback, value) => {
|
||||||
if (callback === null) { return; }
|
if (callback === null) { return; }
|
||||||
@ -215,39 +224,25 @@ function promiseTimeout(delay, resolveValue) {
|
|||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
timer = null;
|
timer = null;
|
||||||
}
|
}
|
||||||
promiseResolve = null;
|
resolve = null;
|
||||||
promiseReject = null;
|
reject = null;
|
||||||
callback(value);
|
callback(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
const resolve = (value) => complete(promiseResolve, value);
|
const resolveWrapper = (value) => complete(resolve, value);
|
||||||
const reject = (value) => complete(promiseReject, value);
|
const rejectWrapper = (value) => complete(reject, value);
|
||||||
|
|
||||||
const promise = new Promise((resolve2, reject2) => {
|
|
||||||
promiseResolve = resolve2;
|
|
||||||
promiseReject = reject2;
|
|
||||||
});
|
|
||||||
timer = setTimeout(() => {
|
timer = setTimeout(() => {
|
||||||
timer = null;
|
timer = null;
|
||||||
resolve(resolveValue);
|
resolveWrapper(resolveValue);
|
||||||
}, delay);
|
}, delay);
|
||||||
|
|
||||||
promise.resolve = resolve;
|
promise.resolve = resolveWrapper;
|
||||||
promise.reject = reject;
|
promise.reject = rejectWrapper;
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
function deferPromise() {
|
|
||||||
let resolve;
|
|
||||||
let reject;
|
|
||||||
const promise = new Promise((resolve2, reject2) => {
|
|
||||||
resolve = resolve2;
|
|
||||||
reject = reject2;
|
|
||||||
});
|
|
||||||
return {promise, resolve, reject};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Common events
|
* Common events
|
||||||
|
Loading…
Reference in New Issue
Block a user