You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// If provided, the writer will use the transaction for writing messages.
26
+
tx?: TX
23
27
// Path to the topic to write to.
24
28
// Example: "/Root/my-topic"
25
29
topic: string
@@ -118,9 +122,13 @@ export function createTopicWriter(driver: Driver, options: TopicWriterOptions):
118
122
// When the writer stream is not ready, it will queue messages to be sent later.
119
123
letisReady=false;
120
124
// Flag to indicate if the writer is closed.
121
-
// When the writer is closed, it will not accept new messages and will reject all pending write requests.
122
-
// This is useful to ensure that the writer does not leak resources and can be closed gracefully.
125
+
// When the writer is closed, it will not accept new messages.
126
+
// The writer will still process and acknowledge any messages that were already sent.
123
127
letisClosed=false;
128
+
// Flag to indicate if the writer is disposed.
129
+
// When the writer is disposed, it will not accept new messages and will reject all pending write requests.
130
+
// This is useful to ensure that the writer does not leak resources and can be closed gracefully.
131
+
letisDisposed=false;
124
132
// Queue for messages to be written.
125
133
// This is used to store messages that are not yet sent to the topic service.
126
134
letqueue: {
@@ -153,6 +161,7 @@ export function createTopicWriter(driver: Driver, options: TopicWriterOptions):
153
161
154
162
for(constitemofqq){
155
163
_write({
164
+
tx: options.tx,
156
165
queue: outgoing,
157
166
codec: codec,
158
167
lastSeqNo: (lastSeqNo||item.extra.seqNo)!,
@@ -184,6 +193,7 @@ export function createTopicWriter(driver: Driver, options: TopicWriterOptions):
184
193
awaitdriver.ready(signal);
185
194
186
195
_flush({
196
+
tx: options.tx,
187
197
queue: outgoing,
188
198
codec: codec,
189
199
buffer,
@@ -270,6 +280,7 @@ export function createTopicWriter(driver: Driver, options: TopicWriterOptions):
270
280
break
271
281
case'writeResponse':
272
282
_on_write_response({
283
+
tx: options.tx,
273
284
queue: outgoing,
274
285
codec: codec,
275
286
buffer,
@@ -322,6 +333,10 @@ export function createTopicWriter(driver: Driver, options: TopicWriterOptions):
322
333
thrownewError('Writer is closed, cannot write messages');
323
334
}
324
335
336
+
if(isDisposed){
337
+
thrownewError('Writer is disposed, cannot write messages');
338
+
}
339
+
325
340
if(!extra.seqNo&&isSeqNoProvided){
326
341
thrownewError('Missing sequence number for message. Sequence number is provided by the user previously, so after that all messages must have seqNo provided');
327
342
}
@@ -344,6 +359,7 @@ export function createTopicWriter(driver: Driver, options: TopicWriterOptions):
344
359
}
345
360
346
361
return_write({
362
+
tx: options.tx,
347
363
queue: outgoing,
348
364
codec: codec,
349
365
buffer,
@@ -363,7 +379,7 @@ export function createTopicWriter(driver: Driver, options: TopicWriterOptions):
363
379
// If a reason is provided, it will be used to reject all pending acks and write requests.
364
380
// If no reason is provided, it will use a default error message.
365
381
functionclose(reason?: Error){
366
-
if(isClosed){
382
+
if(isDisposed){
367
383
return;
368
384
}
369
385
@@ -395,9 +411,18 @@ export function createTopicWriter(driver: Driver, options: TopicWriterOptions):
395
411
}
396
412
397
413
isClosed=true;
414
+
isDisposed=true;
398
415
dbg('writer closed, reason: %O',reason);
399
416
}
400
417
418
+
// Before committing the transaction, require all messages to be written and acknowledged.
419
+
options.tx?.registerPrecommitHook(async()=>{
420
+
// Close the writer. Do not accept new messages.
421
+
isClosed=true;
422
+
// Wait for all messages to be flushed.
423
+
awaitflush();
424
+
})
425
+
401
426
return{
402
427
flush,
403
428
write,
@@ -413,3 +438,14 @@ export function createTopicWriter(driver: Driver, options: TopicWriterOptions):
0 commit comments