refactor: use deferred promise (#10044)

This commit is contained in:
Nikolay Vitkov 2023-04-19 15:28:38 +02:00 committed by GitHub
parent c12512822a
commit 5547e43829
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 29 deletions

View File

@ -33,6 +33,7 @@ import {
import {BrowserContext} from '../api/BrowserContext.js';
import {Page} from '../api/Page.js';
import {assert} from '../util/assert.js';
import {createDeferredPromise} from '../util/DeferredPromise.js';
import {ChromeTargetManager} from './ChromeTargetManager.js';
import {CDPSession, Connection, ConnectionEmittedEvents} from './Connection.js';
@ -478,11 +479,8 @@ export class CDPBrowser extends BrowserBase {
options: WaitForTargetOptions = {}
): Promise<Target> {
const {timeout = 30000} = options;
let resolve: (value: Target | PromiseLike<Target>) => void;
let isResolved = false;
const targetPromise = new Promise<Target>(x => {
return (resolve = x);
});
const targetPromise = createDeferredPromise<Target | PromiseLike<Target>>();
this.on(BrowserEmittedEvents.TargetCreated, check);
this.on(BrowserEmittedEvents.TargetChanged, check);
try {
@ -497,9 +495,8 @@ export class CDPBrowser extends BrowserBase {
}
async function check(target: Target): Promise<void> {
if ((await predicate(target)) && !isResolved) {
isResolved = true;
resolve(target);
if ((await predicate(target)) && !targetPromise.resolved()) {
targetPromise.resolve(target);
}
}
}

View File

@ -18,6 +18,7 @@ import {Protocol} from 'devtools-protocol';
import {TargetFilterCallback} from '../api/Browser.js';
import {assert} from '../util/assert.js';
import {createDeferredPromise} from '../util/DeferredPromise.js';
import {CDPSession, Connection} from './Connection.js';
import {EventEmitter} from './EventEmitter.js';
@ -80,10 +81,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
(event: Protocol.Target.DetachedFromTargetEvent) => void
> = new WeakMap();
#initializeCallback = () => {};
#initializePromise: Promise<void> = new Promise(resolve => {
this.#initializeCallback = resolve;
});
#initializePromise = createDeferredPromise<void>();
#targetsIdsForInit: Set<string> = new Set();
constructor(
@ -381,7 +379,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
#finishInitializationIfReady(targetId?: string): void {
targetId !== undefined && this.#targetsIdsForInit.delete(targetId);
if (this.#targetsIdsForInit.size === 0) {
this.#initializeCallback();
this.#initializePromise.resolve();
}
}

View File

@ -18,6 +18,7 @@ import {Protocol} from 'devtools-protocol';
import {TargetFilterCallback} from '../api/Browser.js';
import {assert} from '../util/assert.js';
import {createDeferredPromise} from '../util/DeferredPromise.js';
import {CDPSession, Connection} from './Connection.js';
import {EventEmitter} from './EventEmitter.js';
@ -87,10 +88,7 @@ export class FirefoxTargetManager
(event: Protocol.Target.AttachedToTargetEvent) => Promise<void>
> = new WeakMap();
#initializeCallback = () => {};
#initializePromise: Promise<void> = new Promise(resolve => {
this.#initializeCallback = resolve;
});
#initializePromise = createDeferredPromise<void>();
#targetsIdsForInit: Set<string> = new Set();
constructor(
@ -255,7 +253,7 @@ export class FirefoxTargetManager
#finishInitializationIfReady(targetId: string): void {
this.#targetsIdsForInit.delete(targetId);
if (this.#targetsIdsForInit.size === 0) {
this.#initializeCallback();
this.#initializePromise.resolve();
}
}
}

View File

@ -798,17 +798,7 @@ export class Frame {
}
if (path) {
let fs;
try {
fs = (await import('fs')).promises;
} catch (error) {
if (error instanceof TypeError) {
throw new Error(
'Can only pass a file path in a Node-like environment.'
);
}
throw error;
}
const fs = await importFSPromises();
content = await fs.readFile(path, 'utf8');
content += `//# sourceURL=${path.replace(/\n/g, '')}`;
}