fix:implement sending queue

This commit is contained in:
Reza 2022-06-22 10:43:32 +02:00
parent 96c420fbf9
commit 4af89c6de4
1 changed files with 13 additions and 0 deletions

View File

@ -89,6 +89,8 @@ class Room {
/// Key-Value store for private account data only visible for this user. /// Key-Value store for private account data only visible for this user.
Map<String, BasicRoomEvent> roomAccountData = {}; Map<String, BasicRoomEvent> roomAccountData = {};
final _sendingQueue = <Completer>[];
Map<String, dynamic> toJson() => { Map<String, dynamic> toJson() => {
'id': id, 'id': id,
'membership': membership.toString().split('.').last, '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 /// Sends an event to this room with this json as a content. Returns the
/// event ID generated from the server. /// event ID generated from the server.
/// It uses list of completer to make sure events are sending in a row.
Future<String?> sendEvent( Future<String?> sendEvent(
Map<String, dynamic> content, { Map<String, dynamic> content, {
String type = EventTypes.Message, String type = EventTypes.Message,
@ -977,10 +980,16 @@ class Room {
), ),
); );
await _handleFakeSync(syncUpdate); await _handleFakeSync(syncUpdate);
final completer = Completer();
_sendingQueue.add(completer);
while (_sendingQueue.first != completer) {
await _sendingQueue.first.future;
}
final timeoutDate = DateTime.now().add(client.sendTimelineEventTimeout); final timeoutDate = DateTime.now().add(client.sendTimelineEventTimeout);
// Send the text and on success, store and display a *sent* event. // Send the text and on success, store and display a *sent* event.
String? res; String? res;
while (res == null) { while (res == null) {
try { try {
res = await _sendContent( res = await _sendContent(
@ -994,6 +1003,8 @@ class Room {
syncUpdate.rooms!.join!.values.first.timeline!.events!.first syncUpdate.rooms!.join!.values.first.timeline!.events!.first
.unsigned![messageSendingStatusKey] = EventStatus.error.intValue; .unsigned![messageSendingStatusKey] = EventStatus.error.intValue;
await _handleFakeSync(syncUpdate); await _handleFakeSync(syncUpdate);
completer.complete();
_sendingQueue.remove(completer);
return null; return null;
} }
Logs().w('Problem while sending message: $e Try again in 1 seconds...'); Logs().w('Problem while sending message: $e Try again in 1 seconds...');
@ -1004,6 +1015,8 @@ class Room {
.unsigned![messageSendingStatusKey] = EventStatus.sent.intValue; .unsigned![messageSendingStatusKey] = EventStatus.sent.intValue;
syncUpdate.rooms!.join!.values.first.timeline!.events!.first.eventId = res; syncUpdate.rooms!.join!.values.first.timeline!.events!.first.eventId = res;
await _handleFakeSync(syncUpdate); await _handleFakeSync(syncUpdate);
completer.complete();
_sendingQueue.remove(completer);
return res; return res;
} }