From 940e976a670213a9ee8c6e2623bc51e45f4378bd Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Mon, 27 May 2024 21:49:42 +0200 Subject: [PATCH] fix: prevent premature cancellation of observerelation --- lib/src/coap_client.dart | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/src/coap_client.dart b/lib/src/coap_client.dart index c8c4edf3..0c8f8f4d 100644 --- a/lib/src/coap_client.dart +++ b/lib/src/coap_client.dart @@ -513,16 +513,23 @@ class CoapClient { final CoapRequest request, { final int maxRetransmit = 0, }) async { + var isObserving = false; request ..observe = ObserveRegistration.register.value ..maxRetransmit = maxRetransmit; - final responseStream = _sendWithStreamResponse(request) - .asBroadcastStream(onCancel: (final sub) => sub.cancel()); + final responseStream = _sendWithStreamResponse(request).asBroadcastStream( + onCancel: (final sub) { + if (isObserving) { + sub.cancel(); + } + }, + ); final relation = CoapObserveClientRelation(request, responseStream); final resp = await _waitForResponse(request, responseStream); if (!resp.hasOption()) { relation.isCancelled = true; } + isObserving = true; return relation; }