2020-06-25 16:05:36 -07: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 { EventEmitter } from 'events';
|
2020-07-01 18:36:09 -07:00
|
|
|
import { helper, debugAssert, assert } from '../../helper';
|
2020-06-30 22:21:17 -07:00
|
|
|
import { Channel } from '../channels';
|
|
|
|
import { serializeError } from '../serializers';
|
2020-07-24 15:16:33 -07:00
|
|
|
import { createScheme, Validator, ValidationError } from '../validator';
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-06-30 21:30:39 -07:00
|
|
|
export const dispatcherSymbol = Symbol('dispatcher');
|
|
|
|
|
|
|
|
export function lookupDispatcher<DispatcherType>(object: any): DispatcherType {
|
|
|
|
const result = object[dispatcherSymbol];
|
|
|
|
debugAssert(result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function existingDispatcher<DispatcherType>(object: any): DispatcherType {
|
|
|
|
return object[dispatcherSymbol];
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:38:06 -07:00
|
|
|
export function lookupNullableDispatcher<DispatcherType>(object: any | null): DispatcherType | undefined {
|
|
|
|
return object ? lookupDispatcher(object) : undefined;
|
2020-06-30 21:30:39 -07:00
|
|
|
}
|
|
|
|
|
2020-06-26 17:24:21 -07:00
|
|
|
export class Dispatcher<Type, Initializer> extends EventEmitter implements Channel {
|
2020-07-10 16:24:11 -07:00
|
|
|
private _connection: DispatcherConnection;
|
|
|
|
private _isScope: boolean;
|
|
|
|
// Parent is always "isScope".
|
|
|
|
private _parent: Dispatcher<any, any> | undefined;
|
|
|
|
// Only "isScope" channel owners have registered dispatchers inside.
|
|
|
|
private _dispatchers = new Map<string, Dispatcher<any, any>>();
|
2020-07-27 10:21:39 -07:00
|
|
|
private _disposed = false;
|
2020-07-10 16:24:11 -07:00
|
|
|
|
2020-06-25 16:05:36 -07:00
|
|
|
readonly _guid: string;
|
|
|
|
readonly _type: string;
|
2020-07-10 16:24:11 -07:00
|
|
|
readonly _scope: Dispatcher<any, any>;
|
2020-06-29 18:58:09 -07:00
|
|
|
_object: Type;
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-07-10 16:24:11 -07:00
|
|
|
constructor(parent: Dispatcher<any, any> | DispatcherConnection, object: Type, type: string, initializer: Initializer, isScope?: boolean, guid = type + '@' + helper.guid()) {
|
2020-06-25 16:05:36 -07:00
|
|
|
super();
|
2020-07-10 16:24:11 -07:00
|
|
|
|
|
|
|
this._connection = parent instanceof DispatcherConnection ? parent : parent._connection;
|
|
|
|
this._isScope = !!isScope;
|
|
|
|
this._parent = parent instanceof DispatcherConnection ? undefined : parent;
|
|
|
|
this._scope = isScope ? this : this._parent!;
|
|
|
|
|
|
|
|
assert(!this._connection._dispatchers.has(guid));
|
|
|
|
this._connection._dispatchers.set(guid, this);
|
|
|
|
if (this._parent) {
|
|
|
|
assert(!this._parent._dispatchers.has(guid));
|
|
|
|
this._parent._dispatchers.set(guid, this);
|
|
|
|
}
|
|
|
|
|
2020-06-25 16:05:36 -07:00
|
|
|
this._type = type;
|
|
|
|
this._guid = guid;
|
|
|
|
this._object = object;
|
2020-07-10 16:24:11 -07:00
|
|
|
|
2020-06-30 21:30:39 -07:00
|
|
|
(object as any)[dispatcherSymbol] = this;
|
2020-07-10 16:24:11 -07:00
|
|
|
if (this._parent)
|
2020-07-27 10:21:39 -07:00
|
|
|
this._connection.sendMessageToClient(this._parent._guid, '__create__', { type, initializer, guid }, !!isScope);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-06-26 17:24:21 -07:00
|
|
|
_dispatchEvent(method: string, params: Dispatcher<any, any> | any = {}) {
|
2020-07-10 16:24:11 -07:00
|
|
|
this._connection.sendMessageToClient(this._guid, method, params);
|
2020-06-30 22:21:17 -07:00
|
|
|
}
|
|
|
|
|
2020-07-10 16:24:11 -07:00
|
|
|
_dispose() {
|
2020-07-27 10:21:39 -07:00
|
|
|
assert(!this._disposed);
|
2020-07-01 13:55:29 -07:00
|
|
|
|
2020-07-10 16:24:11 -07:00
|
|
|
// Clean up from parent and connection.
|
|
|
|
if (this._parent)
|
|
|
|
this._parent._dispatchers.delete(this._guid);
|
|
|
|
this._connection._dispatchers.delete(this._guid);
|
2020-07-01 13:55:29 -07:00
|
|
|
|
2020-07-10 16:24:11 -07:00
|
|
|
// Dispose all children.
|
2020-07-27 10:21:39 -07:00
|
|
|
for (const dispatcher of [...this._dispatchers.values()])
|
|
|
|
dispatcher._dispose();
|
2020-07-10 16:24:11 -07:00
|
|
|
this._dispatchers.clear();
|
2020-07-27 10:21:39 -07:00
|
|
|
|
|
|
|
if (this._isScope)
|
|
|
|
this._connection.sendMessageToClient(this._guid, '__dispose__', {});
|
2020-06-30 22:21:17 -07:00
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-07-10 16:24:11 -07:00
|
|
|
_debugScopeState(): any {
|
|
|
|
return {
|
|
|
|
_guid: this._guid,
|
2020-07-27 10:21:39 -07:00
|
|
|
objects: Array.from(this._dispatchers.values()).map(o => o._debugScopeState()),
|
2020-07-10 16:24:11 -07:00
|
|
|
};
|
2020-06-30 22:21:17 -07:00
|
|
|
}
|
2020-07-10 16:24:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export type DispatcherScope = Dispatcher<any, any>;
|
2020-07-01 13:55:29 -07:00
|
|
|
|
2020-07-10 16:24:11 -07:00
|
|
|
class Root extends Dispatcher<{}, {}> {
|
|
|
|
constructor(connection: DispatcherConnection) {
|
|
|
|
super(connection, {}, '', {}, true, '');
|
2020-07-01 13:55:29 -07:00
|
|
|
}
|
2020-06-30 22:21:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class DispatcherConnection {
|
|
|
|
readonly _dispatchers = new Map<string, Dispatcher<any, any>>();
|
2020-07-10 16:24:11 -07:00
|
|
|
private _rootDispatcher: Root;
|
2020-07-13 08:31:20 -07:00
|
|
|
onmessage = (message: object) => {};
|
2020-07-24 15:16:33 -07:00
|
|
|
private _validateParams: (type: string, method: string, params: any) => any;
|
2020-06-30 22:21:17 -07:00
|
|
|
|
2020-07-27 10:21:39 -07:00
|
|
|
async sendMessageToClient(guid: string, method: string, params: any, disallowDispatchers?: boolean): Promise<any> {
|
|
|
|
const allowDispatchers = !disallowDispatchers;
|
|
|
|
this.onmessage({ guid, method, params: this._replaceDispatchersWithGuids(params, allowDispatchers) });
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-01 13:55:29 -07:00
|
|
|
constructor() {
|
2020-07-10 16:24:11 -07:00
|
|
|
this._rootDispatcher = new Root(this);
|
2020-07-24 15:16:33 -07:00
|
|
|
|
|
|
|
const tChannel = (name: string): Validator => {
|
|
|
|
return (arg: any, path: string) => {
|
|
|
|
if (arg && typeof arg === 'object' && typeof arg.guid === 'string') {
|
|
|
|
const guid = arg.guid;
|
|
|
|
const dispatcher = this._dispatchers.get(guid);
|
|
|
|
if (!dispatcher)
|
|
|
|
throw new ValidationError(`${path}: no object with guid ${guid}`);
|
|
|
|
if (name !== '*' && dispatcher._type !== name)
|
|
|
|
throw new ValidationError(`${path}: object with guid ${guid} has type ${dispatcher._type}, expected ${name}`);
|
|
|
|
return dispatcher;
|
|
|
|
}
|
|
|
|
throw new ValidationError(`${path}: expected ${name}`);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
const scheme = createScheme(tChannel);
|
|
|
|
this._validateParams = (type: string, method: string, params: any): any => {
|
|
|
|
const name = type + method[0].toUpperCase() + method.substring(1) + 'Params';
|
|
|
|
if (!scheme[name])
|
|
|
|
throw new ValidationError(`Uknown scheme for ${type}.${method}`);
|
|
|
|
return scheme[name](params, '');
|
|
|
|
};
|
2020-07-01 13:55:29 -07:00
|
|
|
}
|
|
|
|
|
2020-07-10 16:24:11 -07:00
|
|
|
rootDispatcher(): Dispatcher<any, any> {
|
|
|
|
return this._rootDispatcher;
|
2020-06-30 22:21:17 -07:00
|
|
|
}
|
|
|
|
|
2020-07-13 08:31:20 -07:00
|
|
|
async dispatch(message: object) {
|
|
|
|
const { id, guid, method, params } = message as any;
|
2020-06-30 22:21:17 -07:00
|
|
|
const dispatcher = this._dispatchers.get(guid);
|
|
|
|
if (!dispatcher) {
|
2020-07-13 08:31:20 -07:00
|
|
|
this.onmessage({ id, error: serializeError(new Error('Target browser or context has been closed')) });
|
2020-06-30 22:21:17 -07:00
|
|
|
return;
|
|
|
|
}
|
2020-07-01 13:55:29 -07:00
|
|
|
if (method === 'debugScopeState') {
|
2020-07-13 08:31:20 -07:00
|
|
|
this.onmessage({ id, result: this._rootDispatcher._debugScopeState() });
|
2020-07-01 13:55:29 -07:00
|
|
|
return;
|
|
|
|
}
|
2020-06-27 11:10:07 -07:00
|
|
|
try {
|
2020-07-24 15:16:33 -07:00
|
|
|
const validated = this._validateParams(dispatcher._type, method, params);
|
|
|
|
const result = await (dispatcher as any)[method](validated);
|
2020-07-27 10:21:39 -07:00
|
|
|
this.onmessage({ id, result: this._replaceDispatchersWithGuids(result, true) });
|
2020-06-27 11:10:07 -07:00
|
|
|
} catch (e) {
|
2020-07-13 08:31:20 -07:00
|
|
|
this.onmessage({ id, error: serializeError(e) });
|
2020-06-27 11:10:07 -07:00
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-27 10:21:39 -07:00
|
|
|
private _replaceDispatchersWithGuids(payload: any, allowDispatchers: boolean): any {
|
2020-06-25 16:05:36 -07:00
|
|
|
if (!payload)
|
|
|
|
return payload;
|
2020-07-27 10:21:39 -07:00
|
|
|
if (payload instanceof Dispatcher) {
|
|
|
|
if (!allowDispatchers)
|
|
|
|
throw new Error(`Channels are not allowed in the scope's initialzier`);
|
2020-06-25 16:05:36 -07:00
|
|
|
return { guid: payload._guid };
|
2020-07-27 10:21:39 -07:00
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
if (Array.isArray(payload))
|
2020-07-27 10:21:39 -07:00
|
|
|
return payload.map(p => this._replaceDispatchersWithGuids(p, allowDispatchers));
|
2020-06-26 17:24:21 -07:00
|
|
|
if (typeof payload === 'object') {
|
|
|
|
const result: any = {};
|
|
|
|
for (const key of Object.keys(payload))
|
2020-07-27 10:21:39 -07:00
|
|
|
result[key] = this._replaceDispatchersWithGuids(payload[key], allowDispatchers);
|
2020-06-26 17:24:21 -07:00
|
|
|
return result;
|
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
return payload;
|
|
|
|
}
|
|
|
|
}
|