From 793a37183750b85ee71c8a6ac9d24f8e4ac0dff7 Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Mon, 5 Jun 2023 13:34:53 +0200 Subject: [PATCH] refactor: Tracing uses deferred (#10315) --- packages/puppeteer-core/src/common/Tracing.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/puppeteer-core/src/common/Tracing.ts b/packages/puppeteer-core/src/common/Tracing.ts index e76f3a15..18de4abb 100644 --- a/packages/puppeteer-core/src/common/Tracing.ts +++ b/packages/puppeteer-core/src/common/Tracing.ts @@ -14,6 +14,7 @@ * limitations under the License. */ import {assert} from '../util/assert.js'; +import {Deferred} from '../util/Deferred.js'; import {isErrorLike} from '../util/ErrorLike.js'; import {CDPSession} from './Connection.js'; @@ -115,12 +116,7 @@ export class Tracing { * @returns Promise which resolves to buffer with trace data. */ async stop(): Promise { - let resolve: (value: Buffer | undefined) => void; - let reject: (err: Error) => void; - const contentPromise = new Promise((x, y) => { - resolve = x; - reject = y; - }); + const contentDeferred = Deferred.create(); this.#client.once('Tracing.tracingComplete', async event => { try { const readable = await getReadableFromProtocolStream( @@ -128,17 +124,17 @@ export class Tracing { event.stream ); const buffer = await getReadableAsBuffer(readable, this.#path); - resolve(buffer ?? undefined); + contentDeferred.resolve(buffer ?? undefined); } catch (error) { if (isErrorLike(error)) { - reject(error); + contentDeferred.reject(error); } else { - reject(new Error(`Unknown error: ${error}`)); + contentDeferred.reject(new Error(`Unknown error: ${error}`)); } } }); await this.#client.send('Tracing.end'); this.#recording = false; - return contentPromise; + return contentDeferred.valueOrThrow(); } }