mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: migrate src/Worker to typescript (#5715)
This commit is contained in:
parent
feec588f95
commit
11bc5a6450
@ -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};
|
Loading…
Reference in New Issue
Block a user