Merge branch 'callbacks-fix-make-streams' into 'master'

[Client] Make callbacks to streams

See merge request famedly/famedlysdk!148
This commit is contained in:
Christian Pauly 2020-01-04 10:36:04 +00:00
commit ae1c757e4e
3 changed files with 21 additions and 21 deletions

View File

@ -44,8 +44,6 @@ import 'sync/RoomUpdate.dart';
import 'sync/UserUpdate.dart'; import 'sync/UserUpdate.dart';
import 'utils/MatrixException.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); typedef RoomSorter = int Function(Room a, Room b);
enum HTTPType { GET, POST, PUT, DELETE } enum HTTPType { GET, POST, PUT, DELETE }
@ -121,12 +119,6 @@ class Client {
/// Presences of users by a given matrix ID /// Presences of users by a given matrix ID
Map<String, Presence> presences = {}; 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) { Room getRoomByAlias(String alias) {
for (int i = 0; i < rooms.length; i++) { for (int i = 0; i < rooms.length; i++) {
if (rooms[i].canonicalAlias == alias) return rooms[i]; if (rooms[i].canonicalAlias == alias) return rooms[i];
@ -145,12 +137,12 @@ class Client {
if (userUpdate.type == "account_data") { if (userUpdate.type == "account_data") {
AccountData newAccountData = AccountData.fromJson(userUpdate.content); AccountData newAccountData = AccountData.fromJson(userUpdate.content);
accountData[newAccountData.typeKey] = newAccountData; accountData[newAccountData.typeKey] = newAccountData;
if (onAccountData != null) onAccountData(newAccountData); if (onAccountData != null) onAccountData.add(newAccountData);
} }
if (userUpdate.type == "presence") { if (userUpdate.type == "presence") {
Presence newPresence = Presence.fromJson(userUpdate.content); Presence newPresence = Presence.fromJson(userUpdate.content);
presences[newPresence.sender] = newPresence; 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. /// When a new sync response is coming in, this gives the complete payload.
final StreamController<dynamic> onSync = StreamController.broadcast(); 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 /// Matrix synchronisation is done with https long polling. This needs a
/// timeout which is usually 30 seconds. /// timeout which is usually 30 seconds.
int syncTimeoutSec = 30; int syncTimeoutSec = 30;
@ -948,7 +947,7 @@ class Client {
rooms[j].mInvitedMemberCount = chatUpdate.summary.mInvitedMemberCount; 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(); _sortRooms();
} }
@ -992,7 +991,7 @@ class Client {
rooms[j].ephemerals[eventUpdate.eventType] = rooms[j].ephemerals[eventUpdate.eventType] =
RoomAccountData.fromJson(eventUpdate.content, rooms[j]); 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(); if (eventUpdate.type == "timeline") _sortRooms();
} }

View File

@ -21,6 +21,8 @@
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>. * along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
*/ */
import 'dart:async';
import 'package:famedlysdk/src/Client.dart'; import 'package:famedlysdk/src/Client.dart';
import 'package:famedlysdk/src/Event.dart'; import 'package:famedlysdk/src/Event.dart';
import 'package:famedlysdk/src/RoomAccountData.dart'; import 'package:famedlysdk/src/RoomAccountData.dart';
@ -35,8 +37,6 @@ import './User.dart';
import 'Timeline.dart'; import 'Timeline.dart';
import 'utils/StatesMap.dart'; import 'utils/StatesMap.dart';
typedef onRoomUpdate = void Function();
/// Represents a Matrix room. /// Represents a Matrix room.
class Room { class Room {
/// The full qualified Matrix ID for the room in the format '!localid:server.abc'. /// 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"] ? roomAccountData["m.fully_read"].content["event_id"]
: ""; : "";
/// If something changes, this callback will be triggered. /// If something changes, this callback will be triggered. Will return the
onRoomUpdate onUpdate; /// room id.
final StreamController<String> onUpdate = StreamController.broadcast();
/// The name of the room if set by a participant. /// The name of the room if set by a participant.
String get name => states["m.room.name"] != null String get name => states["m.room.name"] != null
@ -826,7 +827,7 @@ class Room {
return; return;
}); });
} }
if (onUpdate != null) onUpdate(); if (onUpdate != null) onUpdate.add(id);
_requestingMatrixIds.remove(mxID); _requestingMatrixIds.remove(mxID);
return user; return user;
} }

View File

@ -59,12 +59,12 @@ void main() {
test('Login', () async { test('Login', () async {
int presenceCounter = 0; int presenceCounter = 0;
int accountDataCounter = 0; int accountDataCounter = 0;
matrix.onPresence = (Presence data) { matrix.onPresence.stream.listen((Presence data) {
presenceCounter++; presenceCounter++;
}; });
matrix.onAccountData = (AccountData data) { matrix.onAccountData.stream.listen((AccountData data) {
accountDataCounter++; accountDataCounter++;
}; });
expect(matrix.homeserver, null); expect(matrix.homeserver, null);
expect(matrix.matrixVersions, null); expect(matrix.matrixVersions, null);