Skip to content

Commit afbe5f8

Browse files
committed
migrate from built value
1 parent 120e576 commit afbe5f8

24 files changed

+130
-2446
lines changed

example/example.dart

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import 'package:built_value/built_value.dart';
21
import 'package:built_redux/built_redux.dart';
32

43
part 'example.g.dart';
54

65
void main() {
76
// Create a redux store holding the state of your app.
87
// Its API contains three getters: stream, state, and actions.
9-
final store = Store<Counter, CounterBuilder, CounterActions>(
8+
final store = Store<Counter, CounterActions>(
109
reducerBuilder.build(), // build returns a reducer function
1110
Counter(),
1211
CounterActions(),
@@ -28,38 +27,41 @@ void main() {
2827
// reducers
2928
abstract class CounterActions extends ReduxActions {
3029
ActionDispatcher<int> get increment;
30+
3131
ActionDispatcher<int> get decrement;
3232

3333
// factory to create on instance of the generated implementation of CounterActions
3434
CounterActions._();
35+
3536
factory CounterActions() => _$CounterActions();
3637
}
3738

3839
// This is a built value. It is an immutable model that implements the Built interface.
3940
// All of the state in your redux store is contained in a single built value model.
40-
abstract class Counter implements Built<Counter, CounterBuilder> {
41+
class Counter {
4142
/// [count] value of the counter
42-
int get count;
43+
int count;
4344

4445
// Built value constructor. The factory is returning the default state
45-
Counter._();
46-
factory Counter() => _$Counter._(count: 0);
46+
Counter({
47+
this.count = 0,
48+
});
4749
}
4850

4951
// These are reducer functions. They have a (state, action, builder) => void signature.
5052
// They describes how an action transforms the state into the next state by applying changes to the builder supplied.
5153
// You are required to use the builder passed, calling state.rebuild will NOT update the state in your redux store.
52-
void increment(Counter state, Action<int> action, CounterBuilder builder) =>
53-
builder.count = state.count + action.payload;
54+
void increment(Counter state, Action<int> action) =>
55+
state.count = state.count + action.payload;
5456

55-
void decrement(Counter state, Action<int> action, CounterBuilder builder) =>
56-
builder.count = state.count - action.payload;
57+
void decrement(Counter state, Action<int> action) =>
58+
state.count = state.count - action.payload;
5759

5860
// This is a reducer builder. Use of ReducerBuilder is not required, however it
5961
// is strongly recommended as it gives you static type checking to make sure
6062
// the payload for action name provided is the same as the expected payload
6163
// for the action provided to your reducer. Calling .build() returns a reducer function
6264
// that can be passed to the store's constructor.
63-
final reducerBuilder = ReducerBuilder<Counter, CounterBuilder>()
65+
final reducerBuilder = ReducerBuilder<Counter>()
6466
..add(CounterActionsNames.increment, increment)
6567
..add(CounterActionsNames.decrement, decrement);

example/example.g.dart

Lines changed: 0 additions & 82 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/middleware.dart

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ import 'typedefs.dart';
66

77
/// [MiddlewareApi] put in scope to your [Middleware] function by redux.
88
/// When using [MiddlewareBuilder] (recommended) [MiddlewareApi] is passed to your [MiddlewareHandler]
9-
class MiddlewareApi<
10-
State extends Built<State, StateBuilder>,
11-
StateBuilder extends Builder<State, StateBuilder>,
12-
Actions extends ReduxActions> {
9+
class MiddlewareApi<State, Actions extends ReduxActions> {
1310
final State Function() _state;
1411
final Actions Function() _actions;
1512

1613
MiddlewareApi._(this._state, this._actions);
1714

18-
factory MiddlewareApi(Store<State, StateBuilder, Actions> _store) =>
15+
factory MiddlewareApi(Store<State, Actions> _store) =>
1916
MiddlewareApi._(() => _store.state, () => _store.actions);
2017

2118
/// [state] returns the current state
@@ -29,23 +26,19 @@ class MiddlewareApi<
2926
/// with many different payload types, while maintaining type safety.
3027
/// Each [MiddlewareHandler] added with add<T> must take a state of type State, an Action of type
3128
/// Action<T>, and a builder of type StateBuilder
32-
class MiddlewareBuilder<
33-
State extends Built<State, StateBuilder>,
34-
StateBuilder extends Builder<State, StateBuilder>,
35-
Actions extends ReduxActions> {
36-
var _map =
37-
Map<String, MiddlewareHandler<State, StateBuilder, Actions, dynamic>>();
29+
class MiddlewareBuilder<State, Actions extends ReduxActions> {
30+
var _map = Map<String, MiddlewareHandler<State, Actions, dynamic>>();
3831

3932
void add<Payload>(ActionName<Payload> aMgr,
40-
MiddlewareHandler<State, StateBuilder, Actions, Payload> handler) {
33+
MiddlewareHandler<State, Actions, Payload> handler) {
4134
_map[aMgr.name] = (api, next, action) {
4235
handler(api, next, action as Action<Payload>);
4336
};
4437
}
4538

4639
/// [combine] combines this MiddlewareBuilder with another MiddlewareBuilder
4740
/// for the same type
48-
void combine(MiddlewareBuilder<State, StateBuilder, Actions> other) {
41+
void combine(MiddlewareBuilder<State, Actions> other) {
4942
_map.addAll(other._map);
5043
}
5144

@@ -54,45 +47,35 @@ class MiddlewareBuilder<
5447
NestedState extends Built<NestedState, NestedStateBuilder>,
5548
NestedStateBuilder extends Builder<NestedState, NestedStateBuilder>,
5649
NestedActions extends ReduxActions>(
57-
NestedMiddlewareBuilder<State, StateBuilder, Actions, NestedState,
58-
NestedStateBuilder, NestedActions>
50+
NestedMiddlewareBuilder<State, Actions, NestedState, NestedActions>
5951
other) {
6052
_map.addAll(other._map);
6153
}
6254

6355
/// [build] returns a [Middleware] function that handles all actions added with [add]
64-
Middleware<State, StateBuilder, Actions> build() =>
65-
(MiddlewareApi<State, StateBuilder, Actions> api) =>
66-
(ActionHandler next) => (Action<dynamic> action) {
67-
var handler = _map[action.name];
68-
if (handler != null) {
69-
handler(api, next, action);
70-
return;
71-
}
72-
73-
next(action);
74-
};
56+
Middleware<State, Actions> build() => (MiddlewareApi<State, Actions> api) =>
57+
(ActionHandler next) => (Action<dynamic> action) {
58+
var handler = _map[action.name];
59+
if (handler != null) {
60+
handler(api, next, action);
61+
return;
62+
}
63+
64+
next(action);
65+
};
7566
}
7667

77-
class NestedMiddlewareBuilder<
78-
State extends Built<State, StateBuilder>,
79-
StateBuilder extends Builder<State, StateBuilder>,
80-
Actions extends ReduxActions,
81-
NestedState extends Built<NestedState, NestedStateBuilder>,
82-
NestedStateBuilder extends Builder<NestedState, NestedStateBuilder>,
68+
class NestedMiddlewareBuilder<State, Actions extends ReduxActions, NestedState,
8369
NestedActions extends ReduxActions> {
84-
final _map =
85-
Map<String, MiddlewareHandler<State, StateBuilder, Actions, dynamic>>();
70+
final _map = Map<String, MiddlewareHandler<State, Actions, dynamic>>();
8671

8772
final NestedState Function(State) _stateMapper;
8873
final NestedActions Function(Actions) _actionsMapper;
8974

9075
NestedMiddlewareBuilder(this._stateMapper, this._actionsMapper);
9176

92-
void add<Payload>(
93-
ActionName<Payload> aMgr,
94-
MiddlewareHandler<NestedState, NestedStateBuilder, NestedActions, Payload>
95-
handler) {
77+
void add<Payload>(ActionName<Payload> aMgr,
78+
MiddlewareHandler<NestedState, NestedActions, Payload> handler) {
9679
_map[aMgr.name] = (api, next, action) {
9780
handler(
9881
MiddlewareApi._(
@@ -106,10 +89,10 @@ class NestedMiddlewareBuilder<
10689
/// `NestedState`, `NestedStateBuilder`, `NestedActions` and combines it with
10790
/// this `NestedMiddlewareBuilder`.
10891
void combineMiddlewareBuilder(
109-
MiddlewareBuilder<NestedState, NestedStateBuilder, NestedActions> other) {
92+
MiddlewareBuilder<NestedState, NestedActions> other) {
11093
var adapted = other._map.map((name, handler) => MapEntry(
11194
name,
112-
(MiddlewareApi<State, StateBuilder, Actions> api, ActionHandler next,
95+
(MiddlewareApi<State, Actions> api, ActionHandler next,
11396
Action action) =>
11497
handler(
11598
MiddlewareApi._(() => _stateMapper(api.state),
@@ -123,10 +106,6 @@ class NestedMiddlewareBuilder<
123106
/// [MiddlewareHandler] is a function that handles an action in a middleware. Its is only for
124107
/// use with [MiddlewareBuilder]. If you are not using [MiddlewareBuilder] middleware must be
125108
/// declared as a [Middleware] function.
126-
typedef MiddlewareHandler<
127-
State extends Built<State, StateBuilder>,
128-
StateBuilder extends Builder<State, StateBuilder>,
129-
Actions extends ReduxActions,
130-
Payload>
131-
= void Function(MiddlewareApi<State, StateBuilder, Actions> api,
132-
ActionHandler next, Action<Payload> action);
109+
typedef MiddlewareHandler<State, Actions extends ReduxActions, Payload>
110+
= void Function(MiddlewareApi<State, Actions> api, ActionHandler next,
111+
Action<Payload> action);

0 commit comments

Comments
 (0)