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

docs(new): add TSDoc comments to WebWorker #6029

Merged
merged 4 commits into from Jun 17, 2020
Merged
Changes from 1 commit
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
46 changes: 46 additions & 0 deletions src/WebWorker.ts
Expand Up @@ -30,12 +30,33 @@ type ExceptionThrownCallback = (
) => void;
type JSHandleFactory = (obj: Protocol.Runtime.RemoteObject) => JSHandle;

/**
* The WebWorker class represents a [WebWorker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API).
AVGP marked this conversation as resolved.
Show resolved Hide resolved
* The events `workercreated` and `workerdestroyed` are emitted on the page object to signal the worker lifecycle.
AVGP marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* ```js
* page.on('workercreated', worker => console.log('Worker created: ' + worker.url()));
* page.on('workerdestroyed', worker => console.log('Worker destroyed: ' + worker.url()));
*
* console.log('Current workers:');
* for (const worker of page.workers()) {
* console.log(' ' + worker.url());
* }
* ```
*
* @public
*/
export class WebWorker extends EventEmitter {
_client: CDPSession;
_url: string;
_executionContextPromise: Promise<ExecutionContext>;
_executionContextCallback: (value: ExecutionContext) => void;

/**
*
* @internal
*/
constructor(
client: CDPSession,
url: string,
Expand Down Expand Up @@ -76,14 +97,31 @@ export class WebWorker extends EventEmitter {
);
}

/**
* Returns the URL for the script this web worker is running.
AVGP marked this conversation as resolved.
Show resolved Hide resolved
* @returns The URL of this web worker.
*/
url(): string {
return this._url;
}

/**
* Returns the ExecutionContext the WebWorker runs in
* @returns The ExecutionContext the web worker runs in.
*/
async executionContext(): Promise<ExecutionContext> {
return this._executionContextPromise;
}

/**
* If the function passed to the `worker.evaluate` returns a Promise, then `worker.evaluate` would wait for the promise to resolve and return its value.
* If the function passed to the `worker.evaluate` returns a non-serializable value, then `worker.evaluate` resolves to `undefined`. DevTools Protocol also supports transferring some additional values that are not serializable by `JSON`: `-0`, `NaN`, `Infinity`, `-Infinity`, and bigint literals.
* Shortcut for `await worker.executionContext()).evaluate(pageFunction, ...args)`.
*
* @param pageFunction - Function to be evaluated in the worker context.
* @param args - Arguments to pass to `pageFunction`.
* @returns Promise which resolves to the return value of `pageFunction`.
*/
async evaluate<ReturnType extends any>(
pageFunction: Function | string,
...args: any[]
Expand All @@ -94,6 +132,14 @@ export class WebWorker extends EventEmitter {
);
}

/**
* The only difference between `worker.evaluate` and `worker.evaluateHandle` is that `worker.evaluateHandle` returns in-page object (JSHandle).
* If the function passed to the `worker.evaluateHandle` returns a [Promise], then `worker.evaluateHandle` would wait for the promise to resolve and return its value.
* Shortcut for [(await worker.executionContext()).evaluateHandle(pageFunction, ...args)](#executioncontextevaluatehandlepagefunction-args).
* @param pageFunction - Function to be evaluated in the page context.
* @param args - Arguments to pass to `pageFunction`.
* @returns Promise which resolves to the return value of `pageFunction`.
*/
async evaluateHandle(
pageFunction: Function | string,
...args: any[]
Expand Down