91
});
92
}
94
>
public getProxy<T>(identifier: ProxyIdentifier<T>): Proxied<T> {
95
>
if (!this._proxies[identifier.sid]) {
96
>
this._proxies[identifier.sid] = this._createProxy(identifier.sid);
97
>
}
98
>
return this._proxies[identifier.sid];
99
>
}
100
>
101
>
private _createProxy<T>(proxyId: string): T {
102
>
const handler = {
103
>
get: (target: any, name: PropertyKey) => {
104
>
if (typeof name === 'string' && !target[name] && name.charCodeAt(0) === CharCode.DollarSign) {
105
>
target[name] = (...myArgs: any[]) => {
106
>
return this._remoteCall(proxyId, name, myArgs);
107
>
};
108
>
}
109
>
110
>
return target[name];
111
>
}
112
>
};
113
>
return new Proxy(Object.create(null), handler);
114
>
}
115
>
116
>
public set<T, R extends T>(identifier: ProxyIdentifier<T>, value: R): R {
117
>
this._locals[identifier.sid] = value;
118
>
return value;
119
>
}
120
>
121
>
protected _remoteCall(proxyId: string, path: string, args: any[]): Promise<any> {
122
>
this._callCount++;
123
>
124
>
return new Promise<any>((c) => {
125
>
setTimeout(c, 0);
126
>
}).then(() => {
127
>
const instance = this._locals[proxyId];
128
>
// pretend the args went over the wire... (invoke .toJSON on objects...)
129
>
const wireArgs = simulateWireTransfer(args);
130
>
let p: Promise<any>;
131
>
try {
132
>
const result = (<Function>instance[path]).apply(instance, wireArgs);
133
>
p = isThenable(result) ? result : Promise.resolve(result);
134
>
} catch (err) {
135
p = Promise.reject(err);
136
}
138
>
return p.then(result => {
139
>
this._callCount--;
140
>
// pretend the result went over the wire... (invoke .toJSON on objects...)
141
>
const wireResult = simulateWireTransfer(result);
142
>
return wireResult;
143
>
}, err => {
144
this._callCount--;
145
return Promise.reject(err);
147
>
});
148
>
}
149
>
150
>
public dispose() { }
151
>
152
>
public assertRegistered(identifiers: ProxyIdentifier<any>[]): void {
153
throw new Error('Not implemented!');
154
}
156
>
157
>
function simulateWireTransfer<T>(obj: T): T {
158
>
if (!obj) {
159
>
return obj;
160
>
}
161
>
162
>
if (Array.isArray(obj)) {
163
>
// eslint-disable-next-line local/code-no-any-casts
164
>
return obj.map(simulateWireTransfer) as any;
165
>
}
166
167
if (obj instanceof SerializableObjectWithBuffers) {