refactor: Tracing uses deferred (#10315)
This commit is contained in:
parent
8aa441a9e5
commit
793a371837
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user