@@ -24,6 +24,7 @@ import { TypeDaprBindingCallback } from "../../../types/DaprBindingCallback.type
24
24
import { TypeDaprPubSubCallback } from "../../../types/DaprPubSubCallback.type" ;
25
25
import { Logger } from "../../../logger/Logger" ;
26
26
import { LoggerOptions } from "../../../types/logger/LoggerOptions" ;
27
+ import { KeyValueType } from "../../../types/KeyValue.type" ;
27
28
28
29
29
30
// https://github.com/badsyntax/grpc-js-typescript/issues/1#issuecomment-705419742
@@ -33,13 +34,13 @@ export default class GRPCServerImpl implements IAppCallbackServer {
33
34
34
35
handlersInvoke : { [ key : string ] : TypeDaprInvokerCallback } ;
35
36
handlersBindings : { [ key : string ] : TypeDaprBindingCallback } ;
36
- handlersTopics : { [ key : string ] : TypeDaprPubSubCallback } ;
37
+ registrationsTopics : { [ key : string ] : { cb : TypeDaprPubSubCallback , metadata : KeyValueType } } ;
37
38
38
39
constructor ( loggerOptions ?: LoggerOptions ) {
39
40
this . logger = new Logger ( "GRPCServer" , "GRPCServerImpl" , loggerOptions ) ;
40
41
this . handlersInvoke = { } ;
41
42
this . handlersBindings = { } ;
42
- this . handlersTopics = { } ;
43
+ this . registrationsTopics = { } ;
43
44
}
44
45
45
46
createPubSubSubscriptionHandlerKey ( pubSubName : string , topicName : string ) : string {
@@ -59,9 +60,9 @@ export default class GRPCServerImpl implements IAppCallbackServer {
59
60
this . handlersInvoke [ handlerKey ] = cb ;
60
61
}
61
62
62
- registerPubSubSubscriptionHandler ( pubSubName : string , topicName : string , cb : TypeDaprInvokerCallback ) : void {
63
+ registerPubSubSubscriptionHandler ( pubSubName : string , topicName : string , cb : TypeDaprInvokerCallback , metadata : KeyValueType = { } ) : void {
63
64
const handlerKey = this . createPubSubSubscriptionHandlerKey ( pubSubName , topicName ) ;
64
- this . handlersTopics [ handlerKey ] = cb ;
65
+ this . registrationsTopics [ handlerKey ] = { cb : cb , metadata } ;
65
66
}
66
67
67
68
registerInputBindingHandler ( bindingName : string , cb : TypeDaprInvokerCallback ) : void {
@@ -144,8 +145,8 @@ export default class GRPCServerImpl implements IAppCallbackServer {
144
145
const req = call . request ;
145
146
const handlerKey = this . createPubSubSubscriptionHandlerKey ( req . getPubsubName ( ) , req . getTopic ( ) ) ;
146
147
147
- if ( ! this . handlersTopics [ handlerKey ] ) {
148
- this . logger . warn ( `Event from topic: "${ handlerKey } " was not handled` ) ;
148
+ if ( ! this . registrationsTopics [ handlerKey ] ) {
149
+ this . logger . warn ( `Topic "${ handlerKey } " unregistered, event was not handled. ` ) ;
149
150
return ;
150
151
}
151
152
@@ -161,7 +162,7 @@ export default class GRPCServerImpl implements IAppCallbackServer {
161
162
const res = new TopicEventResponse ( ) ;
162
163
163
164
try {
164
- await this . handlersTopics [ handlerKey ] ( dataParsed ) ;
165
+ await ( this . registrationsTopics [ handlerKey ] ) . cb ( dataParsed ) ;
165
166
res . setStatus ( TopicEventResponse . TopicEventResponseStatus . SUCCESS ) ;
166
167
} catch ( e ) {
167
168
// @todo : for now we drop, maybe we should allow retrying as well more easily?
@@ -175,19 +176,22 @@ export default class GRPCServerImpl implements IAppCallbackServer {
175
176
// @todo : WIP
176
177
async listTopicSubscriptions ( call : grpc . ServerUnaryCall < Empty , ListTopicSubscriptionsResponse > , callback : grpc . sendUnaryData < ListTopicSubscriptionsResponse > ) : Promise < void > {
177
178
const res = new ListTopicSubscriptionsResponse ( ) ;
179
+ const topicSubscriptions : TopicSubscription [ ] = [ ] ;
178
180
179
- const values = Object . keys ( this . handlersTopics ) . map ( ( i ) => {
180
- const handlerTopic = i . split ( "|" ) ;
181
-
181
+ for ( const [ key , value ] of Object . entries ( this . registrationsTopics ) ) {
182
+ const [ pubSubName , topicName ] = key . split ( "|" ) ;
182
183
const topicSubscription = new TopicSubscription ( ) ;
183
- topicSubscription . setPubsubName ( handlerTopic [ 0 ] ) ;
184
- topicSubscription . setTopic ( handlerTopic [ 1 ] ) ;
185
184
186
- return topicSubscription ;
187
- } ) ;
185
+ topicSubscription . setPubsubName ( pubSubName ) ;
186
+ topicSubscription . setTopic ( topicName ) ;
187
+ for ( const [ mKey , mValue ] of Object . entries ( value . metadata ) ) {
188
+ topicSubscription . getMetadataMap ( ) . set ( mKey , mValue ) ;
189
+ }
188
190
189
- res . setSubscriptionsList ( values ) ;
191
+ topicSubscriptions . push ( topicSubscription ) ;
192
+ }
190
193
194
+ res . setSubscriptionsList ( topicSubscriptions ) ;
191
195
return callback ( null , res ) ;
192
196
}
193
197
@@ -197,4 +201,4 @@ export default class GRPCServerImpl implements IAppCallbackServer {
197
201
res . setBindingsList ( Object . keys ( this . handlersBindings ) ) ;
198
202
return callback ( null , res ) ;
199
203
}
200
- }
204
+ }
0 commit comments