From 3f8a09f2b3af517e59a7c90a4717cd35c8ddf08c Mon Sep 17 00:00:00 2001 From: Plague Fox Date: Thu, 23 May 2024 15:17:51 +0400 Subject: [PATCH] chore: Refactor ChatRepositorySpinifyImpl to use lazy loading for event streams --- .../feature/chat/data/chat_repository.dart | 4 +-- lib/src/model/channel_events.dart | 27 +++++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/example/lib/src/feature/chat/data/chat_repository.dart b/example/lib/src/feature/chat/data/chat_repository.dart index c078d64..2a8b1bc 100644 --- a/example/lib/src/feature/chat/data/chat_repository.dart +++ b/example/lib/src/feature/chat/data/chat_repository.dart @@ -62,8 +62,8 @@ final class ChatRepositorySpinifyImpl implements IChatRepository { decoder = const PlainMessageDecoder(); } - return _spinify.stream.publications - .where((event) => event.channel == channel) + return _spinify.stream + .publication(channel: channel) .map>((event) => event.data) .map(decoder.convert) .handleError(ignoreErrors); diff --git a/lib/src/model/channel_events.dart b/lib/src/model/channel_events.dart index a8ee835..9d4f2a1 100644 --- a/lib/src/model/channel_events.dart +++ b/lib/src/model/channel_events.dart @@ -11,31 +11,36 @@ import 'channel_event.dart'; extension type ChannelEvents(Stream stream) implements Stream { /// Stream of publication events. - ChannelEvents get publication => - filter(); + ChannelEvents publication({String? channel}) => + filter(channel: channel); /// Stream of presence events. - ChannelEvents get presence => filter(); + ChannelEvents presence({String? channel}) => + filter(channel: channel); /// Stream of unsubscribe events. - ChannelEvents get unsubscribe => - filter(); + ChannelEvents unsubscribe({String? channel}) => + filter(channel: channel); /// Stream of message events. - ChannelEvents get message => filter(); + ChannelEvents message({String? channel}) => + filter(channel: channel); /// Stream of subscribe events. - ChannelEvents get subscribe => filter(); + ChannelEvents subscribe({String? channel}) => + filter(channel: channel); /// Stream of connect events. - ChannelEvents get connect => filter(); + ChannelEvents connect({String? channel}) => + filter(channel: channel); /// Stream of disconnect events. - ChannelEvents get disconnect => - filter(); + ChannelEvents disconnect({String? channel}) => + filter(channel: channel); /// Stream of refresh events. - ChannelEvents get refresh => filter(); + ChannelEvents refresh({String? channel}) => + filter(channel: channel); /// Filtered stream of data of [SpinifyChannelEvent]. ChannelEvents filter({String? channel}) =>