mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
refactor: Tracing uses deferred (#10315)
This commit is contained in:
parent
8aa441a9e5
commit
793a371837
@ -14,6 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
import {assert} from '../util/assert.js';
|
import {assert} from '../util/assert.js';
|
||||||
|
import {Deferred} from '../util/Deferred.js';
|
||||||
import {isErrorLike} from '../util/ErrorLike.js';
|
import {isErrorLike} from '../util/ErrorLike.js';
|
||||||
|
|
||||||
import {CDPSession} from './Connection.js';
|
import {CDPSession} from './Connection.js';
|
||||||
@ -115,12 +116,7 @@ export class Tracing {
|
|||||||
* @returns Promise which resolves to buffer with trace data.
|
* @returns Promise which resolves to buffer with trace data.
|
||||||
*/
|
*/
|
||||||
async stop(): Promise<Buffer | undefined> {
|
async stop(): Promise<Buffer | undefined> {
|
||||||
let resolve: (value: Buffer | undefined) => void;
|
const contentDeferred = Deferred.create<Buffer | undefined>();
|
||||||
let reject: (err: Error) => void;
|
|
||||||
const contentPromise = new Promise<Buffer | undefined>((x, y) => {
|
|
||||||
resolve = x;
|
|
||||||
reject = y;
|
|
||||||
});
|
|
||||||
this.#client.once('Tracing.tracingComplete', async event => {
|
this.#client.once('Tracing.tracingComplete', async event => {
|
||||||
try {
|
try {
|
||||||
const readable = await getReadableFromProtocolStream(
|
const readable = await getReadableFromProtocolStream(
|
||||||
@ -128,17 +124,17 @@ export class Tracing {
|
|||||||
event.stream
|
event.stream
|
||||||
);
|
);
|
||||||
const buffer = await getReadableAsBuffer(readable, this.#path);
|
const buffer = await getReadableAsBuffer(readable, this.#path);
|
||||||
resolve(buffer ?? undefined);
|
contentDeferred.resolve(buffer ?? undefined);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (isErrorLike(error)) {
|
if (isErrorLike(error)) {
|
||||||
reject(error);
|
contentDeferred.reject(error);
|
||||||
} else {
|
} else {
|
||||||
reject(new Error(`Unknown error: ${error}`));
|
contentDeferred.reject(new Error(`Unknown error: ${error}`));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
await this.#client.send('Tracing.end');
|
await this.#client.send('Tracing.end');
|
||||||
this.#recording = false;
|
this.#recording = false;
|
||||||
return contentPromise;
|
return contentDeferred.valueOrThrow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user