From 9049d3b4f5d7dfab4714598303dd485666db3414 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Thu, 24 Oct 2019 09:39:39 +0000 Subject: [PATCH] [Room] enhance performance --- lib/src/Room.dart | 10 ++++++++++ lib/src/RoomList.dart | 7 ++++++- test/RoomList_test.dart | 3 +++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/src/Room.dart b/lib/src/Room.dart index df14e58..8ed128b 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -27,6 +27,7 @@ import 'package:famedlysdk/src/RoomAccountData.dart'; import 'package:famedlysdk/src/RoomState.dart'; import 'package:famedlysdk/src/responses/ErrorResponse.dart'; import 'package:famedlysdk/src/sync/EventUpdate.dart'; +import 'package:famedlysdk/src/sync/RoomUpdate.dart'; import 'package:famedlysdk/src/utils/ChatTime.dart'; import 'package:famedlysdk/src/utils/MatrixFile.dart'; import 'package:famedlysdk/src/utils/MxContent.dart'; @@ -532,6 +533,15 @@ class Room { client.connection.onEvent.add(eventUpdate); } } + client.connection.onRoomUpdate.add( + RoomUpdate( + id: id, + membership: membership, + prev_batch: resp["end"], + notification_count: notificationCount, + highlight_count: highlightCount, + ), + ); } /// Sets this room as a direct chat for this user. diff --git a/lib/src/RoomList.dart b/lib/src/RoomList.dart index 9f38dc3..5ea3d51 100644 --- a/lib/src/RoomList.dart +++ b/lib/src/RoomList.dart @@ -59,6 +59,7 @@ class RoomList { StreamSubscription eventSub; StreamSubscription roomSub; + StreamSubscription firstSyncSub; RoomList( {this.client, @@ -69,6 +70,8 @@ class RoomList { this.onlyLeft = false}) { eventSub ??= client.connection.onEvent.stream.listen(_handleEventUpdate); roomSub ??= client.connection.onRoomUpdate.stream.listen(_handleRoomUpdate); + firstSyncSub ??= + client.connection.onFirstSync.stream.listen((b) => sortAndUpdate()); sort(); } @@ -148,6 +151,7 @@ class RoomList { } void _handleEventUpdate(EventUpdate eventUpdate) { + if (eventUpdate.type == "history") return; // Search the room in the rooms num j = 0; for (j = 0; j < rooms.length; j++) { @@ -170,7 +174,7 @@ class RoomList { RoomAccountData.fromJson(eventUpdate.content, rooms[j]); } if (rooms[j].onUpdate != null) rooms[j].onUpdate(); - sortAndUpdate(); + if (eventUpdate.type == "timeline") sortAndUpdate(); } bool sortLock = false; @@ -184,6 +188,7 @@ class RoomList { } sortAndUpdate() { + if (client.prevBatch == null) return; sort(); if (onUpdate != null) onUpdate(); } diff --git a/test/RoomList_test.dart b/test/RoomList_test.dart index aa7fe11..0017c7b 100644 --- a/test/RoomList_test.dart +++ b/test/RoomList_test.dart @@ -37,6 +37,7 @@ void main() { test("Create and insert one room", () async { final Client client = Client("testclient"); client.homeserver = "https://testserver.abc"; + client.prevBatch = "1234"; int updateCount = 0; List insertList = []; @@ -85,6 +86,7 @@ void main() { test("Restort", () async { final Client client = Client("testclient"); client.homeserver = "https://testserver.abc"; + client.prevBatch = "1234"; int updateCount = 0; List insertList = []; @@ -198,6 +200,7 @@ void main() { test("onlyLeft", () async { final Client client = Client("testclient"); client.homeserver = "https://testserver.abc"; + client.prevBatch = "1234"; int updateCount = 0; List insertList = [];