Skip to content

Commit 06f182e

Browse files
committed
pass state to reducers
1 parent 9fd6f06 commit 06f182e

File tree

11 files changed

+39
-35
lines changed

11 files changed

+39
-35
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ import 'package:built_redux/built_redux.dart';
103103
* These are reducers, a pure function with (builder, action) => state signature.
104104
* It describes how an action transforms the state into the next state.
105105
*/
106-
increment(CounterBuilder builder, Action<int> action) =>
107-
builder..count = builder.build().count + action.payload;
106+
increment(Counter state, Action<int> action, CounterBuilder builder) =>
107+
builder..count = state.count + action.payload;
108108

109-
decrement(CounterBuilder builder, Action<int> action) =>
110-
builder..count = builder.build().count - action.payload;
109+
decrement(Counter state, Action<int> action, CounterBuilder builder) =>
110+
builder..count = state.count - action.payload;
111111

112112
/**
113113
* This is a reducer builder. Use of ReducerBuilder is not required, however it

example/lib/reducers/app_state.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ abstract class AppStateActions extends ReduxActions {
2020
factory AppStateActions() => new _$AppStateActions();
2121
}
2222

23+
_setCurrentGroupReducer(AppState state, Action<int> action, AppStateBuilder builder) =>
24+
builder..currentGroup = action.payload;
25+
2326
final _reducers = (new ReducerBuilder<AppStateBuilder>()
24-
..add<int>(
25-
AppStateActionsNames.setCurrentGroup,
26-
(builder, action) => builder..currentGroup = action.payload,
27-
))
27+
..add<int>(AppStateActionsNames.setCurrentGroup, _setCurrentGroupReducer))
2828
.build();
2929

3030
// Built Reducer

example/lib/reducers/groups.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ class AddTodoToGroupPayload {
2424
}
2525

2626
// Reducers
27-
_addTodoToGroupReducer(GroupsReducerBuilder builder, Action<AddTodoToGroupPayload> action) =>
28-
builder.groupMap[action.payload.groupId] = builder.groupMap
29-
.build()[action.payload.groupId]
27+
_addTodoToGroupReducer(
28+
GroupsReducer state, Action<AddTodoToGroupPayload> action, GroupsReducerBuilder builder) =>
29+
builder.groupMap[action.payload.groupId] = state.groupMap[action.payload.groupId]
3030
.rebuild((gbuilder) => gbuilder..todoIds.add(action.payload.todoId));
3131

32-
_addGroupReducer(GroupsReducerBuilder builder, Action<Group> action) =>
32+
_addGroupReducer(GroupsReducer state, Action<Group> action, GroupsReducerBuilder builder) =>
3333
builder.groupMap[action.payload.id] = action.payload;
3434

35-
_removeGroupReducer(GroupsReducerBuilder builder, Action<int> action) =>
35+
_removeGroupReducer(GroupsReducer state, Action<int> action, GroupsReducerBuilder builder) =>
3636
builder.groupMap.remove(action.payload);
3737

3838
final _reducers = (new ReducerBuilder<GroupsReducerBuilder>()

example/lib/reducers/todos.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ abstract class TodosActions extends ReduxActions {
1818
}
1919

2020
// Reducers
21-
addTodoReducer(TodosReducerBuilder builder, Action<Todo> action) =>
21+
addTodoReducer(TodosReducer state, Action<Todo> action, TodosReducerBuilder builder) =>
2222
builder.todosMap[action.payload.id] = action.payload;
2323

24-
removeTodoReducer(TodosReducerBuilder builder, Action<int> action) =>
24+
removeTodoReducer(TodosReducer state, Action<int> action, TodosReducerBuilder builder) =>
2525
builder.todosMap.remove(action.payload);
2626

27-
updateTodoStatusReducer(TodosReducerBuilder builder, Action<int> action) =>
28-
builder.todosMap[action.payload] = builder.todosMap.build()[action.payload].rebuild(
29-
(tbuilder) => tbuilder..done = !tbuilder.done,
30-
);
27+
updateTodoStatusReducer(TodosReducer state, Action<int> action, TodosReducerBuilder builder) =>
28+
builder.todosMap[action.payload] = state.todosMap[action.payload].rebuild(
29+
(tbuilder) => tbuilder..done = !tbuilder.done,
30+
);
3131

3232
final _reducers = (new ReducerBuilder<TodosReducerBuilder>()
3333
..add<Todo>(TodosActionsNames.addTodo, addTodoReducer)

example/pubspec.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ packages:
6363
version: "1.4.0"
6464
built_redux:
6565
description:
66-
name: built_redux
67-
url: "https://pub.dartlang.org"
68-
source: hosted
69-
version: "0.0.1"
66+
path: ".."
67+
relative: true
68+
source: path
69+
version: "0.0.3"
7070
built_value:
7171
description:
7272
name: built_value

example/pubspec.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ dev_dependencies:
1212
built_value_generator: ^1.0.0
1313
test: "^0.12.6+2"
1414

15+
dependency_overrides:
16+
built_redux:
17+
path: ../.
18+
1519
transformers:
1620
- built_redux
1721
- $dart2js

lib/src/action.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// [Action]
1+
/// [Action] is the object passed to your reducer to signify the state change that needs to take place.
22
/// Action [name]s should always be unique!
33
class Action<Payload> {
44
/// A unique action name.
@@ -17,7 +17,7 @@ class ActionMgr<P> {
1717

1818
String get name => _name;
1919

20-
call(P payload) => _dispatcher(new Action()
20+
call(P payload) => _dispatcher(new Action<P>()
2121
..name = name
2222
..payload = payload);
2323

lib/src/built_reducer.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ abstract class BuiltReducer<V extends Built<V, B>, B extends Builder<V, B>> impl
99
/// This is so each reducer can have the action payload generic be a different non dynamic value
1010
Map<String, Reducer<V, B, dynamic>> get reducers;
1111

12-
void reduce(B builder, Action<dynamic> a) {
12+
void reduce(V state, Action<dynamic> a, B builder) {
1313
var reducer = reducers[a.name];
1414
// TODO: warn if payload type doesn't match reducer
15-
if (reducer != null) reducer(builder, a);
15+
if (reducer != null) reducer(state, a, builder);
1616

17-
reduceChildren(builder, a);
17+
reduceChildren(state, a, builder);
1818
}
1919

2020
/// Generated by the transformer for developer convienience
2121
/// Is implemented here as a noop to avoid anaylisis issues
22-
void reduceChildren(B builder, Action<dynamic> a) {}
22+
void reduceChildren(V state, Action<dynamic> a, B builder) {}
2323
}
2424

2525
class ReducerBuilder<B> {

lib/src/store.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class Store<State extends BuiltReducer, Actions extends ReduxActions> {
3232

3333
// setup the middleware dispatch chain
3434
ActionHandler handler = (action) {
35-
var state = _state.rebuild((b) => _state.reduce(b, action));
35+
var state = _state.rebuild((b) => _state.reduce(_state, action, b));
3636

3737
// if the hashcode did not change bail
38-
if (identical(_state, state)) return;
38+
if (_state == state) return;
3939

4040
// update the internal state and publish the change
4141
_state = state;

lib/transformer.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class BuiltStoreTransformer extends Transformer {
5858
var name = superclass.name.name;
5959
if (name == 'BuiltReducer') {
6060
String reduceChildrenBody =
61-
"\n\n @override\n reduceChildren(dynamic b, Action<dynamic> a) {\n }";
61+
"\n\n @override\n reduceChildren(dynamic state, Action<dynamic> a, dynamic builder) {\n }";
6262

6363
for (ClassMember m in cd.members) {
6464
if (m is! MethodDeclaration) continue;
@@ -67,8 +67,8 @@ class BuiltStoreTransformer extends Transformer {
6767
if (!md.isGetter || md.returnType == null) continue;
6868

6969
if (builtReducers.contains(md.returnType.name.name))
70-
reduceChildrenBody =
71-
reduceChildrenBody.replaceFirst("{", "{\n ${md.name}.reduce(b.${md.name}, a);");
70+
reduceChildrenBody = reduceChildrenBody.replaceFirst(
71+
"{", "{\n ${md.name}.reduce(state.${md.name}, a, builder.${md.name});");
7272
}
7373

7474
transformedFile.insert(

0 commit comments

Comments
 (0)