Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(waitForEvent): refactor waitForEvent into a single implementation #1602

Merged
merged 2 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions src/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import { helper } from './helper';
import * as network from './network';
import { Page, PageBinding } from './page';
import * as platform from './platform';
import { TimeoutSettings } from './timeoutSettings';
import * as types from './types';
import { Events } from './events';
import { ExtendedEventEmitter } from './extendedEventEmitter';

export type BrowserContextOptions = {
viewport?: types.Size | null,
Expand Down Expand Up @@ -62,7 +62,7 @@ export interface BrowserContext {
close(): Promise<void>;
}

export abstract class BrowserContextBase extends platform.EventEmitter implements BrowserContext {
export abstract class BrowserContextBase extends ExtendedEventEmitter implements BrowserContext {
readonly _timeoutSettings = new TimeoutSettings();
readonly _pageBindings = new Map<string, PageBinding>();
readonly _options: BrowserContextOptions;
Expand All @@ -73,7 +73,10 @@ export abstract class BrowserContextBase extends platform.EventEmitter implement
readonly _permissions = new Map<string, string[]>();

constructor(options: BrowserContextOptions) {
super();
super({
timeoutGetter: event => this._timeoutSettings.timeout(),
abortGetter: event => event === Events.BrowserContext.Close ? new Promise<Error>(() => void 0) : this._closePromise,
});
this._options = options;
this._closePromise = new Promise(fulfill => this._closePromiseFulfill = fulfill);
}
Expand Down Expand Up @@ -132,17 +135,6 @@ export abstract class BrowserContextBase extends platform.EventEmitter implement
setDefaultTimeout(timeout: number) {
this._timeoutSettings.setDefaultTimeout(timeout);
}

async waitForEvent(event: string, optionsOrPredicate?: Function | (types.TimeoutOptions & { predicate?: Function })): Promise<any> {
if (!optionsOrPredicate)
optionsOrPredicate = {};
if (typeof optionsOrPredicate === 'function')
optionsOrPredicate = { predicate: optionsOrPredicate };
const { timeout = this._timeoutSettings.timeout(), predicate = () => true } = optionsOrPredicate;

const abortPromise = (event === Events.BrowserContext.Close) ? new Promise<Error>(() => { }) : this._closePromise;
return helper.waitForEvent(this, event, (...args: any[]) => !!predicate(...args), timeout, abortPromise);
}
}

export function assertBrowserContextIsNotOwned(context: BrowserContextBase) {
Expand Down
24 changes: 24 additions & 0 deletions src/extendedEventEmitter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { EventEmitter } from './platform';
JoelEinbinder marked this conversation as resolved.
Show resolved Hide resolved
import { helper } from './helper';

export class ExtendedEventEmitter extends EventEmitter {
private _timeoutGetter: (event: string) => number;
private _abortGetter: (event: string) => Promise<Error>;
constructor(options: {timeoutGetter: (event: string) => number, abortGetter?: (event: string) => Promise<Error>}) {
super();
const {
timeoutGetter,
abortGetter = () => new Promise<Error>(() => void 0)
} = options;
this._timeoutGetter = timeoutGetter;
this._abortGetter = abortGetter;
}

async waitForEvent(event: string, optionsOrPredicate: Function|{ predicate?: Function, timeout?: number } = {}): Promise<any> {
const {
predicate = () => true,
timeout = this._timeoutGetter(event)
} = typeof optionsOrPredicate === 'function' ? {predicate: optionsOrPredicate} : optionsOrPredicate;
return helper.waitForEvent(this, event, predicate, timeout, this._abortGetter(event));
}
}
15 changes: 6 additions & 9 deletions src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { BrowserContext, BrowserContextBase } from './browserContext';
import { ConsoleMessage, ConsoleMessageLocation } from './console';
import * as accessibility from './accessibility';
import * as platform from './platform';
import { ExtendedEventEmitter } from './extendedEventEmitter';

export interface PageDelegate {
readonly rawMouse: input.RawMouse;
Expand Down Expand Up @@ -87,7 +88,7 @@ export type FileChooser = {
multiple: boolean
};

export class Page extends platform.EventEmitter {
export class Page extends ExtendedEventEmitter {
private _closed = false;
private _closedCallback: () => void;
private _closedPromise: Promise<void>;
Expand All @@ -111,7 +112,10 @@ export class Page extends platform.EventEmitter {
_ownedContext: BrowserContext | undefined;

constructor(delegate: PageDelegate, browserContext: BrowserContextBase) {
super();
super({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we already inherit, perhaps use overridden methods instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

timeoutGetter: event => this._timeoutSettings.timeout(),
abortGetter: event => this._disconnectedPromise,
});
this._delegate = delegate;
this._closedCallback = () => {};
this._closedPromise = new Promise(f => this._closedCallback = f);
Expand Down Expand Up @@ -303,13 +307,6 @@ export class Page extends platform.EventEmitter {
return this.mainFrame().waitForNavigation(options);
}

async waitForEvent(event: string, optionsOrPredicate: Function | (types.TimeoutOptions & { predicate?: Function }) = {}): Promise<any> {
if (typeof optionsOrPredicate === 'function')
optionsOrPredicate = { predicate: optionsOrPredicate };
const { timeout = this._timeoutSettings.timeout(), predicate = () => true } = optionsOrPredicate;
return helper.waitForEvent(this, event, (...args: any[]) => !!predicate(...args), timeout, this._disconnectedPromise);
}

async waitForRequest(urlOrPredicate: string | RegExp | ((r: network.Request) => boolean), options: types.TimeoutOptions = {}): Promise<network.Request> {
const { timeout = this._timeoutSettings.timeout() } = options;
return helper.waitForEvent(this, Events.Page.Request, (request: network.Request) => {
Expand Down