Skip to content

Commit

Permalink
Implement UnSubscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
muzzammilshahid committed Apr 29, 2024
1 parent 859ed89 commit 9691c45
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/src/session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Session {
final Map<int, Completer<Published>> publishRequests = {};
final Map<int, SubscribeRequest> subscribeRequests = {};
final Map<int, void Function(Event)> subscriptions = {};
final Map<int, UnsubscribeRequest> unsubscribeRequests = {};

void processIncomingMessage(Message msg) {
if (msg is Result) {
Expand Down Expand Up @@ -74,6 +75,12 @@ class Session {
if (endpoint != null) {
endpoint(msg);
}
} else if (msg is UnSubscribed) {
var request = unsubscribeRequests.remove(msg.requestID);
if (request != null) {
subscriptions.remove(request.subscriptionId);
request.future.complete();
}
}
}

Expand Down Expand Up @@ -143,4 +150,14 @@ class Session {

return completer.future;
}

Future<void> unsubscribe(Subscription sub) {
var unsubscribe = UnSubscribe(nextID, sub.subscriptionId);

var completer = Completer<void>();
unsubscribeRequests[unsubscribe.requestID] = UnsubscribeRequest(completer, sub.subscriptionId);
baseSession.send(wampSession.sendMessage(unsubscribe));

return completer.future;
}
}
7 changes: 7 additions & 0 deletions lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ class SubscribeRequest {
final Completer<Subscription> future;
final void Function(Event) endpoint;
}

class UnsubscribeRequest {
UnsubscribeRequest(this.future, this.subscriptionId);

final Completer<void> future;
final int subscriptionId;
}

0 comments on commit 9691c45

Please sign in to comment.