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 = { export type CDPSessionEventEvent = {
method: string, method: string,
params?: { params?: SerializedValue,
},
}; };
export type CDPSessionDisconnectedEvent = {}; export type CDPSessionDisconnectedEvent = {};
export type CDPSessionSendParams = { export type CDPSessionSendParams = {
method: string, method: string,
params?: { params?: SerializedValue,
},
}; };
export type CDPSessionSendResult = { export type CDPSessionSendResult = {
result: { result: SerializedValue,
},
}; };
export type CDPSessionDetachParams = {}; export type CDPSessionDetachParams = {};
export type CDPSessionDetachResult = void; export type CDPSessionDetachResult = void;

View file

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

View file

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