11import 'package:flutter_github_search_rx_redux/domain/search_usecase.dart' ;
22import 'package:rx_redux/rx_redux.dart' ;
33import 'package:rxdart/rxdart.dart' ;
4+ import 'package:rxdart_ext/rxdart_ext.dart' ;
45
56import 'home_action.dart' ;
67import 'home_state.dart' ;
@@ -10,7 +11,7 @@ RxReduxStore<HomeAction, HomeState> createStore(SearchUseCase searchUseCase) =>
1011 initialState: HomeState .initial (),
1112 sideEffects: HomeSideEffects (searchUseCase)(),
1213 reducer: (state, action) => action.reduce (state),
13- // logger: rxReduxDefaultLogger,
14+ logger: rxReduxDefaultLogger,
1415 );
1516
1617class HomeSideEffects {
@@ -50,36 +51,44 @@ class HomeSideEffects {
5051 Stream <HomeAction > actions,
5152 GetState <HomeState > getState,
5253 ) {
53- final textChangedAction$ = actions.whereType <TextChangedAction >();
54+ final textChangedAction$ = actions.whereType <TextChangedAction >().debug ();
55+
56+ final performLoadingNextPage = (LoadNextPageAction action) {
57+ return Stream .value (getState ())
58+ .where ((state) => state.canLoadNextPage)
59+ .exhaustMap ((state) => _nextPage (state.term, state.page + 1 )
60+ .takeUntil (textChangedAction$)
61+ .debug ());
62+ };
5463
5564 return actions
5665 .whereType <LoadNextPageAction >()
57- .map ((_) => getState ())
58- .where ((state) => state.canLoadNextPage)
59- .exhaustMap ((state) => _nextPage (state.term, state.page + 1 )
60- .takeUntil (textChangedAction$));
66+ .exhaustMap (performLoadingNextPage);
6167 }
6268
6369 Stream <HomeAction > retry (
6470 Stream <HomeAction > actions,
6571 GetState <HomeState > getState,
6672 ) {
67- final textChangedAction$ = actions.whereType <TextChangedAction >();
73+ final textChangedAction$ = actions.whereType <TextChangedAction >(). debug () ;
6874
69- return actions
70- .whereType <RetryAction >()
71- .map ((_) => getState ())
72- .where ((state) => state.canRetry)
73- .exhaustMap ((state) => _nextPage (state.term, state.page + 1 )
74- .takeUntil (textChangedAction$));
75+ final performRetry = (RetryAction action) {
76+ return Stream .value (getState ())
77+ .where ((state) => state.canRetry)
78+ .exhaustMap ((state) => _nextPage (state.term, state.page + 1 )
79+ .takeUntil (textChangedAction$)
80+ .debug ());
81+ };
82+
83+ return actions.whereType <RetryAction >().exhaustMap (performRetry);
7584 }
7685
7786 Stream <HomeAction > _nextPage (String term, int nextPage) {
7887 final loadingAction = SearchLoadingAction ((b) => b
7988 ..term = term
8089 ..nextPage = nextPage);
8190
82- return Rx .defer (() => _searchUseCase (term: term, page: nextPage). asStream ( ))
91+ return Rx .fromCallable (() => _searchUseCase (term: term, page: nextPage))
8392 .map <HomeAction >(
8493 (items) => SearchSuccessAction ((b) => b
8594 ..term = term
0 commit comments