From 90a06ebce547ed853e501532a03491356a93e483 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Sat, 4 Jan 2020 11:29:38 +0100 Subject: [PATCH] [Client] Make callbacks to streams --- lib/src/Client.dart | 23 +++++++++++------------ lib/src/Room.dart | 11 ++++++----- test/Client_test.dart | 8 ++++---- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/src/Client.dart b/lib/src/Client.dart index 20cad8a..3df69b8 100644 --- a/lib/src/Client.dart +++ b/lib/src/Client.dart @@ -44,8 +44,6 @@ import 'sync/RoomUpdate.dart'; import 'sync/UserUpdate.dart'; import 'utils/MatrixException.dart'; -typedef AccountDataEventCB = void Function(AccountData accountData); -typedef PresenceCB = void Function(Presence presence); typedef RoomSorter = int Function(Room a, Room b); enum HTTPType { GET, POST, PUT, DELETE } @@ -121,12 +119,6 @@ class Client { /// Presences of users by a given matrix ID Map presences = {}; - /// Callback will be called on account data updates. - AccountDataEventCB onAccountData; - - /// Callback will be called on presences. - PresenceCB onPresence; - Room getRoomByAlias(String alias) { for (int i = 0; i < rooms.length; i++) { if (rooms[i].canonicalAlias == alias) return rooms[i]; @@ -145,12 +137,12 @@ class Client { if (userUpdate.type == "account_data") { AccountData newAccountData = AccountData.fromJson(userUpdate.content); accountData[newAccountData.typeKey] = newAccountData; - if (onAccountData != null) onAccountData(newAccountData); + if (onAccountData != null) onAccountData.add(newAccountData); } if (userUpdate.type == "presence") { Presence newPresence = Presence.fromJson(userUpdate.content); presences[newPresence.sender] = newPresence; - if (onPresence != null) onPresence(newPresence); + if (onPresence != null) onPresence.add(newPresence); } } @@ -472,6 +464,13 @@ class Client { /// When a new sync response is coming in, this gives the complete payload. final StreamController onSync = StreamController.broadcast(); + /// Callback will be called on presences. + final StreamController onPresence = StreamController.broadcast(); + + /// Callback will be called on account data updates. + final StreamController onAccountData = + StreamController.broadcast(); + /// Matrix synchronisation is done with https long polling. This needs a /// timeout which is usually 30 seconds. int syncTimeoutSec = 30; @@ -948,7 +947,7 @@ class Client { rooms[j].mInvitedMemberCount = chatUpdate.summary.mInvitedMemberCount; } } - if (rooms[j].onUpdate != null) rooms[j].onUpdate(); + if (rooms[j].onUpdate != null) rooms[j].onUpdate.add(rooms[j].id); } _sortRooms(); } @@ -992,7 +991,7 @@ class Client { rooms[j].ephemerals[eventUpdate.eventType] = RoomAccountData.fromJson(eventUpdate.content, rooms[j]); } - if (rooms[j].onUpdate != null) rooms[j].onUpdate(); + if (rooms[j].onUpdate != null) rooms[j].onUpdate.add(rooms[j].id); if (eventUpdate.type == "timeline") _sortRooms(); } diff --git a/lib/src/Room.dart b/lib/src/Room.dart index 6f7f57f..54b04e1 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -21,6 +21,8 @@ * along with famedlysdk. If not, see . */ +import 'dart:async'; + import 'package:famedlysdk/src/Client.dart'; import 'package:famedlysdk/src/Event.dart'; import 'package:famedlysdk/src/RoomAccountData.dart'; @@ -35,8 +37,6 @@ import './User.dart'; import 'Timeline.dart'; import 'utils/StatesMap.dart'; -typedef onRoomUpdate = void Function(); - /// Represents a Matrix room. class Room { /// The full qualified Matrix ID for the room in the format '!localid:server.abc'. @@ -91,8 +91,9 @@ class Room { ? roomAccountData["m.fully_read"].content["event_id"] : ""; - /// If something changes, this callback will be triggered. - onRoomUpdate onUpdate; + /// If something changes, this callback will be triggered. Will return the + /// room id. + final StreamController onUpdate = StreamController.broadcast(); /// The name of the room if set by a participant. String get name => states["m.room.name"] != null @@ -826,7 +827,7 @@ class Room { return; }); } - if (onUpdate != null) onUpdate(); + if (onUpdate != null) onUpdate.add(id); _requestingMatrixIds.remove(mxID); return user; } diff --git a/test/Client_test.dart b/test/Client_test.dart index 4133bda..da48cf5 100644 --- a/test/Client_test.dart +++ b/test/Client_test.dart @@ -59,12 +59,12 @@ void main() { test('Login', () async { int presenceCounter = 0; int accountDataCounter = 0; - matrix.onPresence = (Presence data) { + matrix.onPresence.stream.listen((Presence data) { presenceCounter++; - }; - matrix.onAccountData = (AccountData data) { + }); + matrix.onAccountData.stream.listen((AccountData data) { accountDataCounter++; - }; + }); expect(matrix.homeserver, null); expect(matrix.matrixVersions, null);