From 4d1e5e3ed3eb1a3bd9f2070cb92680021e55218a Mon Sep 17 00:00:00 2001
From: Christian Pauly <christian-pauly@posteo.de>
Date: Mon, 6 Jan 2020 20:21:25 +0000
Subject: [PATCH] [Store] Async storing of prev batch

---
 lib/src/client.dart    | 11 +++++++----
 lib/src/store_api.dart |  4 ++--
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/lib/src/client.dart b/lib/src/client.dart
index 760230a..f5fc9bd 100644
--- a/lib/src/client.dart
+++ b/lib/src/client.dart
@@ -721,13 +721,16 @@ class Client {
       if (this.store != null) {
         await this.store.transaction(() {
           handleSync(syncResp);
-          this.store.storePrevBatch(syncResp);
-          return;
+          this.store.storePrevBatch(syncResp["next_batch"]);
         });
       } else {
         await handleSync(syncResp);
       }
-      if (this.prevBatch == null) this.onFirstSync.add(true);
+      if (this.prevBatch == null) {
+        this.onFirstSync.add(true);
+        this.prevBatch = syncResp["next_batch"];
+        _sortRooms();
+      }
       this.prevBatch = syncResp["next_batch"];
       if (hash == _syncRequest.hashCode) unawaited(_sync());
     } on MatrixException catch (exception) {
@@ -1042,7 +1045,7 @@ class Client {
       .compareTo(a.timeCreated.millisecondsSinceEpoch);
 
   _sortRooms() {
-    if (_sortLock || rooms.length < 2) return;
+    if (prevBatch == null || _sortLock || rooms.length < 2) return;
     _sortLock = true;
     rooms?.sort(sortRoomsBy);
     _sortLock = false;
diff --git a/lib/src/store_api.dart b/lib/src/store_api.dart
index bd55f0f..dd3bce5 100644
--- a/lib/src/store_api.dart
+++ b/lib/src/store_api.dart
@@ -48,11 +48,11 @@ abstract class StoreAPI {
 
   var txn;
 
-  Future<void> transaction(Future<void> queries());
+  Future<void> transaction(void queries());
 
   /// Will be automatically called on every synchronisation. Must be called inside of
   //  /// [transaction].
-  Future<void> storePrevBatch(dynamic sync);
+  void storePrevBatch(String prevBatch);
 
   Future<void> storeRoomPrevBatch(Room room);