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: migrate src/Worker to typescript #5715

Merged
merged 1 commit into from Apr 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 26 additions & 42 deletions src/Worker.js → src/Worker.ts
Expand Up @@ -13,71 +13,55 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const EventEmitter = require('events');
const {debugError} = require('./helper');
const {ExecutionContext} = require('./ExecutionContext');
const {JSHandle} = require('./JSHandle');
// Used as a TypeDef
// eslint-disable-next-line no-unused-vars
const {CDPSession} = require('./Connection');
import {EventEmitter} from 'events';
import {debugError} from './helper';
import {ExecutionContext} from './ExecutionContext';
import {JSHandle} from './JSHandle';
import {CDPSession} from './Connection';

class Worker extends EventEmitter {
/**
* @param {CDPSession} client
* @param {string} url
* @param {function(string, !Array<!JSHandle>, Protocol.Runtime.StackTrace=):void} consoleAPICalled
* @param {function(!Protocol.Runtime.ExceptionDetails):void} exceptionThrown
*/
constructor(client, url, consoleAPICalled, exceptionThrown) {
type ConsoleAPICalledCallback = (eventType: string, handles: JSHandle[], trace: Protocol.Runtime.StackTrace) => void;
type ExceptionThrownCallback = (details: Protocol.Runtime.ExceptionDetails) => void;
type JSHandleFactory = (obj: Protocol.Runtime.RemoteObject) => JSHandle;

export class Worker extends EventEmitter {
_client: CDPSession;
_url: string;
_executionContextPromise: Promise<ExecutionContext>;
_executionContextCallback: (value: ExecutionContext) => void;

constructor(client: CDPSession, url: string, consoleAPICalled: ConsoleAPICalledCallback, exceptionThrown: ExceptionThrownCallback) {
super();
this._client = client;
this._url = url;
this._executionContextPromise = new Promise(x => this._executionContextCallback = x);
/** @type {function(!Protocol.Runtime.RemoteObject):!JSHandle} */
let jsHandleFactory;
this._executionContextPromise = new Promise<ExecutionContext>(x => this._executionContextCallback = x);

let jsHandleFactory: JSHandleFactory;
this._client.once('Runtime.executionContextCreated', async event => {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
jsHandleFactory = remoteObject => new JSHandle(executionContext, client, remoteObject);
const executionContext = new ExecutionContext(client, event.context, null);
this._executionContextCallback(executionContext);
});

// This might fail if the target is closed before we recieve all execution contexts.
this._client.send('Runtime.enable', {}).catch(debugError);

this._client.on('Runtime.consoleAPICalled', event => consoleAPICalled(event.type, event.args.map(jsHandleFactory), event.stackTrace));
this._client.on('Runtime.exceptionThrown', exception => exceptionThrown(exception.exceptionDetails));
}

/**
* @return {string}
*/
url() {
url(): string {
return this._url;
}

/**
* @return {!Promise<ExecutionContext>}
*/
async executionContext() {
async executionContext(): Promise<ExecutionContext> {
return this._executionContextPromise;
}

/**
* @param {Function|string} pageFunction
* @param {!Array<*>} args
* @return {!Promise<*>}
*/
async evaluate(pageFunction, ...args) {
return (await this._executionContextPromise).evaluate(pageFunction, ...args);
async evaluate<ReturnType extends any>(pageFunction: Function|string, ...args: any[]): Promise<ReturnType> {
return (await this._executionContextPromise).evaluate<ReturnType>(pageFunction, ...args);
}

/**
* @param {Function|string} pageFunction
* @param {!Array<*>} args
* @return {!Promise<!JSHandle>}
*/
async evaluateHandle(pageFunction, ...args) {
async evaluateHandle(pageFunction: Function|string, ...args: any[]): Promise<JSHandle> {
return (await this._executionContextPromise).evaluateHandle(pageFunction, ...args);
}
}

module.exports = {Worker};