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 {BrowserContext} from '../api/BrowserContext.js';
|
||||||
import {Page} from '../api/Page.js';
|
import {Page} from '../api/Page.js';
|
||||||
import {assert} from '../util/assert.js';
|
import {assert} from '../util/assert.js';
|
||||||
|
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
||||||
|
|
||||||
import {ChromeTargetManager} from './ChromeTargetManager.js';
|
import {ChromeTargetManager} from './ChromeTargetManager.js';
|
||||||
import {CDPSession, Connection, ConnectionEmittedEvents} from './Connection.js';
|
import {CDPSession, Connection, ConnectionEmittedEvents} from './Connection.js';
|
||||||
@ -478,11 +479,8 @@ export class CDPBrowser extends BrowserBase {
|
|||||||
options: WaitForTargetOptions = {}
|
options: WaitForTargetOptions = {}
|
||||||
): Promise<Target> {
|
): Promise<Target> {
|
||||||
const {timeout = 30000} = options;
|
const {timeout = 30000} = options;
|
||||||
let resolve: (value: Target | PromiseLike<Target>) => void;
|
const targetPromise = createDeferredPromise<Target | PromiseLike<Target>>();
|
||||||
let isResolved = false;
|
|
||||||
const targetPromise = new Promise<Target>(x => {
|
|
||||||
return (resolve = x);
|
|
||||||
});
|
|
||||||
this.on(BrowserEmittedEvents.TargetCreated, check);
|
this.on(BrowserEmittedEvents.TargetCreated, check);
|
||||||
this.on(BrowserEmittedEvents.TargetChanged, check);
|
this.on(BrowserEmittedEvents.TargetChanged, check);
|
||||||
try {
|
try {
|
||||||
@ -497,9 +495,8 @@ export class CDPBrowser extends BrowserBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function check(target: Target): Promise<void> {
|
async function check(target: Target): Promise<void> {
|
||||||
if ((await predicate(target)) && !isResolved) {
|
if ((await predicate(target)) && !targetPromise.resolved()) {
|
||||||
isResolved = true;
|
targetPromise.resolve(target);
|
||||||
resolve(target);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ import {Protocol} from 'devtools-protocol';
|
|||||||
|
|
||||||
import {TargetFilterCallback} from '../api/Browser.js';
|
import {TargetFilterCallback} from '../api/Browser.js';
|
||||||
import {assert} from '../util/assert.js';
|
import {assert} from '../util/assert.js';
|
||||||
|
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
||||||
|
|
||||||
import {CDPSession, Connection} from './Connection.js';
|
import {CDPSession, Connection} from './Connection.js';
|
||||||
import {EventEmitter} from './EventEmitter.js';
|
import {EventEmitter} from './EventEmitter.js';
|
||||||
@ -80,10 +81,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
|
|||||||
(event: Protocol.Target.DetachedFromTargetEvent) => void
|
(event: Protocol.Target.DetachedFromTargetEvent) => void
|
||||||
> = new WeakMap();
|
> = new WeakMap();
|
||||||
|
|
||||||
#initializeCallback = () => {};
|
#initializePromise = createDeferredPromise<void>();
|
||||||
#initializePromise: Promise<void> = new Promise(resolve => {
|
|
||||||
this.#initializeCallback = resolve;
|
|
||||||
});
|
|
||||||
#targetsIdsForInit: Set<string> = new Set();
|
#targetsIdsForInit: Set<string> = new Set();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@ -381,7 +379,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
|
|||||||
#finishInitializationIfReady(targetId?: string): void {
|
#finishInitializationIfReady(targetId?: string): void {
|
||||||
targetId !== undefined && this.#targetsIdsForInit.delete(targetId);
|
targetId !== undefined && this.#targetsIdsForInit.delete(targetId);
|
||||||
if (this.#targetsIdsForInit.size === 0) {
|
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 {TargetFilterCallback} from '../api/Browser.js';
|
||||||
import {assert} from '../util/assert.js';
|
import {assert} from '../util/assert.js';
|
||||||
|
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
||||||
|
|
||||||
import {CDPSession, Connection} from './Connection.js';
|
import {CDPSession, Connection} from './Connection.js';
|
||||||
import {EventEmitter} from './EventEmitter.js';
|
import {EventEmitter} from './EventEmitter.js';
|
||||||
@ -87,10 +88,7 @@ export class FirefoxTargetManager
|
|||||||
(event: Protocol.Target.AttachedToTargetEvent) => Promise<void>
|
(event: Protocol.Target.AttachedToTargetEvent) => Promise<void>
|
||||||
> = new WeakMap();
|
> = new WeakMap();
|
||||||
|
|
||||||
#initializeCallback = () => {};
|
#initializePromise = createDeferredPromise<void>();
|
||||||
#initializePromise: Promise<void> = new Promise(resolve => {
|
|
||||||
this.#initializeCallback = resolve;
|
|
||||||
});
|
|
||||||
#targetsIdsForInit: Set<string> = new Set();
|
#targetsIdsForInit: Set<string> = new Set();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@ -255,7 +253,7 @@ export class FirefoxTargetManager
|
|||||||
#finishInitializationIfReady(targetId: string): void {
|
#finishInitializationIfReady(targetId: string): void {
|
||||||
this.#targetsIdsForInit.delete(targetId);
|
this.#targetsIdsForInit.delete(targetId);
|
||||||
if (this.#targetsIdsForInit.size === 0) {
|
if (this.#targetsIdsForInit.size === 0) {
|
||||||
this.#initializeCallback();
|
this.#initializePromise.resolve();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -798,17 +798,7 @@ export class Frame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (path) {
|
if (path) {
|
||||||
let fs;
|
const fs = await importFSPromises();
|
||||||
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;
|
|
||||||
}
|
|
||||||
content = await fs.readFile(path, 'utf8');
|
content = await fs.readFile(path, 'utf8');
|
||||||
content += `//# sourceURL=${path.replace(/\n/g, '')}`;
|
content += `//# sourceURL=${path.replace(/\n/g, '')}`;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user