@@ -2509,35 +2509,35 @@ class ChannelClientState {
25092509
25102510 /// Update channelState with updated information.
25112511 void updateChannelState (ChannelState updatedState) {
2512- final _existingStateMessages = [...messages];
2512+ final _existingStateMessages = < Message > [...messages];
25132513 final newMessages = < Message > [
2514- ..._existingStateMessages.merge (updatedState.messages),
2514+ ..._existingStateMessages.merge (
2515+ updatedState.messages,
2516+ key: (message) => message.id,
2517+ update: (original, updated) => updated.syncWith (original),
2518+ ),
25152519 ].sorted (_sortByCreatedAt);
25162520
2517- final _existingStateWatchers = _channelState.watchers ?? [];
2518- final _updatedStateWatchers = updatedState.watchers ?? [];
2521+ final _existingStateWatchers = < User > [...? _channelState.watchers];
25192522 final newWatchers = < User > [
2520- ..._updatedStateWatchers,
2521- ..._existingStateWatchers
2522- .where ((w) =>
2523- ! _updatedStateWatchers.any ((newWatcher) => newWatcher.id == w.id))
2524- .toList (),
2525- ];
2526-
2527- final newMembers = < Member > [
2528- ...updatedState.members ?? [],
2523+ ..._existingStateWatchers.merge (
2524+ updatedState.watchers,
2525+ key: (watcher) => watcher.id,
2526+ update: (original, updated) => updated,
2527+ ),
25292528 ];
25302529
2531- final _existingStateRead = _channelState.read ?? [];
2532- final _updatedStateRead = updatedState.read ?? [];
2530+ final _existingStateRead = < Read > [...? _channelState.read];
25332531 final newReads = < Read > [
2534- ..._updatedStateRead,
2535- ..._existingStateRead
2536- . where ((r ) =>
2537- ! _updatedStateRead. any ((newRead ) => newRead.user.id == r.user.id))
2538- . toList ( ),
2532+ ..._existingStateRead. merge (
2533+ updatedState.read,
2534+ key : (read ) => read.user.id,
2535+ update : (original, updated ) => updated,
2536+ ),
25392537 ];
25402538
2539+ final newMembers = < Member > [...? updatedState.members];
2540+
25412541 _checkExpiredAttachmentMessages (updatedState);
25422542
25432543 _channelState = _channelState.copyWith (
@@ -2730,21 +2730,3 @@ bool _pinIsValid(Message message) {
27302730 final now = DateTime .now ();
27312731 return message.pinExpires! .isAfter (now);
27322732}
2733-
2734- extension on Iterable <Message > {
2735- Iterable <Message > merge (Iterable <Message >? other) {
2736- if (other == null ) return this ;
2737-
2738- final messageMap = {for (final message in this ) message.id: message};
2739-
2740- for (final message in other) {
2741- messageMap.update (
2742- message.id,
2743- message.syncWith,
2744- ifAbsent: () => message,
2745- );
2746- }
2747-
2748- return messageMap.values;
2749- }
2750- }
0 commit comments