mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
refactor: prefer use of DeferPromise (#9969)
This commit is contained in:
parent
c8f6adf9f3
commit
0b1e20cf5b
@ -192,26 +192,6 @@ export class HTTPRequest extends BaseHTTPRequest {
|
||||
return this._redirectChain.slice();
|
||||
}
|
||||
|
||||
/**
|
||||
* Access information about the request's failure.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* Example of logging all failed requests:
|
||||
*
|
||||
* ```ts
|
||||
* page.on('requestfailed', request => {
|
||||
* console.log(request.url() + ' ' + request.failure().errorText);
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* @returns `null` unless the request failed. If the request fails this can
|
||||
* return an object with `errorText` containing a human-readable error
|
||||
* message, e.g. `net::ERR_FAILED`. It is not guaranteed that there will be
|
||||
* failure text if the request fails.
|
||||
*/
|
||||
override failure(): {errorText: string} | null {
|
||||
if (!this._failureText) {
|
||||
return null;
|
||||
@ -221,35 +201,6 @@ export class HTTPRequest extends BaseHTTPRequest {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Continues request with optional request overrides.
|
||||
*
|
||||
* @remarks
|
||||
*
|
||||
* To use this, request
|
||||
* interception should be enabled with {@link Page.setRequestInterception}.
|
||||
*
|
||||
* Exception is immediately thrown if the request interception is not enabled.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* ```ts
|
||||
* await page.setRequestInterception(true);
|
||||
* page.on('request', request => {
|
||||
* // Override headers
|
||||
* const headers = Object.assign({}, request.headers(), {
|
||||
* foo: 'bar', // set "foo" header
|
||||
* origin: undefined, // remove "origin" header
|
||||
* });
|
||||
* request.continue({headers});
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* @param overrides - optional overrides to apply to the request.
|
||||
* @param priority - If provided, intercept is resolved using
|
||||
* cooperative handling rules. Otherwise, intercept is resolved
|
||||
* immediately.
|
||||
*/
|
||||
override async continue(
|
||||
overrides: ContinueRequestOverrides = {},
|
||||
priority?: number
|
||||
|
@ -19,6 +19,7 @@ import {
|
||||
HTTPResponse as BaseHTTPResponse,
|
||||
RemoteAddress,
|
||||
} from '../api/HTTPResponse.js';
|
||||
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
||||
|
||||
import {CDPSession} from './Connection.js';
|
||||
import {ProtocolError} from './Errors.js';
|
||||
@ -33,8 +34,7 @@ export class HTTPResponse extends BaseHTTPResponse {
|
||||
#client: CDPSession;
|
||||
#request: HTTPRequest;
|
||||
#contentPromise: Promise<Buffer> | null = null;
|
||||
#bodyLoadedPromise: Promise<Error | void>;
|
||||
#bodyLoadedPromiseFulfill: (err: Error | void) => void = () => {};
|
||||
#bodyLoadedPromise = createDeferredPromise<Error | void>();
|
||||
#remoteAddress: RemoteAddress;
|
||||
#status: number;
|
||||
#statusText: string;
|
||||
@ -55,10 +55,6 @@ export class HTTPResponse extends BaseHTTPResponse {
|
||||
this.#client = client;
|
||||
this.#request = request;
|
||||
|
||||
this.#bodyLoadedPromise = new Promise(fulfill => {
|
||||
this.#bodyLoadedPromiseFulfill = fulfill;
|
||||
});
|
||||
|
||||
this.#remoteAddress = {
|
||||
ip: responsePayload.remoteIPAddress,
|
||||
port: responsePayload.remotePort,
|
||||
@ -105,9 +101,9 @@ export class HTTPResponse extends BaseHTTPResponse {
|
||||
|
||||
override _resolveBody(err: Error | null): void {
|
||||
if (err) {
|
||||
return this.#bodyLoadedPromiseFulfill(err);
|
||||
return this.#bodyLoadedPromise.resolve(err);
|
||||
}
|
||||
return this.#bodyLoadedPromiseFulfill();
|
||||
return this.#bodyLoadedPromise.resolve();
|
||||
}
|
||||
|
||||
override remoteAddress(): RemoteAddress {
|
||||
|
@ -74,27 +74,10 @@ export class LifecycleWatcher {
|
||||
#eventListeners: PuppeteerEventListener[];
|
||||
#initialLoaderId: string;
|
||||
|
||||
#sameDocumentNavigationCompleteCallback: (x?: Error) => void = noop;
|
||||
#sameDocumentNavigationPromise = new Promise<Error | undefined>(fulfill => {
|
||||
this.#sameDocumentNavigationCompleteCallback = fulfill;
|
||||
});
|
||||
|
||||
#lifecycleCallback: () => void = noop;
|
||||
#lifecyclePromise: Promise<void> = new Promise(fulfill => {
|
||||
this.#lifecycleCallback = fulfill;
|
||||
});
|
||||
|
||||
#newDocumentNavigationCompleteCallback: (x?: Error) => void = noop;
|
||||
#newDocumentNavigationPromise: Promise<Error | undefined> = new Promise(
|
||||
fulfill => {
|
||||
this.#newDocumentNavigationCompleteCallback = fulfill;
|
||||
}
|
||||
);
|
||||
|
||||
#terminationCallback: (x?: Error) => void = noop;
|
||||
#terminationPromise: Promise<Error | undefined> = new Promise(fulfill => {
|
||||
this.#terminationCallback = fulfill;
|
||||
});
|
||||
#sameDocumentNavigationPromise = createDeferredPromise<Error | undefined>();
|
||||
#lifecyclePromise = createDeferredPromise<void>();
|
||||
#newDocumentNavigationPromise = createDeferredPromise<Error | undefined>();
|
||||
#terminationPromise = createDeferredPromise<Error | undefined>();
|
||||
|
||||
#timeoutPromise: Promise<TimeoutError | undefined>;
|
||||
|
||||
@ -211,8 +194,7 @@ export class LifecycleWatcher {
|
||||
|
||||
#onFrameDetached(frame: Frame): void {
|
||||
if (this.#frame === frame) {
|
||||
this.#terminationCallback.call(
|
||||
null,
|
||||
this.#terminationPromise.resolve(
|
||||
new Error('Navigating frame was detached')
|
||||
);
|
||||
return;
|
||||
@ -227,7 +209,7 @@ export class LifecycleWatcher {
|
||||
}
|
||||
|
||||
#terminate(error: Error): void {
|
||||
this.#terminationCallback.call(null, error);
|
||||
this.#terminationPromise.resolve(error);
|
||||
}
|
||||
|
||||
sameDocumentNavigationPromise(): Promise<Error | undefined> {
|
||||
@ -286,12 +268,12 @@ export class LifecycleWatcher {
|
||||
if (!checkLifecycle(this.#frame, this.#expectedLifecycle)) {
|
||||
return;
|
||||
}
|
||||
this.#lifecycleCallback();
|
||||
this.#lifecyclePromise.resolve();
|
||||
if (this.#hasSameDocumentNavigation) {
|
||||
this.#sameDocumentNavigationCompleteCallback();
|
||||
this.#sameDocumentNavigationPromise.resolve(undefined);
|
||||
}
|
||||
if (this.#swapped || this.#frame._loaderId !== this.#initialLoaderId) {
|
||||
this.#newDocumentNavigationCompleteCallback();
|
||||
this.#newDocumentNavigationPromise.resolve(undefined);
|
||||
}
|
||||
|
||||
function checkLifecycle(
|
||||
|
Loading…
Reference in New Issue
Block a user