Skip to content

Commit

Permalink
fix: prevent premature cancellation of observerelation (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed May 31, 2024
1 parent bb85a54 commit 2e08045
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/src/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<ObserveOption>()) {
relation.isCancelled = true;
}
isObserving = true;
return relation;
}

Expand Down

0 comments on commit 2e08045

Please sign in to comment.