@@ -23,6 +23,7 @@ import { Emitter, Event } from '../event';
23
23
import { Channel } from '../message-rpc/channel' ;
24
24
import { RequestHandler , RpcProtocol } from '../message-rpc/rpc-protocol' ;
25
25
import { ConnectionHandler } from './handler' ;
26
+ import { Deferred } from '../promise-util' ;
26
27
27
28
export type JsonRpcServer < Client > = Disposable & {
28
29
/**
@@ -55,11 +56,11 @@ export class JsonRpcConnectionHandler<T extends object> implements ConnectionHan
55
56
}
56
57
}
57
58
/**
58
- * Factory for creating a new {@link RpcConnection } for a given chanel and {@link RequestHandler}.
59
+ * Factory for creating a new {@link RpcProtocol } for a given chanel and {@link RequestHandler}.
59
60
*/
60
- export type RpcConnectionFactory = ( channel : Channel , requestHandler : RequestHandler ) => RpcProtocol ;
61
+ export type RpcProtocolFactory = ( channel : Channel , requestHandler : RequestHandler ) => RpcProtocol ;
61
62
62
- const defaultRPCConnectionFactory : RpcConnectionFactory = ( channel , requestHandler ) => new RpcProtocol ( channel , requestHandler ) ;
63
+ const defaultRpcProtocolFactory : RpcProtocolFactory = ( channel , requestHandler ) => new RpcProtocol ( channel , requestHandler ) ;
63
64
64
65
/**
65
66
* Factory for JSON-RPC proxy objects.
@@ -109,25 +110,22 @@ export class JsonRpcProxyFactory<T extends object> implements ProxyHandler<T> {
109
110
protected readonly onDidOpenConnectionEmitter = new Emitter < void > ( ) ;
110
111
protected readonly onDidCloseConnectionEmitter = new Emitter < void > ( ) ;
111
112
112
- protected connectionPromiseResolve : ( connection : RpcProtocol ) => void ;
113
- protected connectionPromise : Promise < RpcProtocol > ;
113
+ protected rpcDeferred : Deferred < RpcProtocol > ;
114
114
115
115
/**
116
116
* Build a new JsonRpcProxyFactory.
117
117
*
118
118
* @param target - The object to expose to JSON-RPC methods calls. If this
119
119
* is omitted, the proxy won't be able to handle requests, only send them.
120
120
*/
121
- constructor ( public target ?: any , protected rpcConnectionFactory = defaultRPCConnectionFactory ) {
121
+ constructor ( public target ?: any , protected rpcProtocolFactory = defaultRpcProtocolFactory ) {
122
122
this . waitForConnection ( ) ;
123
123
}
124
124
125
125
protected waitForConnection ( ) : void {
126
- this . connectionPromise = new Promise ( resolve =>
127
- this . connectionPromiseResolve = resolve
128
- ) ;
129
- this . connectionPromise . then ( connection => {
130
- connection . channel . onClose ( ( ) => {
126
+ this . rpcDeferred = new Deferred < RpcProtocol > ( ) ;
127
+ this . rpcDeferred . promise . then ( protocol => {
128
+ protocol . channel . onClose ( ( ) => {
131
129
this . onDidCloseConnectionEmitter . fire ( undefined ) ;
132
130
// Wait for connection in case the backend reconnects
133
131
this . waitForConnection ( ) ;
@@ -143,10 +141,10 @@ export class JsonRpcProxyFactory<T extends object> implements ProxyHandler<T> {
143
141
* response.
144
142
*/
145
143
listen ( channel : Channel ) : void {
146
- const connection = this . rpcConnectionFactory ( channel , ( meth , args ) => this . onRequest ( meth , ...args ) ) ;
147
- connection . onNotification ( event => this . onNotification ( event . method , ...event . args ) ) ;
144
+ const protocol = this . rpcProtocolFactory ( channel , ( meth , args ) => this . onRequest ( meth , ...args ) ) ;
145
+ protocol . onNotification ( event => this . onNotification ( event . method , ...event . args ) ) ;
148
146
149
- this . connectionPromiseResolve ( connection ) ;
147
+ this . rpcDeferred . resolve ( protocol ) ;
150
148
}
151
149
152
150
/**
@@ -249,7 +247,7 @@ export class JsonRpcProxyFactory<T extends object> implements ProxyHandler<T> {
249
247
return ( ...args : any [ ] ) => {
250
248
const method = p . toString ( ) ;
251
249
const capturedError = new Error ( `Request '${ method } ' failed` ) ;
252
- return this . connectionPromise . then ( connection =>
250
+ return this . rpcDeferred . promise . then ( connection =>
253
251
new Promise < void > ( ( resolve , reject ) => {
254
252
try {
255
253
if ( isNotify ) {
0 commit comments