|
1 | 1 | library redux_toolkit;
|
2 | 2 |
|
3 |
| -import 'package:redux/redux.dart'; |
4 |
| - |
5 |
| -typedef CaseReducer<State, Action> = State Function(State, Action); |
6 |
| -typedef DefaultCaseReducer<State> = State Function(State); |
7 |
| -typedef BuilderCallback<State> = Function(ActionReducerMapBuilder<State>); |
8 |
| - |
9 |
| -abstract class ActionReducerMapBuilder<State> { |
10 |
| - ActionReducerMapBuilder<State> addCase<Action>(CaseReducer<State, Action> reducer); |
11 |
| - ActionReducerMapBuilder<State> addDefaultCase(DefaultCaseReducer<State> reducer); |
12 |
| - ActionReducerMap<State> build(); |
13 |
| -} |
14 |
| - |
15 |
| -abstract class ActionReducerMap<State> { |
16 |
| - CaseReducer<State, dynamic> getReducerForAction(Type action); |
17 |
| -} |
18 |
| - |
19 |
| -class _ActionReducerMapBuilder<State> implements ActionReducerMapBuilder<State>, ActionReducerMap<State> { |
20 |
| - final Map<Type, CaseReducer<State, dynamic>> _actionsMap = {}; |
21 |
| - CaseReducer<State, dynamic> _defaultCaseReducer = (state, action) => state; |
22 |
| - |
23 |
| - ActionReducerMapBuilder<State> addCase<Action>(CaseReducer<State, Action> reducer) { |
24 |
| - _actionsMap[Action] = (state, action) => reducer(state, action); |
25 |
| - return this; |
26 |
| - } |
27 |
| - |
28 |
| - ActionReducerMapBuilder<State> addDefaultCase(DefaultCaseReducer<State> reducer) { |
29 |
| - _defaultCaseReducer = (state, action) => reducer(state); |
30 |
| - return this; |
31 |
| - } |
32 |
| - |
33 |
| - ActionReducerMap<State> build() { |
34 |
| - return this; |
35 |
| - } |
36 |
| - |
37 |
| - CaseReducer<State, dynamic> getReducerForAction(Type action) { |
38 |
| - if (_actionsMap.containsKey(action)) { |
39 |
| - return _actionsMap[action]; |
40 |
| - } |
41 |
| - |
42 |
| - return _defaultCaseReducer; |
43 |
| - } |
44 |
| -} |
45 |
| - |
46 |
| -Reducer<State> createReducer<State>(State initialState, BuilderCallback<State> builderCallback) { |
47 |
| - final builder = _ActionReducerMapBuilder<State>(); |
48 |
| - builderCallback(builder); |
49 |
| - final actionReducerMap = builder.build(); |
50 |
| - |
51 |
| - return (state, action) { |
52 |
| - if (state == null) { |
53 |
| - state = initialState; |
54 |
| - } |
55 |
| - |
56 |
| - return actionReducerMap.getReducerForAction(action.runtimeType)(state, action); |
57 |
| - }; |
58 |
| -} |
| 3 | +export 'package:reselect/reselect.dart'; |
| 4 | +export './src/create_reducer.dart'; |
0 commit comments