2020-06-26 01:05:36 +02: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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import * as js from '../../javascript';
|
2020-07-21 21:44:30 +02:00
|
|
|
import { JSHandleChannel, JSHandleInitializer, SerializedArgument, SerializedValue } from '../channels';
|
2020-07-01 07:21:17 +02:00
|
|
|
import { Dispatcher, DispatcherScope } from './dispatcher';
|
2020-07-01 06:30:39 +02:00
|
|
|
import { createHandle } from './elementHandlerDispatcher';
|
2020-07-21 21:44:30 +02:00
|
|
|
import { parseSerializedValue, serializeValue } from '../serializers';
|
2020-06-26 01:05:36 +02:00
|
|
|
|
2020-06-27 02:24:21 +02:00
|
|
|
export class JSHandleDispatcher extends Dispatcher<js.JSHandle, JSHandleInitializer> implements JSHandleChannel {
|
2020-06-26 01:05:36 +02:00
|
|
|
|
2020-06-26 21:28:27 +02:00
|
|
|
constructor(scope: DispatcherScope, jsHandle: js.JSHandle) {
|
2020-07-22 19:37:21 +02:00
|
|
|
super(scope, jsHandle, jsHandle.asElement() ? 'ElementHandle' : 'JSHandle', {
|
2020-06-26 21:28:27 +02:00
|
|
|
preview: jsHandle.toString(),
|
|
|
|
|
});
|
2020-07-15 03:26:50 +02:00
|
|
|
jsHandle._setPreviewCallback(preview => this._dispatchEvent('previewUpdated', { preview }));
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-17 18:53:13 +02:00
|
|
|
async evaluateExpression(params: { expression: string, isFunction: boolean, arg: SerializedArgument }): Promise<{ value: SerializedValue }> {
|
2020-07-15 03:26:50 +02:00
|
|
|
return { value: serializeResult(await this._object._evaluateExpression(params.expression, params.isFunction, true /* returnByValue */, parseArgument(params.arg))) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-17 18:53:13 +02:00
|
|
|
async evaluateExpressionHandle(params: { expression: string, isFunction: boolean, arg: SerializedArgument}): Promise<{ handle: JSHandleChannel }> {
|
2020-06-27 20:10:07 +02:00
|
|
|
const jsHandle = await this._object._evaluateExpression(params.expression, params.isFunction, false /* returnByValue */, parseArgument(params.arg));
|
2020-07-15 03:26:50 +02:00
|
|
|
return { handle: createHandle(this._scope, jsHandle) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-15 03:26:50 +02:00
|
|
|
async getProperty(params: { name: string }): Promise<{ handle: JSHandleChannel }> {
|
2020-07-04 03:04:08 +02:00
|
|
|
const jsHandle = await this._object.getProperty(params.name);
|
2020-07-15 03:26:50 +02:00
|
|
|
return { handle: createHandle(this._scope, jsHandle) };
|
2020-07-04 03:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-15 03:26:50 +02:00
|
|
|
async getPropertyList(): Promise<{ properties: { name: string, value: JSHandleChannel }[] }> {
|
2020-06-27 02:24:21 +02:00
|
|
|
const map = await this._object.getProperties();
|
2020-07-15 03:26:50 +02:00
|
|
|
const properties = [];
|
2020-06-26 01:05:36 +02:00
|
|
|
for (const [name, value] of map)
|
2020-07-15 03:26:50 +02:00
|
|
|
properties.push({ name, value: new JSHandleDispatcher(this._scope, value) });
|
|
|
|
|
return { properties };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-17 18:53:13 +02:00
|
|
|
async jsonValue(): Promise<{ value: SerializedValue }> {
|
2020-07-15 03:26:50 +02:00
|
|
|
return { value: serializeResult(await this._object.jsonValue()) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async dispose() {
|
2020-06-27 02:24:21 +02:00
|
|
|
await this._object.dispose();
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-27 20:10:07 +02:00
|
|
|
|
2020-07-17 18:53:13 +02:00
|
|
|
// Generic channel parser converts guids to JSHandleDispatchers,
|
|
|
|
|
// and this function takes care of coverting them into underlying JSHandles.
|
|
|
|
|
export function parseArgument(arg: SerializedArgument): any {
|
2020-07-21 21:44:30 +02:00
|
|
|
return parseSerializedValue(arg.value, arg.handles.map(a => (a as JSHandleDispatcher)._object));
|
2020-06-27 20:10:07 +02:00
|
|
|
}
|
2020-07-23 02:20:00 +02:00
|
|
|
export function parseValue(v: SerializedValue): any {
|
|
|
|
|
return parseSerializedValue(v, []);
|
|
|
|
|
}
|
2020-06-27 20:10:07 +02:00
|
|
|
|
2020-07-17 18:53:13 +02:00
|
|
|
export function serializeResult(arg: any): SerializedValue {
|
2020-07-21 21:44:30 +02:00
|
|
|
return serializeValue(arg, value => ({ fallThrough: value }), new Set());
|
2020-06-27 20:10:07 +02:00
|
|
|
}
|