diff --git a/src/Worker.js b/src/Worker.ts similarity index 52% rename from src/Worker.js rename to src/Worker.ts index 2a750b2712812..b075370bcefa5 100644 --- a/src/Worker.js +++ b/src/Worker.ts @@ -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, 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; + _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(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} - */ - async executionContext() { + async executionContext(): Promise { 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(pageFunction: Function|string, ...args: any[]): Promise { + return (await this._executionContextPromise).evaluate(pageFunction, ...args); } - /** - * @param {Function|string} pageFunction - * @param {!Array<*>} args - * @return {!Promise} - */ - async evaluateHandle(pageFunction, ...args) { + async evaluateHandle(pageFunction: Function|string, ...args: any[]): Promise { return (await this._executionContextPromise).evaluateHandle(pageFunction, ...args); } } - -module.exports = {Worker};