refactor: rename DeferredPromise to Deferred (#10255)
This commit is contained in:
parent
b5a124ff73
commit
ff22ba8679
@ -33,7 +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 {createDeferred} from '../util/Deferred.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';
|
||||||
@ -361,7 +361,7 @@ export class CDPBrowser extends BrowserBase {
|
|||||||
|
|
||||||
#onAttachedToTarget = async (target: Target) => {
|
#onAttachedToTarget = async (target: Target) => {
|
||||||
if (
|
if (
|
||||||
(await target._initializedPromise.valueOrThrow()) ===
|
(await target._initializedDeferred.valueOrThrow()) ===
|
||||||
InitializationStatus.SUCCESS
|
InitializationStatus.SUCCESS
|
||||||
) {
|
) {
|
||||||
this.emit(BrowserEmittedEvents.TargetCreated, target);
|
this.emit(BrowserEmittedEvents.TargetCreated, target);
|
||||||
@ -372,10 +372,10 @@ export class CDPBrowser extends BrowserBase {
|
|||||||
};
|
};
|
||||||
|
|
||||||
#onDetachedFromTarget = async (target: Target): Promise<void> => {
|
#onDetachedFromTarget = async (target: Target): Promise<void> => {
|
||||||
target._initializedPromise.resolve(InitializationStatus.ABORTED);
|
target._initializedDeferred.resolve(InitializationStatus.ABORTED);
|
||||||
target._isClosedPromise.resolve();
|
target._isClosedDeferred.resolve();
|
||||||
if (
|
if (
|
||||||
(await target._initializedPromise.valueOrThrow()) ===
|
(await target._initializedDeferred.valueOrThrow()) ===
|
||||||
InitializationStatus.SUCCESS
|
InitializationStatus.SUCCESS
|
||||||
) {
|
) {
|
||||||
this.emit(BrowserEmittedEvents.TargetDestroyed, target);
|
this.emit(BrowserEmittedEvents.TargetDestroyed, target);
|
||||||
@ -438,7 +438,7 @@ export class CDPBrowser extends BrowserBase {
|
|||||||
throw new Error(`Missing target for page (id = ${targetId})`);
|
throw new Error(`Missing target for page (id = ${targetId})`);
|
||||||
}
|
}
|
||||||
const initialized =
|
const initialized =
|
||||||
(await target._initializedPromise.valueOrThrow()) ===
|
(await target._initializedDeferred.valueOrThrow()) ===
|
||||||
InitializationStatus.SUCCESS;
|
InitializationStatus.SUCCESS;
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
throw new Error(`Failed to create target for page (id = ${targetId})`);
|
throw new Error(`Failed to create target for page (id = ${targetId})`);
|
||||||
@ -461,7 +461,7 @@ export class CDPBrowser extends BrowserBase {
|
|||||||
this.#targetManager.getAvailableTargets().values()
|
this.#targetManager.getAvailableTargets().values()
|
||||||
).filter(target => {
|
).filter(target => {
|
||||||
return (
|
return (
|
||||||
target._initializedPromise.value() === InitializationStatus.SUCCESS
|
target._initializedDeferred.value() === InitializationStatus.SUCCESS
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -501,17 +501,17 @@ export class CDPBrowser extends BrowserBase {
|
|||||||
options: WaitForTargetOptions = {}
|
options: WaitForTargetOptions = {}
|
||||||
): Promise<Target> {
|
): Promise<Target> {
|
||||||
const {timeout = 30000} = options;
|
const {timeout = 30000} = options;
|
||||||
const targetPromise = createDeferredPromise<Target | PromiseLike<Target>>();
|
const targetDeferred = createDeferred<Target | PromiseLike<Target>>();
|
||||||
|
|
||||||
this.on(BrowserEmittedEvents.TargetCreated, check);
|
this.on(BrowserEmittedEvents.TargetCreated, check);
|
||||||
this.on(BrowserEmittedEvents.TargetChanged, check);
|
this.on(BrowserEmittedEvents.TargetChanged, check);
|
||||||
try {
|
try {
|
||||||
this.targets().forEach(check);
|
this.targets().forEach(check);
|
||||||
if (!timeout) {
|
if (!timeout) {
|
||||||
return await targetPromise.valueOrThrow();
|
return await targetDeferred.valueOrThrow();
|
||||||
}
|
}
|
||||||
return await waitWithTimeout(
|
return await waitWithTimeout(
|
||||||
targetPromise.valueOrThrow(),
|
targetDeferred.valueOrThrow(),
|
||||||
'target',
|
'target',
|
||||||
timeout
|
timeout
|
||||||
);
|
);
|
||||||
@ -521,8 +521,8 @@ export class CDPBrowser extends BrowserBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function check(target: Target): Promise<void> {
|
async function check(target: Target): Promise<void> {
|
||||||
if ((await predicate(target)) && !targetPromise.resolved()) {
|
if ((await predicate(target)) && !targetDeferred.resolved()) {
|
||||||
targetPromise.resolve(target);
|
targetDeferred.resolve(target);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +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 {createDeferred} from '../util/Deferred.js';
|
||||||
|
|
||||||
import {CDPSession, Connection} from './Connection.js';
|
import {CDPSession, Connection} from './Connection.js';
|
||||||
import {EventEmitter} from './EventEmitter.js';
|
import {EventEmitter} from './EventEmitter.js';
|
||||||
@ -81,7 +81,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
|
|||||||
(event: Protocol.Target.DetachedFromTargetEvent) => void
|
(event: Protocol.Target.DetachedFromTargetEvent) => void
|
||||||
> = new WeakMap();
|
> = new WeakMap();
|
||||||
|
|
||||||
#initializePromise = createDeferredPromise<void>();
|
#initializeDeferred = createDeferred<void>();
|
||||||
#targetsIdsForInit: Set<string> = new Set();
|
#targetsIdsForInit: Set<string> = new Set();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@ -131,7 +131,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
|
|||||||
autoAttach: true,
|
autoAttach: true,
|
||||||
});
|
});
|
||||||
this.#finishInitializationIfReady();
|
this.#finishInitializationIfReady();
|
||||||
await this.#initializePromise.valueOrThrow();
|
await this.#initializeDeferred.valueOrThrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
dispose(): void {
|
dispose(): void {
|
||||||
@ -268,7 +268,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
|
|||||||
}
|
}
|
||||||
const previousURL = target.url();
|
const previousURL = target.url();
|
||||||
const wasInitialized =
|
const wasInitialized =
|
||||||
target._initializedPromise.value() === InitializationStatus.SUCCESS;
|
target._initializedDeferred.value() === InitializationStatus.SUCCESS;
|
||||||
|
|
||||||
target._targetInfoChanged(event.targetInfo);
|
target._targetInfoChanged(event.targetInfo);
|
||||||
|
|
||||||
@ -391,7 +391,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.#initializePromise.resolve();
|
this.#initializeDeferred.resolve();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ import {Protocol} from 'devtools-protocol';
|
|||||||
import {ProtocolMapping} from 'devtools-protocol/types/protocol-mapping.js';
|
import {ProtocolMapping} from 'devtools-protocol/types/protocol-mapping.js';
|
||||||
|
|
||||||
import {assert} from '../util/assert.js';
|
import {assert} from '../util/assert.js';
|
||||||
import {createDeferredPromise, DeferredPromise} from '../util/util.js';
|
import {createDeferred, Deferred} from '../util/util.js';
|
||||||
|
|
||||||
import {ConnectionTransport} from './ConnectionTransport.js';
|
import {ConnectionTransport} from './ConnectionTransport.js';
|
||||||
import {debug} from './Debug.js';
|
import {debug} from './Debug.js';
|
||||||
@ -64,7 +64,7 @@ function createIncrementalIdGenerator(): GetIdFn {
|
|||||||
class Callback {
|
class Callback {
|
||||||
#id: number;
|
#id: number;
|
||||||
#error = new ProtocolError();
|
#error = new ProtocolError();
|
||||||
#promise = createDeferredPromise<unknown>();
|
#deferred = createDeferred<unknown>();
|
||||||
#timer?: ReturnType<typeof setTimeout>;
|
#timer?: ReturnType<typeof setTimeout>;
|
||||||
#label: string;
|
#label: string;
|
||||||
|
|
||||||
@ -73,7 +73,7 @@ class Callback {
|
|||||||
this.#label = label;
|
this.#label = label;
|
||||||
if (timeout) {
|
if (timeout) {
|
||||||
this.#timer = setTimeout(() => {
|
this.#timer = setTimeout(() => {
|
||||||
this.#promise.reject(
|
this.#deferred.reject(
|
||||||
rewriteError(
|
rewriteError(
|
||||||
this.#error,
|
this.#error,
|
||||||
`${label} timed out. Increase the 'protocolTimeout' setting in launch/connect calls for a higher timeout if needed.`
|
`${label} timed out. Increase the 'protocolTimeout' setting in launch/connect calls for a higher timeout if needed.`
|
||||||
@ -85,20 +85,20 @@ class Callback {
|
|||||||
|
|
||||||
resolve(value: unknown): void {
|
resolve(value: unknown): void {
|
||||||
clearTimeout(this.#timer);
|
clearTimeout(this.#timer);
|
||||||
this.#promise.resolve(value);
|
this.#deferred.resolve(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
reject(error: Error): void {
|
reject(error: Error): void {
|
||||||
clearTimeout(this.#timer);
|
clearTimeout(this.#timer);
|
||||||
this.#promise.reject(error);
|
this.#deferred.reject(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
get id(): number {
|
get id(): number {
|
||||||
return this.#id;
|
return this.#id;
|
||||||
}
|
}
|
||||||
|
|
||||||
get promise(): DeferredPromise<unknown> {
|
get promise(): Deferred<unknown> {
|
||||||
return this.#promise;
|
return this.#deferred;
|
||||||
}
|
}
|
||||||
|
|
||||||
get error(): ProtocolError {
|
get error(): ProtocolError {
|
||||||
|
@ -18,10 +18,7 @@ import Protocol from 'devtools-protocol';
|
|||||||
|
|
||||||
import {WaitTimeoutOptions} from '../api/Page.js';
|
import {WaitTimeoutOptions} from '../api/Page.js';
|
||||||
import {assert} from '../util/assert.js';
|
import {assert} from '../util/assert.js';
|
||||||
import {
|
import {createDeferred, Deferred} from '../util/Deferred.js';
|
||||||
createDeferredPromise,
|
|
||||||
DeferredPromise,
|
|
||||||
} from '../util/DeferredPromise.js';
|
|
||||||
|
|
||||||
import {CDPSession} from './Connection.js';
|
import {CDPSession} from './Connection.js';
|
||||||
import {TimeoutSettings} from './TimeoutSettings.js';
|
import {TimeoutSettings} from './TimeoutSettings.js';
|
||||||
@ -81,7 +78,7 @@ export class DeviceRequestPrompt {
|
|||||||
#updateDevicesHandle = this.#updateDevices.bind(this);
|
#updateDevicesHandle = this.#updateDevices.bind(this);
|
||||||
#waitForDevicePromises = new Set<{
|
#waitForDevicePromises = new Set<{
|
||||||
filter: (device: DeviceRequestPromptDevice) => boolean;
|
filter: (device: DeviceRequestPromptDevice) => boolean;
|
||||||
promise: DeferredPromise<DeviceRequestPromptDevice>;
|
promise: Deferred<DeviceRequestPromptDevice>;
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -154,14 +151,14 @@ export class DeviceRequestPrompt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const {timeout = this.#timeoutSettings.timeout()} = options;
|
const {timeout = this.#timeoutSettings.timeout()} = options;
|
||||||
const promise = createDeferredPromise<DeviceRequestPromptDevice>({
|
const deferred = createDeferred<DeviceRequestPromptDevice>({
|
||||||
message: `Waiting for \`DeviceRequestPromptDevice\` failed: ${timeout}ms exceeded`,
|
message: `Waiting for \`DeviceRequestPromptDevice\` failed: ${timeout}ms exceeded`,
|
||||||
timeout,
|
timeout,
|
||||||
});
|
});
|
||||||
const handle = {filter, promise};
|
const handle = {filter, promise: deferred};
|
||||||
this.#waitForDevicePromises.add(handle);
|
this.#waitForDevicePromises.add(handle);
|
||||||
try {
|
try {
|
||||||
return await promise.valueOrThrow();
|
return await deferred.valueOrThrow();
|
||||||
} finally {
|
} finally {
|
||||||
this.#waitForDevicePromises.delete(handle);
|
this.#waitForDevicePromises.delete(handle);
|
||||||
}
|
}
|
||||||
@ -218,9 +215,7 @@ export class DeviceRequestPrompt {
|
|||||||
export class DeviceRequestPromptManager {
|
export class DeviceRequestPromptManager {
|
||||||
#client: CDPSession | null;
|
#client: CDPSession | null;
|
||||||
#timeoutSettings: TimeoutSettings;
|
#timeoutSettings: TimeoutSettings;
|
||||||
#deviceRequestPromptPromises = new Set<
|
#deviceRequestPrompDeferreds = new Set<Deferred<DeviceRequestPrompt>>();
|
||||||
DeferredPromise<DeviceRequestPrompt>
|
|
||||||
>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
@ -248,27 +243,27 @@ export class DeviceRequestPromptManager {
|
|||||||
this.#client !== null,
|
this.#client !== null,
|
||||||
'Cannot wait for device prompt through detached session!'
|
'Cannot wait for device prompt through detached session!'
|
||||||
);
|
);
|
||||||
const needsEnable = this.#deviceRequestPromptPromises.size === 0;
|
const needsEnable = this.#deviceRequestPrompDeferreds.size === 0;
|
||||||
let enablePromise: Promise<void> | undefined;
|
let enablePromise: Promise<void> | undefined;
|
||||||
if (needsEnable) {
|
if (needsEnable) {
|
||||||
enablePromise = this.#client.send('DeviceAccess.enable');
|
enablePromise = this.#client.send('DeviceAccess.enable');
|
||||||
}
|
}
|
||||||
|
|
||||||
const {timeout = this.#timeoutSettings.timeout()} = options;
|
const {timeout = this.#timeoutSettings.timeout()} = options;
|
||||||
const promise = createDeferredPromise<DeviceRequestPrompt>({
|
const deferred = createDeferred<DeviceRequestPrompt>({
|
||||||
message: `Waiting for \`DeviceRequestPrompt\` failed: ${timeout}ms exceeded`,
|
message: `Waiting for \`DeviceRequestPrompt\` failed: ${timeout}ms exceeded`,
|
||||||
timeout,
|
timeout,
|
||||||
});
|
});
|
||||||
this.#deviceRequestPromptPromises.add(promise);
|
this.#deviceRequestPrompDeferreds.add(deferred);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const [result] = await Promise.all([
|
const [result] = await Promise.all([
|
||||||
promise.valueOrThrow(),
|
deferred.valueOrThrow(),
|
||||||
enablePromise,
|
enablePromise,
|
||||||
]);
|
]);
|
||||||
return result;
|
return result;
|
||||||
} finally {
|
} finally {
|
||||||
this.#deviceRequestPromptPromises.delete(promise);
|
this.#deviceRequestPrompDeferreds.delete(deferred);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,7 +273,7 @@ export class DeviceRequestPromptManager {
|
|||||||
#onDeviceRequestPrompted(
|
#onDeviceRequestPrompted(
|
||||||
event: Protocol.DeviceAccess.DeviceRequestPromptedEvent
|
event: Protocol.DeviceAccess.DeviceRequestPromptedEvent
|
||||||
) {
|
) {
|
||||||
if (!this.#deviceRequestPromptPromises.size) {
|
if (!this.#deviceRequestPrompDeferreds.size) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -288,9 +283,9 @@ export class DeviceRequestPromptManager {
|
|||||||
this.#timeoutSettings,
|
this.#timeoutSettings,
|
||||||
event
|
event
|
||||||
);
|
);
|
||||||
for (const promise of this.#deviceRequestPromptPromises) {
|
for (const promise of this.#deviceRequestPrompDeferreds) {
|
||||||
promise.resolve(devicePrompt);
|
promise.resolve(devicePrompt);
|
||||||
}
|
}
|
||||||
this.#deviceRequestPromptPromises.clear();
|
this.#deviceRequestPrompDeferreds.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +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 {createDeferred} from '../util/Deferred.js';
|
||||||
|
|
||||||
import {CDPSession, Connection} from './Connection.js';
|
import {CDPSession, Connection} from './Connection.js';
|
||||||
import {EventEmitter} from './EventEmitter.js';
|
import {EventEmitter} from './EventEmitter.js';
|
||||||
@ -88,7 +88,7 @@ export class FirefoxTargetManager
|
|||||||
(event: Protocol.Target.AttachedToTargetEvent) => Promise<void>
|
(event: Protocol.Target.AttachedToTargetEvent) => Promise<void>
|
||||||
> = new WeakMap();
|
> = new WeakMap();
|
||||||
|
|
||||||
#initializePromise = createDeferredPromise<void>();
|
#initializeDeferred = createDeferred<void>();
|
||||||
#targetsIdsForInit: Set<string> = new Set();
|
#targetsIdsForInit: Set<string> = new Set();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@ -169,7 +169,7 @@ export class FirefoxTargetManager
|
|||||||
filter: [{}],
|
filter: [{}],
|
||||||
});
|
});
|
||||||
this.#targetsIdsForInit = new Set(this.#discoveredTargetsByTargetId.keys());
|
this.#targetsIdsForInit = new Set(this.#discoveredTargetsByTargetId.keys());
|
||||||
await this.#initializePromise.valueOrThrow();
|
await this.#initializeDeferred.valueOrThrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
#onTargetCreated = async (
|
#onTargetCreated = async (
|
||||||
@ -253,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.#initializePromise.resolve();
|
this.#initializeDeferred.resolve();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -389,8 +389,8 @@ export class Frame extends BaseFrame {
|
|||||||
|
|
||||||
return this.worlds[MAIN_WORLD].transferHandle(
|
return this.worlds[MAIN_WORLD].transferHandle(
|
||||||
await this.worlds[PUPPETEER_WORLD].evaluateHandle(
|
await this.worlds[PUPPETEER_WORLD].evaluateHandle(
|
||||||
async ({createDeferredPromise}, {url, id, type, content}) => {
|
async ({createDeferred}, {url, id, type, content}) => {
|
||||||
const promise = createDeferredPromise<void>();
|
const deferred = createDeferred<void>();
|
||||||
const script = document.createElement('script');
|
const script = document.createElement('script');
|
||||||
script.type = type;
|
script.type = type;
|
||||||
script.text = content;
|
script.text = content;
|
||||||
@ -399,27 +399,27 @@ export class Frame extends BaseFrame {
|
|||||||
script.addEventListener(
|
script.addEventListener(
|
||||||
'load',
|
'load',
|
||||||
() => {
|
() => {
|
||||||
return promise.resolve();
|
return deferred.resolve();
|
||||||
},
|
},
|
||||||
{once: true}
|
{once: true}
|
||||||
);
|
);
|
||||||
script.addEventListener(
|
script.addEventListener(
|
||||||
'error',
|
'error',
|
||||||
event => {
|
event => {
|
||||||
promise.reject(
|
deferred.reject(
|
||||||
new Error(event.message ?? 'Could not load script')
|
new Error(event.message ?? 'Could not load script')
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
{once: true}
|
{once: true}
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
promise.resolve();
|
deferred.resolve();
|
||||||
}
|
}
|
||||||
if (id) {
|
if (id) {
|
||||||
script.id = id;
|
script.id = id;
|
||||||
}
|
}
|
||||||
document.head.appendChild(script);
|
document.head.appendChild(script);
|
||||||
await promise.valueOrThrow();
|
await deferred.valueOrThrow();
|
||||||
return script;
|
return script;
|
||||||
},
|
},
|
||||||
LazyArg.create(context => {
|
LazyArg.create(context => {
|
||||||
@ -457,8 +457,8 @@ export class Frame extends BaseFrame {
|
|||||||
|
|
||||||
return this.worlds[MAIN_WORLD].transferHandle(
|
return this.worlds[MAIN_WORLD].transferHandle(
|
||||||
await this.worlds[PUPPETEER_WORLD].evaluateHandle(
|
await this.worlds[PUPPETEER_WORLD].evaluateHandle(
|
||||||
async ({createDeferredPromise}, {url, content}) => {
|
async ({createDeferred}, {url, content}) => {
|
||||||
const promise = createDeferredPromise<void>();
|
const deferred = createDeferred<void>();
|
||||||
let element: HTMLStyleElement | HTMLLinkElement;
|
let element: HTMLStyleElement | HTMLLinkElement;
|
||||||
if (!url) {
|
if (!url) {
|
||||||
element = document.createElement('style');
|
element = document.createElement('style');
|
||||||
@ -472,14 +472,14 @@ export class Frame extends BaseFrame {
|
|||||||
element.addEventListener(
|
element.addEventListener(
|
||||||
'load',
|
'load',
|
||||||
() => {
|
() => {
|
||||||
promise.resolve();
|
deferred.resolve();
|
||||||
},
|
},
|
||||||
{once: true}
|
{once: true}
|
||||||
);
|
);
|
||||||
element.addEventListener(
|
element.addEventListener(
|
||||||
'error',
|
'error',
|
||||||
event => {
|
event => {
|
||||||
promise.reject(
|
deferred.reject(
|
||||||
new Error(
|
new Error(
|
||||||
(event as ErrorEvent).message ?? 'Could not load style'
|
(event as ErrorEvent).message ?? 'Could not load style'
|
||||||
)
|
)
|
||||||
@ -488,7 +488,7 @@ export class Frame extends BaseFrame {
|
|||||||
{once: true}
|
{once: true}
|
||||||
);
|
);
|
||||||
document.head.appendChild(element);
|
document.head.appendChild(element);
|
||||||
await promise.valueOrThrow();
|
await deferred.valueOrThrow();
|
||||||
return element;
|
return element;
|
||||||
},
|
},
|
||||||
LazyArg.create(context => {
|
LazyArg.create(context => {
|
||||||
|
@ -15,10 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {Frame as BaseFrame} from '../api/Frame.js';
|
import {Frame as BaseFrame} from '../api/Frame.js';
|
||||||
import {
|
import {createDeferred, Deferred} from '../util/Deferred.js';
|
||||||
createDeferredPromise,
|
|
||||||
DeferredPromise,
|
|
||||||
} from '../util/DeferredPromise.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Keeps track of the page frame tree and it's is managed by
|
* Keeps track of the page frame tree and it's is managed by
|
||||||
@ -34,7 +31,7 @@ export class FrameTree<Frame extends BaseFrame> {
|
|||||||
// frameID -> childFrameIDs
|
// frameID -> childFrameIDs
|
||||||
#childIds = new Map<string, Set<string>>();
|
#childIds = new Map<string, Set<string>>();
|
||||||
#mainFrame?: Frame;
|
#mainFrame?: Frame;
|
||||||
#waitRequests = new Map<string, Set<DeferredPromise<Frame>>>();
|
#waitRequests = new Map<string, Set<Deferred<Frame>>>();
|
||||||
|
|
||||||
getMainFrame(): Frame | undefined {
|
getMainFrame(): Frame | undefined {
|
||||||
return this.#mainFrame;
|
return this.#mainFrame;
|
||||||
@ -53,9 +50,9 @@ export class FrameTree<Frame extends BaseFrame> {
|
|||||||
if (frame) {
|
if (frame) {
|
||||||
return Promise.resolve(frame);
|
return Promise.resolve(frame);
|
||||||
}
|
}
|
||||||
const deferred = createDeferredPromise<Frame>();
|
const deferred = createDeferred<Frame>();
|
||||||
const callbacks =
|
const callbacks =
|
||||||
this.#waitRequests.get(frameId) || new Set<DeferredPromise<Frame>>();
|
this.#waitRequests.get(frameId) || new Set<Deferred<Frame>>();
|
||||||
callbacks.add(deferred);
|
callbacks.add(deferred);
|
||||||
return deferred.valueOrThrow();
|
return deferred.valueOrThrow();
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ import {
|
|||||||
HTTPResponse as BaseHTTPResponse,
|
HTTPResponse as BaseHTTPResponse,
|
||||||
RemoteAddress,
|
RemoteAddress,
|
||||||
} from '../api/HTTPResponse.js';
|
} from '../api/HTTPResponse.js';
|
||||||
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
import {createDeferred} from '../util/Deferred.js';
|
||||||
|
|
||||||
import {CDPSession} from './Connection.js';
|
import {CDPSession} from './Connection.js';
|
||||||
import {ProtocolError} from './Errors.js';
|
import {ProtocolError} from './Errors.js';
|
||||||
@ -34,7 +34,7 @@ export class HTTPResponse extends BaseHTTPResponse {
|
|||||||
#client: CDPSession;
|
#client: CDPSession;
|
||||||
#request: HTTPRequest;
|
#request: HTTPRequest;
|
||||||
#contentPromise: Promise<Buffer> | null = null;
|
#contentPromise: Promise<Buffer> | null = null;
|
||||||
#bodyLoadedPromise = createDeferredPromise<Error | void>();
|
#bodyLoadedDeferred = createDeferred<Error | void>();
|
||||||
#remoteAddress: RemoteAddress;
|
#remoteAddress: RemoteAddress;
|
||||||
#status: number;
|
#status: number;
|
||||||
#statusText: string;
|
#statusText: string;
|
||||||
@ -101,9 +101,9 @@ export class HTTPResponse extends BaseHTTPResponse {
|
|||||||
|
|
||||||
override _resolveBody(err: Error | null): void {
|
override _resolveBody(err: Error | null): void {
|
||||||
if (err) {
|
if (err) {
|
||||||
return this.#bodyLoadedPromise.resolve(err);
|
return this.#bodyLoadedDeferred.resolve(err);
|
||||||
}
|
}
|
||||||
return this.#bodyLoadedPromise.resolve();
|
return this.#bodyLoadedDeferred.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
override remoteAddress(): RemoteAddress {
|
override remoteAddress(): RemoteAddress {
|
||||||
@ -136,7 +136,7 @@ export class HTTPResponse extends BaseHTTPResponse {
|
|||||||
|
|
||||||
override buffer(): Promise<Buffer> {
|
override buffer(): Promise<Buffer> {
|
||||||
if (!this.#contentPromise) {
|
if (!this.#contentPromise) {
|
||||||
this.#contentPromise = this.#bodyLoadedPromise
|
this.#contentPromise = this.#bodyLoadedDeferred
|
||||||
.valueOrThrow()
|
.valueOrThrow()
|
||||||
.then(async error => {
|
.then(async error => {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
@ -19,7 +19,7 @@ import {Protocol} from 'devtools-protocol';
|
|||||||
import type {ClickOptions, ElementHandle} from '../api/ElementHandle.js';
|
import type {ClickOptions, ElementHandle} from '../api/ElementHandle.js';
|
||||||
import {JSHandle} from '../api/JSHandle.js';
|
import {JSHandle} from '../api/JSHandle.js';
|
||||||
import {assert} from '../util/assert.js';
|
import {assert} from '../util/assert.js';
|
||||||
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
import {createDeferred} from '../util/Deferred.js';
|
||||||
|
|
||||||
import {Binding} from './Binding.js';
|
import {Binding} from './Binding.js';
|
||||||
import {CDPSession} from './Connection.js';
|
import {CDPSession} from './Connection.js';
|
||||||
@ -101,7 +101,7 @@ export interface IsolatedWorldChart {
|
|||||||
export class IsolatedWorld {
|
export class IsolatedWorld {
|
||||||
#frame: Frame;
|
#frame: Frame;
|
||||||
#document?: ElementHandle<Document>;
|
#document?: ElementHandle<Document>;
|
||||||
#context = createDeferredPromise<ExecutionContext>();
|
#context = createDeferred<ExecutionContext>();
|
||||||
#detached = false;
|
#detached = false;
|
||||||
|
|
||||||
// Set of bindings that have been registered in the current context.
|
// Set of bindings that have been registered in the current context.
|
||||||
@ -144,7 +144,7 @@ export class IsolatedWorld {
|
|||||||
|
|
||||||
clearContext(): void {
|
clearContext(): void {
|
||||||
this.#document = undefined;
|
this.#document = undefined;
|
||||||
this.#context = createDeferredPromise();
|
this.#context = createDeferred();
|
||||||
}
|
}
|
||||||
|
|
||||||
setContext(context: ExecutionContext): void {
|
setContext(context: ExecutionContext): void {
|
||||||
|
@ -16,10 +16,7 @@
|
|||||||
|
|
||||||
import {HTTPResponse} from '../api/HTTPResponse.js';
|
import {HTTPResponse} from '../api/HTTPResponse.js';
|
||||||
import {assert} from '../util/assert.js';
|
import {assert} from '../util/assert.js';
|
||||||
import {
|
import {Deferred, createDeferred} from '../util/Deferred.js';
|
||||||
DeferredPromise,
|
|
||||||
createDeferredPromise,
|
|
||||||
} from '../util/DeferredPromise.js';
|
|
||||||
|
|
||||||
import {CDPSessionEmittedEvents} from './Connection.js';
|
import {CDPSessionEmittedEvents} from './Connection.js';
|
||||||
import {TimeoutError} from './Errors.js';
|
import {TimeoutError} from './Errors.js';
|
||||||
@ -74,10 +71,10 @@ export class LifecycleWatcher {
|
|||||||
#eventListeners: PuppeteerEventListener[];
|
#eventListeners: PuppeteerEventListener[];
|
||||||
#initialLoaderId: string;
|
#initialLoaderId: string;
|
||||||
|
|
||||||
#sameDocumentNavigationPromise = createDeferredPromise<Error | undefined>();
|
#sameDocumentNavigationDeferred = createDeferred<Error | undefined>();
|
||||||
#lifecyclePromise = createDeferredPromise<void>();
|
#lifecycleDeferred = createDeferred<void>();
|
||||||
#newDocumentNavigationPromise = createDeferredPromise<Error | undefined>();
|
#newDocumentNavigationDeferred = createDeferred<Error | undefined>();
|
||||||
#terminationPromise = createDeferredPromise<Error | undefined>();
|
#terminationDeferred = createDeferred<Error | undefined>();
|
||||||
|
|
||||||
#timeoutPromise: Promise<TimeoutError | undefined>;
|
#timeoutPromise: Promise<TimeoutError | undefined>;
|
||||||
|
|
||||||
@ -85,7 +82,7 @@ export class LifecycleWatcher {
|
|||||||
#hasSameDocumentNavigation?: boolean;
|
#hasSameDocumentNavigation?: boolean;
|
||||||
#swapped?: boolean;
|
#swapped?: boolean;
|
||||||
|
|
||||||
#navigationResponseReceived?: DeferredPromise<void>;
|
#navigationResponseReceived?: Deferred<void>;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
frameManager: FrameManager,
|
frameManager: FrameManager,
|
||||||
@ -172,7 +169,7 @@ export class LifecycleWatcher {
|
|||||||
// navigation requests reported by the backend. This generally should not
|
// navigation requests reported by the backend. This generally should not
|
||||||
// happen by it looks like it's possible.
|
// happen by it looks like it's possible.
|
||||||
this.#navigationResponseReceived?.resolve();
|
this.#navigationResponseReceived?.resolve();
|
||||||
this.#navigationResponseReceived = createDeferredPromise();
|
this.#navigationResponseReceived = createDeferred();
|
||||||
if (request.response() !== null) {
|
if (request.response() !== null) {
|
||||||
this.#navigationResponseReceived?.resolve();
|
this.#navigationResponseReceived?.resolve();
|
||||||
}
|
}
|
||||||
@ -194,7 +191,7 @@ export class LifecycleWatcher {
|
|||||||
|
|
||||||
#onFrameDetached(frame: Frame): void {
|
#onFrameDetached(frame: Frame): void {
|
||||||
if (this.#frame === frame) {
|
if (this.#frame === frame) {
|
||||||
this.#terminationPromise.resolve(
|
this.#terminationDeferred.resolve(
|
||||||
new Error('Navigating frame was detached')
|
new Error('Navigating frame was detached')
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
@ -209,25 +206,25 @@ export class LifecycleWatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#terminate(error: Error): void {
|
#terminate(error: Error): void {
|
||||||
this.#terminationPromise.resolve(error);
|
this.#terminationDeferred.resolve(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
sameDocumentNavigationPromise(): Promise<Error | undefined> {
|
sameDocumentNavigationPromise(): Promise<Error | undefined> {
|
||||||
return this.#sameDocumentNavigationPromise.valueOrThrow();
|
return this.#sameDocumentNavigationDeferred.valueOrThrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
newDocumentNavigationPromise(): Promise<Error | undefined> {
|
newDocumentNavigationPromise(): Promise<Error | undefined> {
|
||||||
return this.#newDocumentNavigationPromise.valueOrThrow();
|
return this.#newDocumentNavigationDeferred.valueOrThrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
lifecyclePromise(): Promise<void> {
|
lifecyclePromise(): Promise<void> {
|
||||||
return this.#lifecyclePromise.valueOrThrow();
|
return this.#lifecycleDeferred.valueOrThrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
timeoutOrTerminationPromise(): Promise<Error | TimeoutError | undefined> {
|
timeoutOrTerminationPromise(): Promise<Error | TimeoutError | undefined> {
|
||||||
return Promise.race([
|
return Promise.race([
|
||||||
this.#timeoutPromise,
|
this.#timeoutPromise,
|
||||||
this.#terminationPromise.valueOrThrow(),
|
this.#terminationDeferred.valueOrThrow(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -271,12 +268,12 @@ export class LifecycleWatcher {
|
|||||||
if (!checkLifecycle(this.#frame, this.#expectedLifecycle)) {
|
if (!checkLifecycle(this.#frame, this.#expectedLifecycle)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.#lifecyclePromise.resolve();
|
this.#lifecycleDeferred.resolve();
|
||||||
if (this.#hasSameDocumentNavigation) {
|
if (this.#hasSameDocumentNavigation) {
|
||||||
this.#sameDocumentNavigationPromise.resolve(undefined);
|
this.#sameDocumentNavigationDeferred.resolve(undefined);
|
||||||
}
|
}
|
||||||
if (this.#swapped || this.#frame._loaderId !== this.#initialLoaderId) {
|
if (this.#swapped || this.#frame._loaderId !== this.#initialLoaderId) {
|
||||||
this.#newDocumentNavigationPromise.resolve(undefined);
|
this.#newDocumentNavigationDeferred.resolve(undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkLifecycle(
|
function checkLifecycle(
|
||||||
|
@ -17,8 +17,8 @@
|
|||||||
import {Protocol} from 'devtools-protocol';
|
import {Protocol} from 'devtools-protocol';
|
||||||
|
|
||||||
import {assert} from '../util/assert.js';
|
import {assert} from '../util/assert.js';
|
||||||
import {createDebuggableDeferredPromise} from '../util/DebuggableDeferredPromise.js';
|
import {createDebuggableDeferred} from '../util/DebuggableDeferred.js';
|
||||||
import {DeferredPromise} from '../util/DeferredPromise.js';
|
import {Deferred} from '../util/Deferred.js';
|
||||||
|
|
||||||
import {CDPSession} from './Connection.js';
|
import {CDPSession} from './Connection.js';
|
||||||
import {EventEmitter} from './EventEmitter.js';
|
import {EventEmitter} from './EventEmitter.js';
|
||||||
@ -88,7 +88,7 @@ export class NetworkManager extends EventEmitter {
|
|||||||
download: -1,
|
download: -1,
|
||||||
latency: 0,
|
latency: 0,
|
||||||
};
|
};
|
||||||
#deferredInitPromise?: DeferredPromise<void>;
|
#deferredInit?: Deferred<void>;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
client: CDPSession,
|
client: CDPSession,
|
||||||
@ -130,10 +130,10 @@ export class NetworkManager extends EventEmitter {
|
|||||||
* might not resolve until after the target is resumed causing a deadlock.
|
* might not resolve until after the target is resumed causing a deadlock.
|
||||||
*/
|
*/
|
||||||
initialize(): Promise<void> {
|
initialize(): Promise<void> {
|
||||||
if (this.#deferredInitPromise) {
|
if (this.#deferredInit) {
|
||||||
return this.#deferredInitPromise.valueOrThrow();
|
return this.#deferredInit.valueOrThrow();
|
||||||
}
|
}
|
||||||
this.#deferredInitPromise = createDebuggableDeferredPromise(
|
this.#deferredInit = createDebuggableDeferred(
|
||||||
'NetworkManager initialization timed out'
|
'NetworkManager initialization timed out'
|
||||||
);
|
);
|
||||||
const init = Promise.all([
|
const init = Promise.all([
|
||||||
@ -144,7 +144,7 @@ export class NetworkManager extends EventEmitter {
|
|||||||
: null,
|
: null,
|
||||||
this.#client.send('Network.enable'),
|
this.#client.send('Network.enable'),
|
||||||
]);
|
]);
|
||||||
const deferredInitPromise = this.#deferredInitPromise;
|
const deferredInitPromise = this.#deferredInit;
|
||||||
init
|
init
|
||||||
.then(() => {
|
.then(() => {
|
||||||
deferredInitPromise.resolve();
|
deferredInitPromise.resolve();
|
||||||
@ -152,7 +152,7 @@ export class NetworkManager extends EventEmitter {
|
|||||||
.catch(err => {
|
.catch(err => {
|
||||||
deferredInitPromise.reject(err);
|
deferredInitPromise.reject(err);
|
||||||
});
|
});
|
||||||
return this.#deferredInitPromise.valueOrThrow();
|
return this.#deferredInit.valueOrThrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
async authenticate(credentials?: Credentials): Promise<void> {
|
async authenticate(credentials?: Credentials): Promise<void> {
|
||||||
|
@ -43,10 +43,7 @@ import {
|
|||||||
NewDocumentScriptEvaluation,
|
NewDocumentScriptEvaluation,
|
||||||
} from '../api/Page.js';
|
} from '../api/Page.js';
|
||||||
import {assert} from '../util/assert.js';
|
import {assert} from '../util/assert.js';
|
||||||
import {
|
import {createDeferred, Deferred} from '../util/Deferred.js';
|
||||||
createDeferredPromise,
|
|
||||||
DeferredPromise,
|
|
||||||
} from '../util/DeferredPromise.js';
|
|
||||||
import {isErrorLike} from '../util/ErrorLike.js';
|
import {isErrorLike} from '../util/ErrorLike.js';
|
||||||
|
|
||||||
import {Accessibility} from './Accessibility.js';
|
import {Accessibility} from './Accessibility.js';
|
||||||
@ -155,7 +152,7 @@ export class CDPPage extends Page {
|
|||||||
#viewport: Viewport | null;
|
#viewport: Viewport | null;
|
||||||
#screenshotTaskQueue: TaskQueue;
|
#screenshotTaskQueue: TaskQueue;
|
||||||
#workers = new Map<string, WebWorker>();
|
#workers = new Map<string, WebWorker>();
|
||||||
#fileChooserPromises = new Set<DeferredPromise<FileChooser>>();
|
#fileChooserPromises = new Set<Deferred<FileChooser>>();
|
||||||
|
|
||||||
#disconnectPromise?: Promise<Error>;
|
#disconnectPromise?: Promise<Error>;
|
||||||
#serviceWorkerBypassed = false;
|
#serviceWorkerBypassed = false;
|
||||||
@ -257,7 +254,7 @@ export class CDPPage extends Page {
|
|||||||
client.on('Page.fileChooserOpened', event => {
|
client.on('Page.fileChooserOpened', event => {
|
||||||
return this.#onFileChooser(event);
|
return this.#onFileChooser(event);
|
||||||
});
|
});
|
||||||
this.#target._isClosedPromise
|
this.#target._isClosedDeferred
|
||||||
.valueOrThrow()
|
.valueOrThrow()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
this.#target
|
this.#target
|
||||||
@ -372,23 +369,23 @@ export class CDPPage extends Page {
|
|||||||
): Promise<FileChooser> {
|
): Promise<FileChooser> {
|
||||||
const needsEnable = this.#fileChooserPromises.size === 0;
|
const needsEnable = this.#fileChooserPromises.size === 0;
|
||||||
const {timeout = this.#timeoutSettings.timeout()} = options;
|
const {timeout = this.#timeoutSettings.timeout()} = options;
|
||||||
const promise = createDeferredPromise<FileChooser>({
|
const deferred = createDeferred<FileChooser>({
|
||||||
message: `Waiting for \`FileChooser\` failed: ${timeout}ms exceeded`,
|
message: `Waiting for \`FileChooser\` failed: ${timeout}ms exceeded`,
|
||||||
timeout,
|
timeout,
|
||||||
});
|
});
|
||||||
this.#fileChooserPromises.add(promise);
|
this.#fileChooserPromises.add(deferred);
|
||||||
let enablePromise: Promise<void> | undefined;
|
let enablePromise: Promise<void> | undefined;
|
||||||
if (needsEnable) {
|
if (needsEnable) {
|
||||||
enablePromise = this.#client.send('Page.setInterceptFileChooserDialog', {
|
enablePromise = this.#client.send('Page.setInterceptFileChooserDialog', {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return Promise.all([promise.valueOrThrow(), enablePromise])
|
return Promise.all([deferred.valueOrThrow(), enablePromise])
|
||||||
.then(([result]) => {
|
.then(([result]) => {
|
||||||
return result;
|
return result;
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
this.#fileChooserPromises.delete(promise);
|
this.#fileChooserPromises.delete(deferred);
|
||||||
throw error;
|
throw error;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -1011,7 +1008,7 @@ export class CDPPage extends Page {
|
|||||||
|
|
||||||
const networkManager = this.#frameManager.networkManager;
|
const networkManager = this.#frameManager.networkManager;
|
||||||
|
|
||||||
const idlePromise = createDeferredPromise<void>();
|
const idleDeferred = createDeferred<void>();
|
||||||
|
|
||||||
let abortRejectCallback: (error: Error) => void;
|
let abortRejectCallback: (error: Error) => void;
|
||||||
const abortPromise = new Promise<Error>((_, reject) => {
|
const abortPromise = new Promise<Error>((_, reject) => {
|
||||||
@ -1027,7 +1024,7 @@ export class CDPPage extends Page {
|
|||||||
const evaluate = () => {
|
const evaluate = () => {
|
||||||
idleTimer && clearTimeout(idleTimer);
|
idleTimer && clearTimeout(idleTimer);
|
||||||
if (networkManager.numRequestsInProgress() === 0) {
|
if (networkManager.numRequestsInProgress() === 0) {
|
||||||
idleTimer = setTimeout(idlePromise.resolve, idleTime);
|
idleTimer = setTimeout(idleDeferred.resolve, idleTime);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1055,7 +1052,7 @@ export class CDPPage extends Page {
|
|||||||
];
|
];
|
||||||
|
|
||||||
await Promise.race([
|
await Promise.race([
|
||||||
idlePromise.valueOrThrow(),
|
idleDeferred.valueOrThrow(),
|
||||||
...eventPromises,
|
...eventPromises,
|
||||||
this.#sessionClosePromise(),
|
this.#sessionClosePromise(),
|
||||||
]).then(
|
]).then(
|
||||||
@ -1579,7 +1576,7 @@ export class CDPPage extends Page {
|
|||||||
await connection.send('Target.closeTarget', {
|
await connection.send('Target.closeTarget', {
|
||||||
targetId: this.#target._targetId,
|
targetId: this.#target._targetId,
|
||||||
});
|
});
|
||||||
await this.#target._isClosedPromise.valueOrThrow();
|
await this.#target._isClosedDeferred.valueOrThrow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ import {Protocol} from 'devtools-protocol';
|
|||||||
import type {Browser} from '../api/Browser.js';
|
import type {Browser} from '../api/Browser.js';
|
||||||
import type {BrowserContext} from '../api/BrowserContext.js';
|
import type {BrowserContext} from '../api/BrowserContext.js';
|
||||||
import {Page, PageEmittedEvents} from '../api/Page.js';
|
import {Page, PageEmittedEvents} from '../api/Page.js';
|
||||||
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
import {createDeferred} from '../util/Deferred.js';
|
||||||
|
|
||||||
import {CDPSession} from './Connection.js';
|
import {CDPSession} from './Connection.js';
|
||||||
import {CDPPage} from './Page.js';
|
import {CDPPage} from './Page.js';
|
||||||
@ -55,11 +55,11 @@ export class Target {
|
|||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
_initializedPromise = createDeferredPromise<InitializationStatus>();
|
_initializedDeferred = createDeferred<InitializationStatus>();
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
_isClosedPromise = createDeferredPromise<void>();
|
_isClosedDeferred = createDeferred<void>();
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
@ -198,15 +198,15 @@ export class Target {
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
protected _initialize(): void {
|
protected _initialize(): void {
|
||||||
this._initializedPromise.resolve(InitializationStatus.SUCCESS);
|
this._initializedDeferred.resolve(InitializationStatus.SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
protected _checkIfInitialized(): void {
|
protected _checkIfInitialized(): void {
|
||||||
if (!this._initializedPromise.resolved()) {
|
if (!this._initializedDeferred.resolved()) {
|
||||||
this._initializedPromise.resolve(InitializationStatus.SUCCESS);
|
this._initializedDeferred.resolve(InitializationStatus.SUCCESS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -248,7 +248,7 @@ export class PageTarget extends Target {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected override _initialize(): void {
|
protected override _initialize(): void {
|
||||||
this._initializedPromise
|
this._initializedDeferred
|
||||||
.valueOrThrow()
|
.valueOrThrow()
|
||||||
.then(async result => {
|
.then(async result => {
|
||||||
if (result === InitializationStatus.ABORTED) {
|
if (result === InitializationStatus.ABORTED) {
|
||||||
@ -294,11 +294,11 @@ export class PageTarget extends Target {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override _checkIfInitialized(): void {
|
override _checkIfInitialized(): void {
|
||||||
if (this._initializedPromise.resolved()) {
|
if (this._initializedDeferred.resolved()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (this._getTargetInfo().url !== '') {
|
if (this._getTargetInfo().url !== '') {
|
||||||
this._initializedPromise.resolve(InitializationStatus.SUCCESS);
|
this._initializedDeferred.resolve(InitializationStatus.SUCCESS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
import {ElementHandle} from '../api/ElementHandle.js';
|
import {ElementHandle} from '../api/ElementHandle.js';
|
||||||
import {JSHandle} from '../api/JSHandle.js';
|
import {JSHandle} from '../api/JSHandle.js';
|
||||||
import type {Poller} from '../injected/Poller.js';
|
import type {Poller} from '../injected/Poller.js';
|
||||||
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
import {createDeferred} from '../util/Deferred.js';
|
||||||
import {isErrorLike} from '../util/ErrorLike.js';
|
import {isErrorLike} from '../util/ErrorLike.js';
|
||||||
import {stringifyFunction} from '../util/Function.js';
|
import {stringifyFunction} from '../util/Function.js';
|
||||||
|
|
||||||
@ -49,7 +49,7 @@ export class WaitTask<T = unknown> {
|
|||||||
|
|
||||||
#timeout?: NodeJS.Timeout;
|
#timeout?: NodeJS.Timeout;
|
||||||
|
|
||||||
#result = createDeferredPromise<HandleFor<T>>();
|
#result = createDeferred<HandleFor<T>>();
|
||||||
|
|
||||||
#poller?: JSHandle<Poller<T>>;
|
#poller?: JSHandle<Poller<T>>;
|
||||||
#signal?: AbortSignal;
|
#signal?: AbortSignal;
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
import {Protocol} from 'devtools-protocol';
|
import {Protocol} from 'devtools-protocol';
|
||||||
|
|
||||||
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
import {createDeferred} from '../util/Deferred.js';
|
||||||
|
|
||||||
import {CDPSession} from './Connection.js';
|
import {CDPSession} from './Connection.js';
|
||||||
import {ConsoleMessageType} from './ConsoleMessage.js';
|
import {ConsoleMessageType} from './ConsoleMessage.js';
|
||||||
@ -68,7 +68,7 @@ export type ExceptionThrownCallback = (
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
export class WebWorker extends EventEmitter {
|
export class WebWorker extends EventEmitter {
|
||||||
#executionContext = createDeferredPromise<ExecutionContext>();
|
#executionContext = createDeferred<ExecutionContext>();
|
||||||
|
|
||||||
#client: CDPSession;
|
#client: CDPSession;
|
||||||
#url: string;
|
#url: string;
|
||||||
|
@ -15,10 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {assert} from '../util/assert.js';
|
import {assert} from '../util/assert.js';
|
||||||
import {
|
import {createDeferred, Deferred} from '../util/Deferred.js';
|
||||||
createDeferredPromise,
|
|
||||||
DeferredPromise,
|
|
||||||
} from '../util/DeferredPromise.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
@ -38,17 +35,17 @@ export class MutationPoller<T> implements Poller<T> {
|
|||||||
#root: Node;
|
#root: Node;
|
||||||
|
|
||||||
#observer?: MutationObserver;
|
#observer?: MutationObserver;
|
||||||
#promise?: DeferredPromise<T>;
|
#deferred?: Deferred<T>;
|
||||||
constructor(fn: () => Promise<T>, root: Node) {
|
constructor(fn: () => Promise<T>, root: Node) {
|
||||||
this.#fn = fn;
|
this.#fn = fn;
|
||||||
this.#root = root;
|
this.#root = root;
|
||||||
}
|
}
|
||||||
|
|
||||||
async start(): Promise<void> {
|
async start(): Promise<void> {
|
||||||
const promise = (this.#promise = createDeferredPromise<T>());
|
const deferred = (this.#deferred = createDeferred<T>());
|
||||||
const result = await this.#fn();
|
const result = await this.#fn();
|
||||||
if (result) {
|
if (result) {
|
||||||
promise.resolve(result);
|
deferred.resolve(result);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +54,7 @@ export class MutationPoller<T> implements Poller<T> {
|
|||||||
if (!result) {
|
if (!result) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
promise.resolve(result);
|
deferred.resolve(result);
|
||||||
await this.stop();
|
await this.stop();
|
||||||
});
|
});
|
||||||
this.#observer.observe(this.#root, {
|
this.#observer.observe(this.#root, {
|
||||||
@ -68,9 +65,9 @@ export class MutationPoller<T> implements Poller<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async stop(): Promise<void> {
|
async stop(): Promise<void> {
|
||||||
assert(this.#promise, 'Polling never started.');
|
assert(this.#deferred, 'Polling never started.');
|
||||||
if (!this.#promise.finished()) {
|
if (!this.#deferred.finished()) {
|
||||||
this.#promise.reject(new Error('Polling stopped'));
|
this.#deferred.reject(new Error('Polling stopped'));
|
||||||
}
|
}
|
||||||
if (this.#observer) {
|
if (this.#observer) {
|
||||||
this.#observer.disconnect();
|
this.#observer.disconnect();
|
||||||
@ -79,8 +76,8 @@ export class MutationPoller<T> implements Poller<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result(): Promise<T> {
|
result(): Promise<T> {
|
||||||
assert(this.#promise, 'Polling never started.');
|
assert(this.#deferred, 'Polling never started.');
|
||||||
return this.#promise.valueOrThrow();
|
return this.#deferred.valueOrThrow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,21 +86,21 @@ export class MutationPoller<T> implements Poller<T> {
|
|||||||
*/
|
*/
|
||||||
export class RAFPoller<T> implements Poller<T> {
|
export class RAFPoller<T> implements Poller<T> {
|
||||||
#fn: () => Promise<T>;
|
#fn: () => Promise<T>;
|
||||||
#promise?: DeferredPromise<T>;
|
#deferred?: Deferred<T>;
|
||||||
constructor(fn: () => Promise<T>) {
|
constructor(fn: () => Promise<T>) {
|
||||||
this.#fn = fn;
|
this.#fn = fn;
|
||||||
}
|
}
|
||||||
|
|
||||||
async start(): Promise<void> {
|
async start(): Promise<void> {
|
||||||
const promise = (this.#promise = createDeferredPromise<T>());
|
const deferred = (this.#deferred = createDeferred<T>());
|
||||||
const result = await this.#fn();
|
const result = await this.#fn();
|
||||||
if (result) {
|
if (result) {
|
||||||
promise.resolve(result);
|
deferred.resolve(result);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const poll = async () => {
|
const poll = async () => {
|
||||||
if (promise.finished()) {
|
if (deferred.finished()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const result = await this.#fn();
|
const result = await this.#fn();
|
||||||
@ -111,22 +108,22 @@ export class RAFPoller<T> implements Poller<T> {
|
|||||||
window.requestAnimationFrame(poll);
|
window.requestAnimationFrame(poll);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
promise.resolve(result);
|
deferred.resolve(result);
|
||||||
await this.stop();
|
await this.stop();
|
||||||
};
|
};
|
||||||
window.requestAnimationFrame(poll);
|
window.requestAnimationFrame(poll);
|
||||||
}
|
}
|
||||||
|
|
||||||
async stop(): Promise<void> {
|
async stop(): Promise<void> {
|
||||||
assert(this.#promise, 'Polling never started.');
|
assert(this.#deferred, 'Polling never started.');
|
||||||
if (!this.#promise.finished()) {
|
if (!this.#deferred.finished()) {
|
||||||
this.#promise.reject(new Error('Polling stopped'));
|
this.#deferred.reject(new Error('Polling stopped'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result(): Promise<T> {
|
result(): Promise<T> {
|
||||||
assert(this.#promise, 'Polling never started.');
|
assert(this.#deferred, 'Polling never started.');
|
||||||
return this.#promise.valueOrThrow();
|
return this.#deferred.valueOrThrow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,17 +136,17 @@ export class IntervalPoller<T> implements Poller<T> {
|
|||||||
#ms: number;
|
#ms: number;
|
||||||
|
|
||||||
#interval?: NodeJS.Timer;
|
#interval?: NodeJS.Timer;
|
||||||
#promise?: DeferredPromise<T>;
|
#deferred?: Deferred<T>;
|
||||||
constructor(fn: () => Promise<T>, ms: number) {
|
constructor(fn: () => Promise<T>, ms: number) {
|
||||||
this.#fn = fn;
|
this.#fn = fn;
|
||||||
this.#ms = ms;
|
this.#ms = ms;
|
||||||
}
|
}
|
||||||
|
|
||||||
async start(): Promise<void> {
|
async start(): Promise<void> {
|
||||||
const promise = (this.#promise = createDeferredPromise<T>());
|
const deferred = (this.#deferred = createDeferred<T>());
|
||||||
const result = await this.#fn();
|
const result = await this.#fn();
|
||||||
if (result) {
|
if (result) {
|
||||||
promise.resolve(result);
|
deferred.resolve(result);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,15 +155,15 @@ export class IntervalPoller<T> implements Poller<T> {
|
|||||||
if (!result) {
|
if (!result) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
promise.resolve(result);
|
deferred.resolve(result);
|
||||||
await this.stop();
|
await this.stop();
|
||||||
}, this.#ms);
|
}, this.#ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
async stop(): Promise<void> {
|
async stop(): Promise<void> {
|
||||||
assert(this.#promise, 'Polling never started.');
|
assert(this.#deferred, 'Polling never started.');
|
||||||
if (!this.#promise.finished()) {
|
if (!this.#deferred.finished()) {
|
||||||
this.#promise.reject(new Error('Polling stopped'));
|
this.#deferred.reject(new Error('Polling stopped'));
|
||||||
}
|
}
|
||||||
if (this.#interval) {
|
if (this.#interval) {
|
||||||
clearInterval(this.#interval);
|
clearInterval(this.#interval);
|
||||||
@ -175,7 +172,7 @@ export class IntervalPoller<T> implements Poller<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result(): Promise<T> {
|
result(): Promise<T> {
|
||||||
assert(this.#promise, 'Polling never started.');
|
assert(this.#deferred, 'Polling never started.');
|
||||||
return this.#promise.valueOrThrow();
|
return this.#deferred.valueOrThrow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
import {createDeferred} from '../util/Deferred.js';
|
||||||
import {createFunction} from '../util/Function.js';
|
import {createFunction} from '../util/Function.js';
|
||||||
|
|
||||||
import * as ARIAQuerySelector from './ARIAQuerySelector.js';
|
import * as ARIAQuerySelector from './ARIAQuerySelector.js';
|
||||||
@ -41,7 +41,7 @@ const PuppeteerUtil = Object.freeze({
|
|||||||
...TextQuerySelector,
|
...TextQuerySelector,
|
||||||
...util,
|
...util,
|
||||||
...XPathQuerySelector,
|
...XPathQuerySelector,
|
||||||
createDeferredPromise,
|
createDeferred,
|
||||||
createFunction,
|
createFunction,
|
||||||
createTextContent,
|
createTextContent,
|
||||||
IntervalPoller,
|
IntervalPoller,
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import {DEFERRED_PROMISE_DEBUG_TIMEOUT} from '../environment.js';
|
import {DEFERRED_PROMISE_DEBUG_TIMEOUT} from '../environment.js';
|
||||||
|
|
||||||
import {DeferredPromise, createDeferredPromise} from './DeferredPromise.js';
|
import {Deferred, createDeferred} from './Deferred.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates and returns a deferred promise using DEFERRED_PROMISE_DEBUG_TIMEOUT
|
* Creates and returns a deferred promise using DEFERRED_PROMISE_DEBUG_TIMEOUT
|
||||||
@ -8,14 +8,12 @@ import {DeferredPromise, createDeferredPromise} from './DeferredPromise.js';
|
|||||||
*
|
*
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export function createDebuggableDeferredPromise<T>(
|
export function createDebuggableDeferred<T>(message: string): Deferred<T> {
|
||||||
message: string
|
|
||||||
): DeferredPromise<T> {
|
|
||||||
if (DEFERRED_PROMISE_DEBUG_TIMEOUT > 0) {
|
if (DEFERRED_PROMISE_DEBUG_TIMEOUT > 0) {
|
||||||
return createDeferredPromise({
|
return createDeferred({
|
||||||
message,
|
message,
|
||||||
timeout: DEFERRED_PROMISE_DEBUG_TIMEOUT,
|
timeout: DEFERRED_PROMISE_DEBUG_TIMEOUT,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return createDeferredPromise();
|
return createDeferred();
|
||||||
}
|
}
|
@ -3,7 +3,7 @@ import {TimeoutError} from '../common/Errors.js';
|
|||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export interface DeferredPromise<T> {
|
export interface Deferred<T> {
|
||||||
finished: () => boolean;
|
finished: () => boolean;
|
||||||
resolved: () => boolean;
|
resolved: () => boolean;
|
||||||
resolve: (value: T) => void;
|
resolve: (value: T) => void;
|
||||||
@ -15,7 +15,7 @@ export interface DeferredPromise<T> {
|
|||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export interface DeferredPromiseOptions {
|
export interface DeferredOptions {
|
||||||
message: string;
|
message: string;
|
||||||
timeout: number;
|
timeout: number;
|
||||||
}
|
}
|
||||||
@ -29,9 +29,7 @@ export interface DeferredPromiseOptions {
|
|||||||
*
|
*
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export function createDeferredPromise<T>(
|
export function createDeferred<T>(opts?: DeferredOptions): Deferred<T> {
|
||||||
opts?: DeferredPromiseOptions
|
|
||||||
): DeferredPromise<T> {
|
|
||||||
let isResolved = false;
|
let isResolved = false;
|
||||||
let isRejected = false;
|
let isRejected = false;
|
||||||
let _value: T | Error | undefined;
|
let _value: T | Error | undefined;
|
@ -15,7 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export * from './assert.js';
|
export * from './assert.js';
|
||||||
export * from './DebuggableDeferredPromise.js';
|
export * from './DebuggableDeferred.js';
|
||||||
export * from './DeferredPromise.js';
|
export * from './Deferred.js';
|
||||||
export * from './ErrorLike.js';
|
export * from './ErrorLike.js';
|
||||||
export * from './AsyncIterableUtil.js';
|
export * from './AsyncIterableUtil.js';
|
||||||
|
@ -16,9 +16,9 @@
|
|||||||
|
|
||||||
import expect from 'expect';
|
import expect from 'expect';
|
||||||
import {
|
import {
|
||||||
DeferredPromise,
|
Deferred,
|
||||||
createDeferredPromise,
|
createDeferred,
|
||||||
} from 'puppeteer-core/internal/util/DeferredPromise.js';
|
} from 'puppeteer-core/internal/util/Deferred.js';
|
||||||
|
|
||||||
describe('DeferredPromise', function () {
|
describe('DeferredPromise', function () {
|
||||||
it('should catch errors', async () => {
|
it('should catch errors', async () => {
|
||||||
@ -29,8 +29,8 @@ describe('DeferredPromise', function () {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Async function that fails.
|
// Async function that fails.
|
||||||
function fails(): DeferredPromise<void> {
|
function fails(): Deferred<void> {
|
||||||
const deferred = createDeferredPromise<void>();
|
const deferred = createDeferred<void>();
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
deferred.reject(new Error('test'));
|
deferred.reject(new Error('test'));
|
||||||
}, 25);
|
}, 25);
|
Loading…
Reference in New Issue
Block a user