Skip to content

Commit

Permalink
chore: use deferred promises in web worker (#8824)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandolf committed Aug 22, 2022
1 parent e05c199 commit b9b24cf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 36 deletions.
4 changes: 3 additions & 1 deletion docs/api/puppeteer.webworker.executioncontext.md
Expand Up @@ -4,7 +4,9 @@ sidebar_label: WebWorker.executionContext

# WebWorker.executionContext() method

Returns the ExecutionContext the WebWorker runs in
> Warning: This API is now obsolete.
>
> Do not use directly.
**Signature:**

Expand Down
2 changes: 1 addition & 1 deletion docs/api/puppeteer.webworker.md
Expand Up @@ -42,5 +42,5 @@ for (const worker of page.workers()) {
| ----------------------------------------------------------------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [evaluate(pageFunction, args)](./puppeteer.webworker.evaluate.md) | | If the function passed to the <code>worker.evaluate</code> returns a Promise, then <code>worker.evaluate</code> would wait for the promise to resolve and return its value. If the function passed to the <code>worker.evaluate</code> returns a non-serializable value, then <code>worker.evaluate</code> resolves to <code>undefined</code>. DevTools Protocol also supports transferring some additional values that are not serializable by <code>JSON</code>: <code>-0</code>, <code>NaN</code>, <code>Infinity</code>, <code>-Infinity</code>, and bigint literals. Shortcut for <code>await worker.executionContext()).evaluate(pageFunction, ...args)</code>. |
| [evaluateHandle(pageFunction, args)](./puppeteer.webworker.evaluatehandle.md) | | The only difference between <code>worker.evaluate</code> and <code>worker.evaluateHandle</code> is that <code>worker.evaluateHandle</code> returns in-page object (JSHandle). If the function passed to the <code>worker.evaluateHandle</code> returns a <code>Promise</code>, then <code>worker.evaluateHandle</code> would wait for the promise to resolve and return its value. Shortcut for <code>await worker.executionContext()).evaluateHandle(pageFunction, ...args)</code> |
| [executionContext()](./puppeteer.webworker.executioncontext.md) | | Returns the ExecutionContext the WebWorker runs in |
| [executionContext()](./puppeteer.webworker.executioncontext.md) | | |
| [url()](./puppeteer.webworker.url.md) | | |
59 changes: 25 additions & 34 deletions src/common/WebWorker.ts
Expand Up @@ -21,6 +21,7 @@ import {EventEmitter} from './EventEmitter.js';
import {ExecutionContext} from './ExecutionContext.js';
import {JSHandle} from './JSHandle.js';
import {debugError} from './util.js';
import {createDeferredPromise} from '../util/DeferredPromise.js';

/**
* @internal
Expand All @@ -38,8 +39,6 @@ export type ExceptionThrownCallback = (
details: Protocol.Runtime.ExceptionDetails
) => void;

type JSHandleFactory = (obj: Protocol.Runtime.RemoteObject) => JSHandle;

/**
* This class represents a
* {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API | WebWorker}.
Expand Down Expand Up @@ -67,10 +66,10 @@ type JSHandleFactory = (obj: Protocol.Runtime.RemoteObject) => JSHandle;
* @public
*/
export class WebWorker extends EventEmitter {
#executionContext = createDeferredPromise<ExecutionContext>();

#client: CDPSession;
#url: string;
#executionContextPromise: Promise<ExecutionContext>;
#executionContextCallback!: (value: ExecutionContext) => void;

/**
* @internal
Expand All @@ -84,47 +83,43 @@ export class WebWorker extends EventEmitter {
super();
this.#client = client;
this.#url = url;
this.#executionContextPromise = new Promise<ExecutionContext>(x => {
return (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 => {
return new JSHandle(executionContext, client, remoteObject);
};
const executionContext = new ExecutionContext(client, event.context);
this.#executionContextCallback(executionContext);
const context = new ExecutionContext(client, event.context);
this.#executionContext.resolve(context);
});

// This might fail if the target is closed before we receive all execution contexts.
this.#client.send('Runtime.enable').catch(debugError);
this.#client.on('Runtime.consoleAPICalled', event => {
this.#client.on('Runtime.consoleAPICalled', async event => {
const context = await this.#executionContext;
return consoleAPICalled(
event.type,
event.args.map(jsHandleFactory),
event.args.map((object: Protocol.Runtime.RemoteObject) => {
return new JSHandle(context, this.#client, object);
}),
event.stackTrace
);
});
this.#client.on('Runtime.exceptionThrown', exception => {
return exceptionThrown(exception.exceptionDetails);
});

// This might fail if the target is closed before we receive all execution contexts.
this.#client.send('Runtime.enable').catch(debugError);
}

/**
* @returns The URL of this web worker.
* @deprecated Do not use directly.
*
* @returns The ExecutionContext the web worker runs in.
*/
url(): string {
return this.#url;
async executionContext(): Promise<ExecutionContext> {
return this.#executionContext;
}

/**
* Returns the ExecutionContext the WebWorker runs in
* @returns The ExecutionContext the web worker runs in.
* @returns The URL of this web worker.
*/
async executionContext(): Promise<ExecutionContext> {
return this.#executionContextPromise;
url(): string {
return this.#url;
}

/**
Expand All @@ -148,10 +143,8 @@ export class WebWorker extends EventEmitter {
pageFunction: Func | string,
...args: Params
): Promise<Awaited<ReturnType<Func>>> {
return (await this.#executionContextPromise).evaluate(
pageFunction,
...args
);
const context = await this.#executionContext;
return context.evaluate(pageFunction, ...args);
}

/**
Expand All @@ -173,9 +166,7 @@ export class WebWorker extends EventEmitter {
pageFunction: Func | string,
...args: Params
): Promise<HandleFor<Awaited<ReturnType<Func>>>> {
return (await this.#executionContextPromise).evaluateHandle(
pageFunction,
...args
);
const context = await this.#executionContext;
return context.evaluateHandle(pageFunction, ...args);
}
}

0 comments on commit b9b24cf

Please sign in to comment.