mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
refactor: use deferred promise (#10044)
This commit is contained in:
parent
c12512822a
commit
5547e43829
@ -33,6 +33,7 @@ import {
|
||||
import {BrowserContext} from '../api/BrowserContext.js';
|
||||
import {Page} from '../api/Page.js';
|
||||
import {assert} from '../util/assert.js';
|
||||
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
||||
|
||||
import {ChromeTargetManager} from './ChromeTargetManager.js';
|
||||
import {CDPSession, Connection, ConnectionEmittedEvents} from './Connection.js';
|
||||
@ -478,11 +479,8 @@ export class CDPBrowser extends BrowserBase {
|
||||
options: WaitForTargetOptions = {}
|
||||
): Promise<Target> {
|
||||
const {timeout = 30000} = options;
|
||||
let resolve: (value: Target | PromiseLike<Target>) => void;
|
||||
let isResolved = false;
|
||||
const targetPromise = new Promise<Target>(x => {
|
||||
return (resolve = x);
|
||||
});
|
||||
const targetPromise = createDeferredPromise<Target | PromiseLike<Target>>();
|
||||
|
||||
this.on(BrowserEmittedEvents.TargetCreated, check);
|
||||
this.on(BrowserEmittedEvents.TargetChanged, check);
|
||||
try {
|
||||
@ -497,9 +495,8 @@ export class CDPBrowser extends BrowserBase {
|
||||
}
|
||||
|
||||
async function check(target: Target): Promise<void> {
|
||||
if ((await predicate(target)) && !isResolved) {
|
||||
isResolved = true;
|
||||
resolve(target);
|
||||
if ((await predicate(target)) && !targetPromise.resolved()) {
|
||||
targetPromise.resolve(target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ import {Protocol} from 'devtools-protocol';
|
||||
|
||||
import {TargetFilterCallback} from '../api/Browser.js';
|
||||
import {assert} from '../util/assert.js';
|
||||
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
||||
|
||||
import {CDPSession, Connection} from './Connection.js';
|
||||
import {EventEmitter} from './EventEmitter.js';
|
||||
@ -80,10 +81,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
|
||||
(event: Protocol.Target.DetachedFromTargetEvent) => void
|
||||
> = new WeakMap();
|
||||
|
||||
#initializeCallback = () => {};
|
||||
#initializePromise: Promise<void> = new Promise(resolve => {
|
||||
this.#initializeCallback = resolve;
|
||||
});
|
||||
#initializePromise = createDeferredPromise<void>();
|
||||
#targetsIdsForInit: Set<string> = new Set();
|
||||
|
||||
constructor(
|
||||
@ -381,7 +379,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
|
||||
#finishInitializationIfReady(targetId?: string): void {
|
||||
targetId !== undefined && this.#targetsIdsForInit.delete(targetId);
|
||||
if (this.#targetsIdsForInit.size === 0) {
|
||||
this.#initializeCallback();
|
||||
this.#initializePromise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,7 @@ import {Protocol} from 'devtools-protocol';
|
||||
|
||||
import {TargetFilterCallback} from '../api/Browser.js';
|
||||
import {assert} from '../util/assert.js';
|
||||
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
||||
|
||||
import {CDPSession, Connection} from './Connection.js';
|
||||
import {EventEmitter} from './EventEmitter.js';
|
||||
@ -87,10 +88,7 @@ export class FirefoxTargetManager
|
||||
(event: Protocol.Target.AttachedToTargetEvent) => Promise<void>
|
||||
> = new WeakMap();
|
||||
|
||||
#initializeCallback = () => {};
|
||||
#initializePromise: Promise<void> = new Promise(resolve => {
|
||||
this.#initializeCallback = resolve;
|
||||
});
|
||||
#initializePromise = createDeferredPromise<void>();
|
||||
#targetsIdsForInit: Set<string> = new Set();
|
||||
|
||||
constructor(
|
||||
@ -255,7 +253,7 @@ export class FirefoxTargetManager
|
||||
#finishInitializationIfReady(targetId: string): void {
|
||||
this.#targetsIdsForInit.delete(targetId);
|
||||
if (this.#targetsIdsForInit.size === 0) {
|
||||
this.#initializeCallback();
|
||||
this.#initializePromise.resolve();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -798,17 +798,7 @@ export class Frame {
|
||||
}
|
||||
|
||||
if (path) {
|
||||
let fs;
|
||||
try {
|
||||
fs = (await import('fs')).promises;
|
||||
} catch (error) {
|
||||
if (error instanceof TypeError) {
|
||||
throw new Error(
|
||||
'Can only pass a file path in a Node-like environment.'
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
const fs = await importFSPromises();
|
||||
content = await fs.readFile(path, 'utf8');
|
||||
content += `//# sourceURL=${path.replace(/\n/g, '')}`;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user