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';
|
|
|
|
import { Channel } from '../channels';
|
2020-07-01 13:55:29 -07:00
|
|
|
import { ConnectionScope } from './connection';
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-06-26 12:28:27 -07:00
|
|
|
export abstract class ChannelOwner<T extends Channel, Initializer> extends EventEmitter {
|
2020-06-25 16:05:36 -07:00
|
|
|
readonly _channel: T;
|
2020-06-26 12:28:27 -07:00
|
|
|
readonly _initializer: Initializer;
|
2020-07-01 13:55:29 -07:00
|
|
|
readonly _scope: ConnectionScope;
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-07-01 13:55:29 -07:00
|
|
|
constructor(scope: ConnectionScope, guid: string, initializer: Initializer, isScope?: boolean) {
|
2020-06-25 16:05:36 -07:00
|
|
|
super();
|
2020-07-01 13:55:29 -07:00
|
|
|
this._scope = isScope ? scope.createChild(guid) : scope;
|
|
|
|
const base = new EventEmitter();
|
|
|
|
this._channel = new Proxy(base, {
|
|
|
|
get: (obj: any, prop) => {
|
|
|
|
if (String(prop).startsWith('_'))
|
|
|
|
return obj[prop];
|
|
|
|
if (prop === 'then')
|
|
|
|
return obj.then;
|
|
|
|
if (prop === 'emit')
|
|
|
|
return obj.emit;
|
|
|
|
if (prop === 'on')
|
|
|
|
return obj.on;
|
|
|
|
if (prop === 'once')
|
|
|
|
return obj.once;
|
|
|
|
if (prop === 'addEventListener')
|
|
|
|
return obj.addListener;
|
|
|
|
if (prop === 'removeEventListener')
|
|
|
|
return obj.removeListener;
|
|
|
|
return (params: any) => scope.sendMessageToServer({ guid, method: String(prop), params });
|
|
|
|
},
|
|
|
|
});
|
|
|
|
this._channel._object = this;
|
|
|
|
this._channel._guid = guid;
|
2020-06-26 12:28:27 -07:00
|
|
|
this._initializer = initializer;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
}
|