chore: migrate src/Worker to typescript (#5715)

This commit is contained in:
Jack Franklin 2020-04-22 15:43:45 +01:00 committed by GitHub
parent feec588f95
commit 11bc5a6450
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,71 +13,55 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
const EventEmitter = require('events'); import {EventEmitter} from 'events';
const {debugError} = require('./helper'); import {debugError} from './helper';
const {ExecutionContext} = require('./ExecutionContext'); import {ExecutionContext} from './ExecutionContext';
const {JSHandle} = require('./JSHandle'); import {JSHandle} from './JSHandle';
// Used as a TypeDef import {CDPSession} from './Connection';
// eslint-disable-next-line no-unused-vars
const {CDPSession} = require('./Connection');
class Worker extends EventEmitter { type ConsoleAPICalledCallback = (eventType: string, handles: JSHandle[], trace: Protocol.Runtime.StackTrace) => void;
/** type ExceptionThrownCallback = (details: Protocol.Runtime.ExceptionDetails) => void;
* @param {CDPSession} client type JSHandleFactory = (obj: Protocol.Runtime.RemoteObject) => JSHandle;
* @param {string} url
* @param {function(string, !Array<!JSHandle>, Protocol.Runtime.StackTrace=):void} consoleAPICalled export class Worker extends EventEmitter {
* @param {function(!Protocol.Runtime.ExceptionDetails):void} exceptionThrown _client: CDPSession;
*/ _url: string;
constructor(client, url, consoleAPICalled, exceptionThrown) { _executionContextPromise: Promise<ExecutionContext>;
_executionContextCallback: (value: ExecutionContext) => void;
constructor(client: CDPSession, url: string, consoleAPICalled: ConsoleAPICalledCallback, exceptionThrown: ExceptionThrownCallback) {
super(); super();
this._client = client; this._client = client;
this._url = url; this._url = url;
this._executionContextPromise = new Promise(x => this._executionContextCallback = x); this._executionContextPromise = new Promise<ExecutionContext>(x => this._executionContextCallback = x);
/** @type {function(!Protocol.Runtime.RemoteObject):!JSHandle} */
let jsHandleFactory; let jsHandleFactory: JSHandleFactory;
this._client.once('Runtime.executionContextCreated', async event => { this._client.once('Runtime.executionContextCreated', async event => {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
jsHandleFactory = remoteObject => new JSHandle(executionContext, client, remoteObject); jsHandleFactory = remoteObject => new JSHandle(executionContext, client, remoteObject);
const executionContext = new ExecutionContext(client, event.context, null); const executionContext = new ExecutionContext(client, event.context, null);
this._executionContextCallback(executionContext); this._executionContextCallback(executionContext);
}); });
// This might fail if the target is closed before we recieve all execution contexts. // This might fail if the target is closed before we recieve all execution contexts.
this._client.send('Runtime.enable', {}).catch(debugError); 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.consoleAPICalled', event => consoleAPICalled(event.type, event.args.map(jsHandleFactory), event.stackTrace));
this._client.on('Runtime.exceptionThrown', exception => exceptionThrown(exception.exceptionDetails)); this._client.on('Runtime.exceptionThrown', exception => exceptionThrown(exception.exceptionDetails));
} }
/** url(): string {
* @return {string}
*/
url() {
return this._url; return this._url;
} }
/** async executionContext(): Promise<ExecutionContext> {
* @return {!Promise<ExecutionContext>}
*/
async executionContext() {
return this._executionContextPromise; return this._executionContextPromise;
} }
/** async evaluate<ReturnType extends any>(pageFunction: Function|string, ...args: any[]): Promise<ReturnType> {
* @param {Function|string} pageFunction return (await this._executionContextPromise).evaluate<ReturnType>(pageFunction, ...args);
* @param {!Array<*>} args
* @return {!Promise<*>}
*/
async evaluate(pageFunction, ...args) {
return (await this._executionContextPromise).evaluate(pageFunction, ...args);
} }
/** async evaluateHandle(pageFunction: Function|string, ...args: any[]): Promise<JSHandle> {
* @param {Function|string} pageFunction
* @param {!Array<*>} args
* @return {!Promise<!JSHandle>}
*/
async evaluateHandle(pageFunction, ...args) {
return (await this._executionContextPromise).evaluateHandle(pageFunction, ...args); return (await this._executionContextPromise).evaluateHandle(pageFunction, ...args);
} }
} }
module.exports = {Worker};