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.
*/
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<Buffer | undefined> {
let resolve: (value: Buffer | undefined) => void;
let reject: (err: Error) => void;
const contentPromise = new Promise<Buffer | undefined>((x, y) => {
resolve = x;
reject = y;
});
const contentDeferred = Deferred.create<Buffer | undefined>();
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();
}
}