1
1
import { defer , interval , Observable , of , OperatorFunction , Subject , throwError } from 'rxjs' ;
2
- import { finalize , flatMap , shareReplay , takeUntil , takeWhile } from 'rxjs/operators'
2
+ import { distinctUntilChanged , finalize , flatMap , shareReplay , takeUntil , takeWhile } from 'rxjs/operators'
3
3
import { Response } from './data/response' ;
4
4
import { Result } from './data/result' ;
5
5
import { Invocation , Invocations } from './data/invocation' ;
@@ -30,6 +30,7 @@ export class RxRpcClient extends RxRpcInvoker {
30
30
private listeners : RxRpcInvocationListener [ ] = [ ] ;
31
31
private currentConnection : RxRpcConnection ;
32
32
private readonly sharedInvocations = new Map < string , Observable < Result > > ( ) ;
33
+ private readonly connectedSubject = new Subject < boolean > ( ) ;
33
34
34
35
constructor ( private readonly transport : RxRpcTransport , options ?: RxRpcClientOptions ) {
35
36
super ( ) ;
@@ -54,6 +55,10 @@ export class RxRpcClient extends RxRpcInvoker {
54
55
} ) ;
55
56
}
56
57
58
+ public observeConnected ( ) : Observable < boolean > {
59
+ return this . connectedSubject . pipe ( distinctUntilChanged ( ) ) ;
60
+ }
61
+
57
62
public addListener ( listener : RxRpcInvocationListener ) : RxRpcInvocationListenerSubscription {
58
63
this . listeners . push ( listener ) ;
59
64
return { unsubscribe : ( ) => this . listeners = this . listeners . filter ( l => l != listener ) } ;
@@ -138,6 +143,7 @@ export class RxRpcClient extends RxRpcInvoker {
138
143
139
144
private onConnected ( connection : RxRpcConnection ) {
140
145
this . currentConnection = connection ;
146
+ this . connectedSubject . next ( true ) ;
141
147
this . currentConnection . messages
142
148
. pipe ( takeUntil ( this . cancelledSubject ) )
143
149
. subscribe (
@@ -156,12 +162,15 @@ export class RxRpcClient extends RxRpcInvoker {
156
162
this . invocations . clear ( ) ;
157
163
this . cancelledSubject . next ( ) ;
158
164
this . currentConnection = null ;
165
+ this . connectedSubject . next ( false ) ;
159
166
}
160
167
161
168
private dispatchResponse ( response : Response ) {
162
169
this . listeners . forEach ( l => l . onResponse ( response ) ) ;
163
- this . invocations
164
- . get ( response . invocationId )
165
- . next ( response . result ) ;
170
+ if ( this . invocations && this . invocations . has ( response . invocationId ) ) {
171
+ this . invocations
172
+ . get ( response . invocationId )
173
+ . next ( response . result ) ;
174
+ }
166
175
}
167
176
}
0 commit comments