[Client] Make callbacks to streams

This commit is contained in:
Christian Pauly 2020-01-04 11:29:38 +01:00
parent cc360f2bc2
commit 90a06ebce5
3 changed files with 21 additions and 21 deletions

View file

@ -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<String, Presence> 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<dynamic> onSync = StreamController.broadcast();
/// Callback will be called on presences.
final StreamController<Presence> onPresence = StreamController.broadcast();
/// Callback will be called on account data updates.
final StreamController<AccountData> 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();
}

View file

@ -21,6 +21,8 @@
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
*/
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<String> 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;
}

View file

@ -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);