refactor: Tracing uses deferred (#10315)

This commit is contained in:
Nikolay Vitkov 2023-06-05 13:34:53 +02:00 committed by GitHub
parent 8aa441a9e5
commit 793a371837
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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();
} }
} }