@@ -13,12 +13,13 @@ limitations under the License.
13
13
14
14
import GRPCClient from './GRPCClient' ;
15
15
import * as grpc from "@grpc/grpc-js" ;
16
- import { GetConfigurationRequest , GetConfigurationResponse , SubscribeConfigurationRequest , SubscribeConfigurationResponse } from '../../../proto/dapr/proto/runtime/v1/dapr_pb' ;
16
+ import { GetConfigurationRequest , GetConfigurationResponse , SubscribeConfigurationRequest , SubscribeConfigurationResponse , UnsubscribeConfigurationRequest , UnsubscribeConfigurationResponse } from '../../../proto/dapr/proto/runtime/v1/dapr_pb' ;
17
17
import IClientConfiguration from '../../../interfaces/Client/IClientConfiguration' ;
18
18
import { KeyValueType } from '../../../types/KeyValue.type' ;
19
19
import { GetConfigurationResponse as GetConfigurationResponseResult } from '../../../types/configuration/GetConfigurationResponse' ;
20
20
import { SubscribeConfigurationResponse as SubscribeConfigurationResponseResult } from '../../../types/configuration/SubscribeConfigurationResponse' ;
21
21
import { SubscribeConfigurationCallback } from '../../../types/configuration/SubscribeConfigurationCallback' ;
22
+ import { SubscribeConfigurationStream } from '../../../types/configuration/SubscribeConfigurationStream' ;
22
23
23
24
export default class GRPCClientConfiguration implements IClientConfiguration {
24
25
client : GRPCClient ;
@@ -68,19 +69,19 @@ export default class GRPCClientConfiguration implements IClientConfiguration {
68
69
} ) ;
69
70
}
70
71
71
- async subscribe ( storeName : string , cb : SubscribeConfigurationCallback ) : Promise < void > {
72
+ async subscribe ( storeName : string , cb : SubscribeConfigurationCallback ) : Promise < SubscribeConfigurationStream > {
72
73
return this . _subscribe ( storeName , cb )
73
74
}
74
75
75
- async subscribeWithKeys ( storeName : string , keys : string [ ] , cb : SubscribeConfigurationCallback ) : Promise < void > {
76
+ async subscribeWithKeys ( storeName : string , keys : string [ ] , cb : SubscribeConfigurationCallback ) : Promise < SubscribeConfigurationStream > {
76
77
return this . _subscribe ( storeName , cb , keys )
77
78
}
78
79
79
- async subscribeWithMetadata ( storeName : string , keys : string [ ] , metadata : KeyValueType , cb : SubscribeConfigurationCallback ) : Promise < void > {
80
+ async subscribeWithMetadata ( storeName : string , keys : string [ ] , metadata : KeyValueType , cb : SubscribeConfigurationCallback ) : Promise < SubscribeConfigurationStream > {
80
81
return this . _subscribe ( storeName , cb , keys , metadata )
81
82
}
82
83
83
- async _subscribe ( storeName : string , cb : SubscribeConfigurationCallback , keys ?: string [ ] , metadataObj ?: KeyValueType ) : Promise < void > {
84
+ async _subscribe ( storeName : string , cb : SubscribeConfigurationCallback , keys ?: string [ ] , metadataObj ?: KeyValueType ) : Promise < SubscribeConfigurationStream > {
84
85
const metadata = new grpc . Metadata ( ) ;
85
86
86
87
const msg = new SubscribeConfigurationRequest ( ) ;
@@ -105,8 +106,11 @@ export default class GRPCClientConfiguration implements IClientConfiguration {
105
106
// we will thus create a set with our listeners so we don't
106
107
// break on multi listeners
107
108
const stream = client . subscribeConfigurationAlpha1 ( msg , metadata ) ;
109
+ let streamId : string ;
108
110
109
111
stream . on ( "data" , async ( data : SubscribeConfigurationResponse ) => {
112
+ streamId = data . getId ( ) ;
113
+
110
114
const wrapped : SubscribeConfigurationResponseResult = {
111
115
items : data . getItemsList ( ) . map ( ( item ) => ( {
112
116
key : item . getKey ( ) ,
@@ -122,6 +126,27 @@ export default class GRPCClientConfiguration implements IClientConfiguration {
122
126
123
127
await cb ( wrapped ) ;
124
128
} ) ;
125
- }
126
129
130
+ return {
131
+ stop : async ( ) => {
132
+ return new Promise ( ( resolve , reject ) => {
133
+ const req = new UnsubscribeConfigurationRequest ( ) ;
134
+ req . setStoreName ( storeName ) ;
135
+ req . setId ( streamId ) ;
136
+
137
+ client . unsubscribeConfigurationAlpha1 ( req , ( err , res : UnsubscribeConfigurationResponse ) => {
138
+ if ( err || ! res . getOk ( ) ) {
139
+ return reject ( res . getMessage ( ) ) ;
140
+ }
141
+
142
+ // Clean up the node.js event emitter
143
+ stream . removeAllListeners ( ) ;
144
+ stream . destroy ( ) ;
145
+
146
+ return resolve ( ) ;
147
+ } ) ;
148
+ } )
149
+ }
150
+ } ;
151
+ }
127
152
}
0 commit comments