@@ -127,9 +127,11 @@ export abstract class NatsService {
127
127
* @param subject
128
128
* @param data
129
129
* @param isResponseCallback
130
+ * @param externalMessageId
130
131
*/
131
- public sendMessage < T > ( subject : string , data ?: unknown , isResponseCallback : boolean = true ) : Promise < T > {
132
- const messageId = GenerateUUIDv4 ( ) ;
132
+ public sendMessage < T > ( subject : string , data ?: unknown , isResponseCallback : boolean = true , externalMessageId ?: string ) : Promise < T > {
133
+ const messageId = externalMessageId ?? GenerateUUIDv4 ( ) ;
134
+
133
135
return new Promise ( async ( resolve , reject ) => {
134
136
const head = headers ( ) ;
135
137
head . append ( 'messageId' , messageId ) ;
@@ -159,14 +161,18 @@ export abstract class NatsService {
159
161
* @param data
160
162
*/
161
163
public sendMessageWithTimeout < T > ( subject : string , timeout : number , data ?: unknown ) : Promise < T > {
162
- return Promise . race ( [
163
- this . sendMessage < T > ( subject , data ) ,
164
- new Promise < T > ( ( _ , reject ) => {
165
- setTimeout ( ( ) => {
166
- reject ( new Error ( 'Timeout exceed' ) )
167
- } , timeout )
168
- } )
169
- ] )
164
+ const messageId = GenerateUUIDv4 ( ) ;
165
+
166
+ const messagePromise = this . sendMessage < T > ( subject , data , true , messageId ) ;
167
+
168
+ const timeoutPromise = new Promise < T > ( ( _ , reject ) => {
169
+ setTimeout ( ( ) => {
170
+ this . responseCallbacksMap . delete ( messageId ) ;
171
+ reject ( new Error ( 'Timeout exceed' ) ) ;
172
+ } , timeout ) ;
173
+ } ) ;
174
+
175
+ return Promise . race ( [ messagePromise , timeoutPromise ] ) ;
170
176
}
171
177
172
178
/**
0 commit comments