@@ -10,14 +10,19 @@ class MiddlewareApi<
10
10
State extends Built <State , StateBuilder >,
11
11
StateBuilder extends Builder <State , StateBuilder >,
12
12
Actions extends ReduxActions > {
13
- Store <State , StateBuilder , Actions > _store;
14
- MiddlewareApi (this ._store);
13
+ final State Function () _state;
14
+ final Actions Function () _actions;
15
+
16
+ MiddlewareApi ._(this ._state, this ._actions);
17
+
18
+ factory MiddlewareApi (Store <State , StateBuilder , Actions > _store) =>
19
+ MiddlewareApi ._(() => _store.state, () => _store.actions);
15
20
16
21
/// [state] returns the current state
17
- State get state => _store.state ;
22
+ State get state => _state () ;
18
23
19
24
/// [actions] returns the actions synced with this redux store
20
- Actions get actions => _store.actions ;
25
+ Actions get actions => _actions () ;
21
26
}
22
27
23
28
/// [MiddlewareBuilder] allows you to build a reducer that handles many different actions
@@ -44,6 +49,16 @@ class MiddlewareBuilder<
44
49
_map.addAll (other._map);
45
50
}
46
51
52
+ void combineNested<
53
+ NestedState extends Built <NestedState , NestedStateBuilder >,
54
+ NestedStateBuilder extends Builder <NestedState , NestedStateBuilder >,
55
+ NestedActions extends ReduxActions > (
56
+ NestedMiddlewareBuilder <State , StateBuilder , Actions , NestedState ,
57
+ NestedStateBuilder , NestedActions >
58
+ other) {
59
+ _map.addAll (other._map);
60
+ }
61
+
47
62
/// [build] returns a [Middleware] function that handles all actions added with [add]
48
63
Middleware <State , StateBuilder , Actions > build () =>
49
64
(MiddlewareApi <State , StateBuilder , Actions > api) =>
@@ -58,9 +73,52 @@ class MiddlewareBuilder<
58
73
};
59
74
}
60
75
76
+ class NestedMiddlewareBuilder <
77
+ State extends Built <State , StateBuilder >,
78
+ StateBuilder extends Builder <State , StateBuilder >,
79
+ Actions extends ReduxActions ,
80
+ NestedState extends Built <NestedState , NestedStateBuilder >,
81
+ NestedStateBuilder extends Builder <NestedState , NestedStateBuilder >,
82
+ NestedActions extends ReduxActions > {
83
+ final _map =
84
+ Map <String , MiddlewareHandler <State , StateBuilder , Actions , dynamic >>();
85
+
86
+ final NestedState Function (State ) _stateMapper;
87
+ final NestedActions Function (Actions ) _actionsMapper;
88
+
89
+ NestedMiddlewareBuilder (this ._stateMapper, this ._actionsMapper);
90
+
91
+ void add <Payload >(
92
+ ActionName <Payload > aMgr,
93
+ MiddlewareHandler <NestedState , NestedStateBuilder , NestedActions , Payload >
94
+ handler) {
95
+ _map[aMgr.name] = (api, next, action) {
96
+ handler (
97
+ MiddlewareApi ._(
98
+ () => _stateMapper (api.state), () => _actionsMapper (api.actions)),
99
+ next,
100
+ action as Action <Payload >);
101
+ };
102
+ }
103
+
104
+ void addAll (
105
+ MiddlewareBuilder <NestedState , NestedStateBuilder , NestedActions > other) {
106
+ var adapted = other._map.map ((name, handler) => MapEntry (
107
+ name,
108
+ (MiddlewareApi <State , StateBuilder , Actions > api, ActionHandler next,
109
+ Action action) =>
110
+ handler (
111
+ MiddlewareApi ._(() => _stateMapper (api.state),
112
+ () => _actionsMapper (api.actions)),
113
+ next,
114
+ action)));
115
+ _map.addAll (adapted);
116
+ }
117
+ }
118
+
61
119
/// [MiddlewareHandler] is a function that handles an action in a middleware. Its is only for
62
120
/// use with [MiddlewareBuilder] . If you are not using [MiddlewareBuilder] middleware must be
63
- /// decalred as a [Middleware] function.
121
+ /// declared as a [Middleware] function.
64
122
typedef void MiddlewareHandler <
65
123
State extends Built <State , StateBuilder >,
66
124
StateBuilder extends Builder <State , StateBuilder >,
0 commit comments