feat(rpc): use SerializedValue for CDPSession (#3076)

This is our way to define a schema for arbitrary values.
This commit is contained in:
Dmitry Gozman 2020-07-21 15:58:00 -07:00 committed by GitHub
parent 1553f19bab
commit db4e856a57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 19 deletions

View file

@ -1571,21 +1571,15 @@ export interface CDPSessionChannel extends Channel {
}
export type CDPSessionEventEvent = {
method: string,
params?: {
},
params?: SerializedValue,
};
export type CDPSessionDisconnectedEvent = {};
export type CDPSessionSendParams = {
method: string,
params?: {
},
params?: SerializedValue,
};
export type CDPSessionSendResult = {
result: {
},
result: SerializedValue,
};
export type CDPSessionDetachParams = {};
export type CDPSessionDetachResult = void;

View file

@ -17,6 +17,7 @@
import { CDPSessionChannel, CDPSessionInitializer } from '../channels';
import { ChannelOwner } from './channelOwner';
import { Protocol } from '../../chromium/protocol';
import { parseResult, serializeArgument } from './jsHandle';
export class CDPSession extends ChannelOwner<CDPSessionChannel, CDPSessionInitializer> {
static from(cdpSession: CDPSessionChannel): CDPSession {
@ -32,7 +33,10 @@ export class CDPSession extends ChannelOwner<CDPSessionChannel, CDPSessionInitia
constructor(parent: ChannelOwner, type: string, guid: string, initializer: CDPSessionInitializer) {
super(parent, type, guid, initializer, true);
this._channel.on('event', ({ method, params }) => this.emit(method, params));
this._channel.on('event', ({ method, params }) => {
const cdpParams = params ? parseResult(params) : undefined;
this.emit(method, cdpParams);
});
this._channel.on('disconnected', () => this._dispose());
this.on = super.on;
@ -47,8 +51,9 @@ export class CDPSession extends ChannelOwner<CDPSessionChannel, CDPSessionInitia
params?: Protocol.CommandParameters[T]
): Promise<Protocol.CommandReturnValues[T]> {
return this._wrapApiCall('cdpSession.send', async () => {
const result = await this._channel.send({ method, params });
return result.result as Protocol.CommandReturnValues[T];
const protocolParams = params ? serializeArgument(params).value : undefined;
const result = await this._channel.send({ method, params: protocolParams });
return parseResult(result.result) as Protocol.CommandReturnValues[T];
});
}

View file

@ -1407,16 +1407,16 @@ interface CDPSession
event event
parameters
method: string
params?: object
params?: SerializedValue
event disconnected
command send
parameters
method: string
params?: object
params?: SerializedValue
returns
result: object
result: SerializedValue
command detach

View file

@ -15,21 +15,26 @@
*/
import { CRSession, CRSessionEvents } from '../../chromium/crConnection';
import { CDPSessionChannel, CDPSessionInitializer } from '../channels';
import { CDPSessionChannel, CDPSessionInitializer, SerializedValue } from '../channels';
import { Dispatcher, DispatcherScope } from './dispatcher';
import { serializeResult, parseArgument } from './jsHandleDispatcher';
export class CDPSessionDispatcher extends Dispatcher<CRSession, CDPSessionInitializer> implements CDPSessionChannel {
constructor(scope: DispatcherScope, crSession: CRSession) {
super(scope, crSession, 'cdpSession', {}, true);
crSession._eventListener = (method, params) => this._dispatchEvent('event', { method, params });
crSession._eventListener = (method, cdpParams) => {
const params = cdpParams ? serializeResult(cdpParams) : undefined;
this._dispatchEvent('event', { method, params });
};
crSession.on(CRSessionEvents.Disconnected, () => {
this._dispatchEvent('disconnected');
this._dispose();
});
}
async send(params: { method: string, params?: Object }): Promise<{ result: Object }> {
return { result: await this._object.send(params.method as any, params.params) };
async send(params: { method: string, params?: SerializedValue }): Promise<{ result: SerializedValue }> {
const cdpParams = params.params ? parseArgument({ value: params.params, handles: [] }) : undefined;
return { result: serializeResult(await this._object.send(params.method as any, cdpParams)) };
}
async detach(): Promise<void> {