-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Implement bloc presentation listener without hooks"
This reverts commit 29b72c2.
- Loading branch information
1 parent
29b72c2
commit 2ccddfb
Showing
5 changed files
with
123 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export 'src/bloc_presentation_listener.dart'; | ||
export 'src/bloc_presentation_mixin.dart'; | ||
export 'src/use_bloc_presentation_listener.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/bloc_presentation/lib/src/use_bloc_presentation_listener.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:bloc_presentation/bloc_presentation.dart'; | ||
import 'package:flutter_hooks/flutter_hooks.dart'; | ||
import 'package:provider/provider.dart'; | ||
|
||
/// Subscribes to the presentation stream and invokes `listener` on each event. | ||
/// | ||
/// If [bloc] is omitted, [useBlocPresentationListener] will automatically perform | ||
/// a lookup using [Provider] and the current `BuildContext`. | ||
void useBlocPresentationListener<B extends BlocPresentationMixin<dynamic, P>, | ||
P>({ | ||
required BlocPresentationWidgetListener<P> listener, | ||
B? bloc, | ||
}) { | ||
final context = useContext(); | ||
final effectiveStream = bloc?.presentation ?? | ||
context.select<B, Stream<P>>((bloc) => bloc.presentation); | ||
|
||
useEffect( | ||
() { | ||
final subscription = effectiveStream.listen( | ||
(event) => listener(context, event), | ||
); | ||
|
||
return subscription.cancel; | ||
}, | ||
[effectiveStream, listener], | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters