From 4af89c6de474fef2c81285d4f718e6bfdafc3135 Mon Sep 17 00:00:00 2001 From: Reza Date: Wed, 22 Jun 2022 10:43:32 +0200 Subject: [PATCH] fix:implement sending queue --- lib/src/room.dart | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/src/room.dart b/lib/src/room.dart index 3e243f0a..accabe4c 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -89,6 +89,8 @@ class Room { /// Key-Value store for private account data only visible for this user. Map roomAccountData = {}; + final _sendingQueue = []; + Map toJson() => { 'id': id, 'membership': membership.toString().split('.').last, @@ -893,6 +895,7 @@ class Room { /// Sends an event to this room with this json as a content. Returns the /// event ID generated from the server. + /// It uses list of completer to make sure events are sending in a row. Future sendEvent( Map content, { String type = EventTypes.Message, @@ -977,10 +980,16 @@ class Room { ), ); await _handleFakeSync(syncUpdate); + final completer = Completer(); + _sendingQueue.add(completer); + while (_sendingQueue.first != completer) { + await _sendingQueue.first.future; + } final timeoutDate = DateTime.now().add(client.sendTimelineEventTimeout); // Send the text and on success, store and display a *sent* event. String? res; + while (res == null) { try { res = await _sendContent( @@ -994,6 +1003,8 @@ class Room { syncUpdate.rooms!.join!.values.first.timeline!.events!.first .unsigned![messageSendingStatusKey] = EventStatus.error.intValue; await _handleFakeSync(syncUpdate); + completer.complete(); + _sendingQueue.remove(completer); return null; } Logs().w('Problem while sending message: $e Try again in 1 seconds...'); @@ -1004,6 +1015,8 @@ class Room { .unsigned![messageSendingStatusKey] = EventStatus.sent.intValue; syncUpdate.rooms!.join!.values.first.timeline!.events!.first.eventId = res; await _handleFakeSync(syncUpdate); + completer.complete(); + _sendingQueue.remove(completer); return res; }