[Room] enhance performance

This commit is contained in:
Christian Pauly 2019-10-24 09:39:39 +00:00
parent 4f3e88611f
commit 9049d3b4f5
3 changed files with 19 additions and 1 deletions

View File

@ -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.

View File

@ -59,6 +59,7 @@ class RoomList {
StreamSubscription<EventUpdate> eventSub;
StreamSubscription<RoomUpdate> roomSub;
StreamSubscription<bool> 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();
}

View File

@ -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<int> 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<int> 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<int> insertList = [];