Merge branch 'reza/sending-queue' into 'main'

fix: implement sending queue

Closes #257

See merge request famedly/company/frontend/famedlysdk!1061
This commit is contained in:
Krille Fear 2022-07-01 06:34:22 +00:00
commit c3a9d64329
1 changed files with 13 additions and 0 deletions

View File

@ -90,6 +90,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,
@ -894,6 +896,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,
@ -978,10 +981,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(
@ -995,6 +1004,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...');
@ -1005,6 +1016,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;
} }