Skip to content

Commit

Permalink
Merge pull request #97 from davidmarne/7.5.0
Browse files Browse the repository at this point in the history
7.5.0
  • Loading branch information
davidmarne authored Sep 13, 2018
2 parents a6bd24c + 5bb06c9 commit 80908a2
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 38 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 7.5.0

* allow 4.x.x of built_collection
* added combineReducerBuilder to NestedReducerBuilder
* added a NestedMiddlewareBuilder

## 7.4.5

* open sdk range to dart 2!
Expand Down
8 changes: 4 additions & 4 deletions doc/basics/reducers.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Reducers specify how the application's state changes in response to actions sent to the store. Remember that actions only describe the fact that something happened, but don't describe how the application's state changes.

## Writing a reducer
## Writing a reducer

Say I'm working on the counter app discussed in the [Actions](actions.md) section. This means I've defined my apps actions as:

Expand All @@ -20,9 +20,9 @@ abstract class AppActions extends ReduxActions {
and my state object as so:

```dart
abstract class Counter implements Built<Counter, CounterBuilder> {
Counter._();
factory Counter() => new _$Counter._(count: 0);
abstract class App implements Built<App, AppBuilder> {
App._();
factory App() => new _$App._(count: 0);
int get count;
}
Expand Down
5 changes: 4 additions & 1 deletion lib/src/middleware.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ class NestedMiddlewareBuilder<
};
}

void addAll(
/// [combineMiddlewareBuilder] takes a `MiddlewareBuilder` with the type arguments
/// `NestedState`, `NestedStateBuilder`, `NestedActions` and combines it with
/// this `NestedMiddlewareBuilder`.
void combineMiddlewareBuilder(
MiddlewareBuilder<NestedState, NestedStateBuilder, NestedActions> other) {
var adapted = other._map.map((name, handler) => MapEntry(
name,
Expand Down
25 changes: 14 additions & 11 deletions lib/src/reducer_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,6 @@ class NestedReducerBuilder<

NestedReducerBuilder(this._stateMapper, this._builderMapper);

void addAll(ReducerBuilder<NestedState, NestedStateBuilder> other) {
final adapted = other._map.map((name, reducer) => MapEntry(
name,
(State state, Action<dynamic> action, StateBuilder builder) => reducer(
_stateMapper(state),
action,
_builderMapper(builder),
)));
_map.addAll(adapted);
}

/// Registers [reducer] function to the given [actionName]
void add<Payload>(ActionName<Payload> actionName,
Reducer<NestedState, NestedStateBuilder, Payload> reducer) {
Expand All @@ -144,6 +133,20 @@ class NestedReducerBuilder<
_builderMapper(builder),
);
}

/// [combineReducerBuilder] takes a `ReducerBuilder` with the type arguments
/// `NestedState`, `NestedStateBuilder`, and combines it with this `NestedReducerBuilder`.
void combineReducerBuilder(
ReducerBuilder<NestedState, NestedStateBuilder> other) {
final adapted = other._map.map((name, reducer) => MapEntry(
name,
(State state, Action<dynamic> action, StateBuilder builder) => reducer(
_stateMapper(state),
action,
_builderMapper(builder),
)));
_map.addAll(adapted);
}
}

/// [AbstractReducerBuilder] returns a reducer builder that
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: built_redux
version: 7.4.6
version: 7.5.0
description:
A state management library written in dart that enforces immutability
authors:
Expand Down
6 changes: 2 additions & 4 deletions test/unit/nested_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,9 @@ NestedReducerBuilder<Base, BaseBuilder, Grandchild, GrandchildBuilder>
BaseBuilder,
Grandchild,
GrandchildBuilder>((s) => s.child.grandchild, (b) => b.child.grandchild)
..addAll(getGrandchildReducer());
..combineReducerBuilder(getGrandchildReducer());

ReducerBuilder<Grandchild, GrandchildBuilder> getGrandchildReducer() =>
new ReducerBuilder<
Grandchild,
GrandchildBuilder>()
new ReducerBuilder<Grandchild, GrandchildBuilder>()
..add<Null>(
GrandchildActionsNames.grandchildAction, (s, a, b) => b.count++);
41 changes: 24 additions & 17 deletions test/unit/test_counter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ void _incrementSubCount(
final reducer = (new ReducerBuilder<Counter, CounterBuilder>()
..add(CounterActionsNames.increment, _increment)
..combine(_otherReducer)
..add(SubCounterActionsNames.increment, _incrementSubCount)
).build();
..add(SubCounterActionsNames.increment, _incrementSubCount))
.build();

final _otherReducer = (new ReducerBuilder<Counter, CounterBuilder>()
..add(CounterActionsNames.incrementOther, _incrementOther));
Expand Down Expand Up @@ -80,11 +80,11 @@ abstract class MiddlewareActions extends ReduxActions {
}

var counterMiddleware =
(new MiddlewareBuilder<Counter, CounterBuilder, CounterActions>()
..add(MiddlewareActionsNames.doubleIt, _doubleIt)
..combine(tripleItMiddlewareBuilder)
..combineNested(subCountNested)
).build();
(new MiddlewareBuilder<Counter, CounterBuilder, CounterActions>()
..add(MiddlewareActionsNames.doubleIt, _doubleIt)
..combine(tripleItMiddlewareBuilder)
..combineNested(subCountNested))
.build();

void _doubleIt(MiddlewareApi<Counter, CounterBuilder, CounterActions> api,
ActionHandler next, Action<int> action) {
Expand All @@ -102,16 +102,23 @@ void _tripleIt(MiddlewareApi<Counter, CounterBuilder, CounterActions> api,
next(action);
}

var subCountNested = NestedMiddlewareBuilder<Counter, CounterBuilder,
CounterActions, SubCounter, SubCounterBuilder, SubCounterActions>(
(c) => c.subCounter, (a) => a.subCounterActions)
..addAll(subCountMiddlewareBuilder);

var subCountMiddlewareBuilder = new MiddlewareBuilder<SubCounter, SubCounterBuilder, SubCounterActions>()
..add(SubCounterActionsNames.doubleIt, _subCounterDoubleIt);

void _subCounterDoubleIt(MiddlewareApi<SubCounter, SubCounterBuilder, SubCounterActions> api,
ActionHandler next, Action<int> action) {
var subCountNested = NestedMiddlewareBuilder<
Counter,
CounterBuilder,
CounterActions,
SubCounter,
SubCounterBuilder,
SubCounterActions>((c) => c.subCounter, (a) => a.subCounterActions)
..combineMiddlewareBuilder(subCountMiddlewareBuilder);

var subCountMiddlewareBuilder =
new MiddlewareBuilder<SubCounter, SubCounterBuilder, SubCounterActions>()
..add(SubCounterActionsNames.doubleIt, _subCounterDoubleIt);

void _subCounterDoubleIt(
MiddlewareApi<SubCounter, SubCounterBuilder, SubCounterActions> api,
ActionHandler next,
Action<int> action) {
api.actions.increment(api.state.subCount * 2);
next(action);
}
Expand Down

0 comments on commit 80908a2

Please sign in to comment.