Skip to content

Commit

Permalink
chore: move non-trivial types out of types.ts (#2680)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Jun 23, 2020
1 parent d0a6e1a commit fca514d
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 183 deletions.
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export { Dialog } from './dialog';
export { Download } from './download';
export { ElementHandle } from './dom';
export { FileChooser } from './fileChooser';
export { Logger } from './types';
export { Logger } from './loggerSink';
export { TimeoutError } from './errors';
export { Frame } from './frames';
export { Keyboard, Mouse } from './input';
Expand Down
3 changes: 2 additions & 1 deletion src/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { Loggers, Logger } from './logger';
import { EventEmitter } from 'events';
import { ProgressController } from './progress';
import { DebugController } from './debug/debugController';
import { LoggerSink } from './loggerSink';

type CommonContextOptions = {
viewport?: types.Size | null,
Expand All @@ -52,7 +53,7 @@ type CommonContextOptions = {

export type PersistentContextOptions = CommonContextOptions;
export type BrowserContextOptions = CommonContextOptions & {
logger?: types.Logger,
logger?: LoggerSink,
};

export interface BrowserContext {
Expand Down
13 changes: 7 additions & 6 deletions src/chromium/crInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
*/

import * as input from '../input';
import * as types from '../types';
import { CRSession } from './crConnection';

function toModifiersMask(modifiers: Set<input.Modifier>): number {
function toModifiersMask(modifiers: Set<types.KeyboardModifier>): number {
let mask = 0;
if (modifiers.has('Alt'))
mask |= 1;
Expand All @@ -38,7 +39,7 @@ export class RawKeyboardImpl implements input.RawKeyboard {
this._client = client;
}

async keydown(modifiers: Set<input.Modifier>, code: string, keyCode: number, keyCodeWithoutLocation: number, key: string, location: number, autoRepeat: boolean, text: string | undefined): Promise<void> {
async keydown(modifiers: Set<types.KeyboardModifier>, code: string, keyCode: number, keyCodeWithoutLocation: number, key: string, location: number, autoRepeat: boolean, text: string | undefined): Promise<void> {
await this._client.send('Input.dispatchKeyEvent', {
type: text ? 'keyDown' : 'rawKeyDown',
modifiers: toModifiersMask(modifiers),
Expand All @@ -53,7 +54,7 @@ export class RawKeyboardImpl implements input.RawKeyboard {
});
}

async keyup(modifiers: Set<input.Modifier>, code: string, keyCode: number, keyCodeWithoutLocation: number, key: string, location: number): Promise<void> {
async keyup(modifiers: Set<types.KeyboardModifier>, code: string, keyCode: number, keyCodeWithoutLocation: number, key: string, location: number): Promise<void> {
await this._client.send('Input.dispatchKeyEvent', {
type: 'keyUp',
modifiers: toModifiersMask(modifiers),
Expand All @@ -76,7 +77,7 @@ export class RawMouseImpl implements input.RawMouse {
this._client = client;
}

async move(x: number, y: number, button: input.Button | 'none', buttons: Set<input.Button>, modifiers: Set<input.Modifier>): Promise<void> {
async move(x: number, y: number, button: types.MouseButton | 'none', buttons: Set<types.MouseButton>, modifiers: Set<types.KeyboardModifier>): Promise<void> {
await this._client.send('Input.dispatchMouseEvent', {
type: 'mouseMoved',
button,
Expand All @@ -86,7 +87,7 @@ export class RawMouseImpl implements input.RawMouse {
});
}

async down(x: number, y: number, button: input.Button, buttons: Set<input.Button>, modifiers: Set<input.Modifier>, clickCount: number): Promise<void> {
async down(x: number, y: number, button: types.MouseButton, buttons: Set<types.MouseButton>, modifiers: Set<types.KeyboardModifier>, clickCount: number): Promise<void> {
await this._client.send('Input.dispatchMouseEvent', {
type: 'mousePressed',
button,
Expand All @@ -97,7 +98,7 @@ export class RawMouseImpl implements input.RawMouse {
});
}

async up(x: number, y: number, button: input.Button, buttons: Set<input.Button>, modifiers: Set<input.Modifier>, clickCount: number): Promise<void> {
async up(x: number, y: number, button: types.MouseButton, buttons: Set<types.MouseButton>, modifiers: Set<types.KeyboardModifier>, clickCount: number): Promise<void> {
await this._client.send('Input.dispatchMouseEvent', {
type: 'mouseReleased',
button,
Expand Down
6 changes: 3 additions & 3 deletions src/debug/recorderController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { Writable } from 'stream';
import { BrowserContextBase } from '../browserContext';
import * as dom from '../dom';
import * as types from '../types';
import { Events } from '../events';
import * as frames from '../frames';
import { Page } from '../page';
Expand Down Expand Up @@ -109,12 +109,12 @@ export class RecorderController {
}
}

export function toClickOptions(action: actions.ClickAction): { method: 'click' | 'dblclick', options: dom.ClickOptions } {
export function toClickOptions(action: actions.ClickAction): { method: 'click' | 'dblclick', options: types.MouseClickOptions } {
let method: 'click' | 'dblclick' = 'click';
if (action.clickCount === 2)
method = 'dblclick';
const modifiers = toModifiers(action.modifiers);
const options: dom.ClickOptions = {};
const options: types.MouseClickOptions = {};
if (action.button !== 'left')
options.button = action.button;
if (modifiers.length)
Expand Down
4 changes: 2 additions & 2 deletions src/debug/terminalOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { Writable } from 'stream';
import * as dom from '../dom';
import * as types from '../types';
import { Frame } from '../frames';
import { formatColors, Formatter } from '../utils/formatter';
import { Action, actionTitle, NavigationSignal, PopupSignal, Signal } from './recorderActions';
Expand Down Expand Up @@ -145,7 +145,7 @@ export class TerminalOutput {
if (action.clickCount === 2)
method = 'dblclick';
const modifiers = toModifiers(action.modifiers);
const options: dom.ClickOptions = {};
const options: types.MouseClickOptions = {};
if (action.button !== 'left')
options.button = action.button;
if (modifiers.length)
Expand Down
56 changes: 23 additions & 33 deletions src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,13 @@ import { assert, helper } from './helper';
import InjectedScript from './injected/injectedScript';
import * as injectedScriptSource from './generated/injectedScriptSource';
import * as debugScriptSource from './generated/debugScriptSource';
import * as input from './input';
import * as js from './javascript';
import { Page } from './page';
import { selectors } from './selectors';
import * as types from './types';
import { Progress, ProgressController } from './progress';
import DebugScript from './debug/injected/debugScript';

export type PointerActionOptions = {
modifiers?: input.Modifier[];
position?: types.Point;
};

export type ClickOptions = PointerActionOptions & input.MouseClickOptions;

export type MultiClickOptions = PointerActionOptions & input.MouseMultiClickOptions;

export class FrameExecutionContext extends js.ExecutionContext {
readonly frame: frames.Frame;
private _injectedScriptPromise?: Promise<js.JSHandle>;
Expand All @@ -56,16 +46,16 @@ export class FrameExecutionContext extends js.ExecutionContext {
return null;
}

async evaluateInternal<R>(pageFunction: types.Func0<R>): Promise<R>;
async evaluateInternal<Arg, R>(pageFunction: types.Func1<Arg, R>, arg: Arg): Promise<R>;
async evaluateInternal<R>(pageFunction: js.Func0<R>): Promise<R>;
async evaluateInternal<Arg, R>(pageFunction: js.Func1<Arg, R>, arg: Arg): Promise<R>;
async evaluateInternal(pageFunction: never, ...args: never[]): Promise<any> {
return await this.frame._page._frameManager.waitForSignalsCreatedBy(null, false /* noWaitFor */, async () => {
return js.evaluate(this, true /* returnByValue */, pageFunction, ...args);
});
}

async evaluateHandleInternal<R>(pageFunction: types.Func0<R>): Promise<types.SmartHandle<R>>;
async evaluateHandleInternal<Arg, R>(pageFunction: types.Func1<Arg, R>, arg: Arg): Promise<types.SmartHandle<R>>;
async evaluateHandleInternal<R>(pageFunction: js.Func0<R>): Promise<js.SmartHandle<R>>;
async evaluateHandleInternal<Arg, R>(pageFunction: js.Func1<Arg, R>, arg: Arg): Promise<js.SmartHandle<R>>;
async evaluateHandleInternal(pageFunction: never, ...args: never[]): Promise<any> {
return await this.frame._page._frameManager.waitForSignalsCreatedBy(null, false /* noWaitFor */, async () => {
return js.evaluate(this, false /* returnByValue */, pageFunction, ...args);
Expand Down Expand Up @@ -133,17 +123,17 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
return this;
}

async _evaluateInMain<R, Arg>(pageFunction: types.Func1<[js.JSHandle<InjectedScript>, ElementHandle<T>, Arg], R>, arg: Arg): Promise<R> {
async _evaluateInMain<R, Arg>(pageFunction: js.Func1<[js.JSHandle<InjectedScript>, ElementHandle<T>, Arg], R>, arg: Arg): Promise<R> {
const main = await this._context.frame._mainContext();
return main.evaluateInternal(pageFunction, [await main.injectedScript(), this, arg]);
}

async _evaluateInUtility<R, Arg>(pageFunction: types.Func1<[js.JSHandle<InjectedScript>, ElementHandle<T>, Arg], R>, arg: Arg): Promise<R> {
async _evaluateInUtility<R, Arg>(pageFunction: js.Func1<[js.JSHandle<InjectedScript>, ElementHandle<T>, Arg], R>, arg: Arg): Promise<R> {
const utility = await this._context.frame._utilityContext();
return utility.evaluateInternal(pageFunction, [await utility.injectedScript(), this, arg]);
}

async _evaluateHandleInUtility<R, Arg>(pageFunction: types.Func1<[js.JSHandle<InjectedScript>, ElementHandle<T>, Arg], R>, arg: Arg): Promise<js.JSHandle<R>> {
async _evaluateHandleInUtility<R, Arg>(pageFunction: js.Func1<[js.JSHandle<InjectedScript>, ElementHandle<T>, Arg], R>, arg: Arg): Promise<js.JSHandle<R>> {
const utility = await this._context.frame._utilityContext();
return utility.evaluateHandleInternal(pageFunction, [await utility.injectedScript(), this, arg]);
}
Expand Down Expand Up @@ -290,7 +280,7 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
};
}

async _retryPointerAction(progress: Progress, action: (point: types.Point) => Promise<void>, options: PointerActionOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'notconnected' | 'done'> {
async _retryPointerAction(progress: Progress, action: (point: types.Point) => Promise<void>, options: types.PointerActionOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'notconnected' | 'done'> {
let first = true;
while (progress.isRunning()) {
progress.logger.info(`${first ? 'attempting' : 'retrying'} ${progress.apiName} action`);
Expand Down Expand Up @@ -321,7 +311,7 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
return 'done';
}

async _performPointerAction(progress: Progress, action: (point: types.Point) => Promise<void>, options: PointerActionOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'notvisible' | 'notconnected' | 'notinviewport' | 'nothittarget' | 'done'> {
async _performPointerAction(progress: Progress, action: (point: types.Point) => Promise<void>, options: types.PointerActionOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'notvisible' | 'notconnected' | 'notinviewport' | 'nothittarget' | 'done'> {
const { force = false, position } = options;
if (!force) {
const result = await this._waitForDisplayedAtStablePositionAndEnabled(progress);
Expand Down Expand Up @@ -363,7 +353,7 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
if ((options as any).__testHookBeforePointerAction)
await (options as any).__testHookBeforePointerAction();
progress.throwIfAborted(); // Avoid action that has side-effects.
let restoreModifiers: input.Modifier[] | undefined;
let restoreModifiers: types.KeyboardModifier[] | undefined;
if (options && options.modifiers)
restoreModifiers = await this._page.keyboard._ensureModifiers(options.modifiers);
progress.logger.info(` performing ${progress.apiName} action`);
Expand All @@ -380,33 +370,33 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
return 'done';
}

hover(options: PointerActionOptions & types.PointerActionWaitOptions = {}): Promise<void> {
hover(options: types.PointerActionOptions & types.PointerActionWaitOptions = {}): Promise<void> {
return this._runAbortableTask(async progress => {
throwIfNotConnected(await this._hover(progress, options));
}, this._page._timeoutSettings.timeout(options), 'hover');
}

_hover(progress: Progress, options: PointerActionOptions & types.PointerActionWaitOptions): Promise<'notconnected' | 'done'> {
_hover(progress: Progress, options: types.PointerActionOptions & types.PointerActionWaitOptions): Promise<'notconnected' | 'done'> {
return this._retryPointerAction(progress, point => this._page.mouse.move(point.x, point.y), options);
}

click(options: ClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions = {}): Promise<void> {
click(options: types.MouseClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions = {}): Promise<void> {
return this._runAbortableTask(async progress => {
throwIfNotConnected(await this._click(progress, options));
}, this._page._timeoutSettings.timeout(options), 'click');
}

_click(progress: Progress, options: ClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'notconnected' | 'done'> {
_click(progress: Progress, options: types.MouseClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'notconnected' | 'done'> {
return this._retryPointerAction(progress, point => this._page.mouse.click(point.x, point.y, options), options);
}

dblclick(options: MultiClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions = {}): Promise<void> {
dblclick(options: types.MouseMultiClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions = {}): Promise<void> {
return this._runAbortableTask(async progress => {
throwIfNotConnected(await this._dblclick(progress, options));
}, this._page._timeoutSettings.timeout(options), 'dblclick');
}

_dblclick(progress: Progress, options: MultiClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'notconnected' | 'done'> {
_dblclick(progress: Progress, options: types.MouseMultiClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions): Promise<'notconnected' | 'done'> {
return this._retryPointerAction(progress, point => this._page.mouse.dblclick(point.x, point.y, options), options);
}

Expand Down Expand Up @@ -607,9 +597,9 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
return selectors._queryAll(this._context.frame, selector, this);
}

async $eval<R, Arg>(selector: string, pageFunction: types.FuncOn<Element, Arg, R>, arg: Arg): Promise<R>;
async $eval<R>(selector: string, pageFunction: types.FuncOn<Element, void, R>, arg?: any): Promise<R>;
async $eval<R, Arg>(selector: string, pageFunction: types.FuncOn<Element, Arg, R>, arg: Arg): Promise<R> {
async $eval<R, Arg>(selector: string, pageFunction: js.FuncOn<Element, Arg, R>, arg: Arg): Promise<R>;
async $eval<R>(selector: string, pageFunction: js.FuncOn<Element, void, R>, arg?: any): Promise<R>;
async $eval<R, Arg>(selector: string, pageFunction: js.FuncOn<Element, Arg, R>, arg: Arg): Promise<R> {
const handle = await selectors._query(this._context.frame, selector, this);
if (!handle)
throw new Error(`Error: failed to find element matching selector "${selector}"`);
Expand All @@ -618,9 +608,9 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
return result;
}

async $$eval<R, Arg>(selector: string, pageFunction: types.FuncOn<Element[], Arg, R>, arg: Arg): Promise<R>;
async $$eval<R>(selector: string, pageFunction: types.FuncOn<Element[], void, R>, arg?: any): Promise<R>;
async $$eval<R, Arg>(selector: string, pageFunction: types.FuncOn<Element[], Arg, R>, arg: Arg): Promise<R> {
async $$eval<R, Arg>(selector: string, pageFunction: js.FuncOn<Element[], Arg, R>, arg: Arg): Promise<R>;
async $$eval<R>(selector: string, pageFunction: js.FuncOn<Element[], void, R>, arg?: any): Promise<R>;
async $$eval<R, Arg>(selector: string, pageFunction: js.FuncOn<Element[], Arg, R>, arg: Arg): Promise<R> {
const arrayHandle = await selectors._queryArray(this._context.frame, selector, this);
const result = await arrayHandle.evaluate(pageFunction, arg);
arrayHandle.dispose();
Expand Down Expand Up @@ -686,7 +676,7 @@ export class InjectedScriptPollHandler<T> {
});
}

async finishHandle(): Promise<types.SmartHandle<T>> {
async finishHandle(): Promise<js.SmartHandle<T>> {
try {
const result = await this._poll!.evaluateHandle(poll => poll.result);
await this._finishInternal();
Expand Down
Loading

0 comments on commit fca514d

Please sign in to comment.