diff --git a/packages/neon/neon/lib/src/router.dart b/packages/neon/neon/lib/src/router.dart index cad5b2af..70fe27a7 100644 --- a/packages/neon/neon/lib/src/router.dart +++ b/packages/neon/neon/lib/src/router.dart @@ -23,7 +23,7 @@ class AppRouter extends GoRouter { required final AccountsBloc accountsBloc, }) : super( debugLogDiagnostics: kDebugMode, - refreshListenable: StreamListenable.behaviorSubject(accountsBloc.activeAccount), + refreshListenable: StreamListenable(accountsBloc.activeAccount), navigatorKey: navigatorKey, initialLocation: const HomeRoute().location, redirect: (final context, final state) { diff --git a/packages/neon/neon/lib/src/utils/stream_listenable.dart b/packages/neon/neon/lib/src/utils/stream_listenable.dart index d1e5b65e..3f47903b 100644 --- a/packages/neon/neon/lib/src/utils/stream_listenable.dart +++ b/packages/neon/neon/lib/src/utils/stream_listenable.dart @@ -9,32 +9,36 @@ import 'package:rxdart/rxdart.dart'; /// Objects need to be manually disposed. class StreamListenable extends ChangeNotifier { /// Listenable Stream - /// - /// Implementation for all types of [Stream]s. - /// For an implementation tailored towards [BehaviorSubject] have a look at [StreamListenable.behaviorSubject]. StreamListenable(final Stream stream) { - notifyListeners(); - - _subscription = stream.asBroadcastStream().listen((final value) { + if (stream is! BehaviorSubject) { notifyListeners(); - }); + } + + addSubscription(stream); } - /// Listenable BehaviorSubject + /// Listenable for multiple Streams. /// - /// Implementation for a [BehaviorSubject]. It ensures to not unececcary notify listeners. - /// For an implementation tailored towards otnher kinds of [Stream] have a look at [StreamListenable]. - StreamListenable.behaviorSubject(final BehaviorSubject subject) { - _subscription = subject.listen((final value) { - notifyListeners(); - }); + /// Notifies it's listeners on every event emitted by any of the streams. + StreamListenable.multiListenable(final Iterable> streams) { + streams.forEach(addSubscription); + } + + void addSubscription(final Stream stream) { + _subscriptions.add( + stream.asBroadcastStream().listen((final _) { + notifyListeners(); + }), + ); } - late final StreamSubscription _subscription; + final List> _subscriptions = []; @override void dispose() { - unawaited(_subscription.cancel()); + for (final subscription in _subscriptions) { + unawaited(subscription.cancel()); + } super.dispose(); }