2020-06-30 20:55:11 +03:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-07-30 03:26:59 +03:00
|
|
|
import { Events } from './events';
|
2022-04-07 00:57:14 +03:00
|
|
|
import type * as channels from '../protocol/channels';
|
2020-06-30 20:55:11 +03:00
|
|
|
import { ChannelOwner } from './channelOwner';
|
2020-12-27 04:05:57 +03:00
|
|
|
import { assertMaxArguments, JSHandle, parseResult, serializeArgument } from './jsHandle';
|
2022-04-07 00:57:14 +03:00
|
|
|
import type { Page } from './page';
|
|
|
|
import type { BrowserContext } from './browserContext';
|
|
|
|
import type * as api from '../../types/types';
|
|
|
|
import type * as structs from '../../types/structs';
|
2020-06-30 20:55:11 +03:00
|
|
|
|
2021-11-18 02:26:01 +03:00
|
|
|
export class Worker extends ChannelOwner<channels.WorkerChannel> implements api.Worker {
|
2020-07-09 07:36:03 +03:00
|
|
|
_page: Page | undefined; // Set for web workers.
|
|
|
|
_context: BrowserContext | undefined; // Set for service workers.
|
2020-06-30 20:55:11 +03:00
|
|
|
|
2020-08-25 03:05:16 +03:00
|
|
|
static from(worker: channels.WorkerChannel): Worker {
|
2020-07-02 04:36:09 +03:00
|
|
|
return (worker as any)._object;
|
2020-06-30 20:55:11 +03:00
|
|
|
}
|
|
|
|
|
2020-08-25 03:05:16 +03:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.WorkerInitializer) {
|
2020-07-11 04:00:10 +03:00
|
|
|
super(parent, type, guid, initializer);
|
2020-07-01 23:55:29 +03:00
|
|
|
this._channel.on('close', () => {
|
2020-07-09 07:36:03 +03:00
|
|
|
if (this._page)
|
|
|
|
this._page._workers.delete(this);
|
|
|
|
if (this._context)
|
2021-04-02 04:47:14 +03:00
|
|
|
this._context._serviceWorkers.delete(this);
|
2020-06-30 20:55:11 +03:00
|
|
|
this.emit(Events.Worker.Close, this);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
url(): string {
|
|
|
|
return this._initializer.url;
|
|
|
|
}
|
|
|
|
|
2020-12-27 04:05:57 +03:00
|
|
|
async evaluate<R, Arg>(pageFunction: structs.PageFunction<Arg, R>, arg?: Arg): Promise<R> {
|
2020-06-30 20:55:11 +03:00
|
|
|
assertMaxArguments(arguments.length, 2);
|
2021-11-20 03:28:11 +03:00
|
|
|
const result = await this._channel.evaluateExpression({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) });
|
|
|
|
return parseResult(result.value);
|
2020-06-30 20:55:11 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 04:05:57 +03:00
|
|
|
async evaluateHandle<R, Arg>(pageFunction: structs.PageFunction<Arg, R>, arg?: Arg): Promise<structs.SmartHandle<R>> {
|
2020-06-30 20:55:11 +03:00
|
|
|
assertMaxArguments(arguments.length, 2);
|
2021-11-20 03:28:11 +03:00
|
|
|
const result = await this._channel.evaluateExpressionHandle({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) });
|
|
|
|
return JSHandle.from(result.handle) as any as structs.SmartHandle<R>;
|
2020-06-30 20:55:11 +03:00
|
|
|
}
|
|
|
|
}
|