Skip to content

Commit

Permalink
Merge pull request #41 from davidmarne/strong_mode_rules
Browse files Browse the repository at this point in the history
Strong mode rules & Action stream
  • Loading branch information
davidmarne authored Nov 5, 2017
2 parents 5719469 + 82ffc80 commit e76eb88
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 206 deletions.
6 changes: 3 additions & 3 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
analyzer:
strong-mode: true
implicit-dynamic: false
implicit-casts: false
strong-mode:
implicit-casts: false
implicit-dynamic: false
1 change: 0 additions & 1 deletion lib/built_redux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ library built_redux;
export 'src/action.dart';
export 'src/reducer_builder.dart';
export 'src/middleware.dart';
export 'src/state_transformer.dart';
export 'src/store.dart';
export 'src/store_change.dart';
export 'src/typedefs.dart';
2 changes: 1 addition & 1 deletion lib/src/action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ActionDispatcher<P> {

ActionDispatcher(this._name);

void setDispatcher(dispatcher) {
void setDispatcher(Dispatcher dispatcher) {
_dispatcher = dispatcher;
}
}
Expand Down
8 changes: 5 additions & 3 deletions lib/src/middleware.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ class MiddlewareBuilder<
var _map = new Map<String,
MiddlewareHandler<State, StateBuilder, Actions, dynamic>>();

void add<T>(ActionName<T> aMgr,
MiddlewareHandler<State, StateBuilder, Actions, T> handler) {
_map[aMgr.name] = handler;
void add<Payload>(ActionName<Payload> aMgr,
MiddlewareHandler<State, StateBuilder, Actions, Payload> handler) {
_map[aMgr.name] = (api, next, action) {
handler(api, next, action as Action<Payload>);
};
}

/// [build] returns a [Middleware] function that handles all actions added with [add]
Expand Down
20 changes: 12 additions & 8 deletions lib/src/reducer_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class ReducerBuilder<State extends Built<State, StateBuilder>,
/// Registers [reducer] function to the given [actionName]
void add<Payload>(ActionName<Payload> actionName,
Reducer<State, StateBuilder, Payload> reducer) {
_map[actionName.name] = reducer;
_map[actionName.name] = (state, action, builder) {
reducer(state, action as Action<Payload>, builder);
};
}

/// [combine] combines this ReducerBuilder with another ReducerBuilder
Expand Down Expand Up @@ -127,7 +129,7 @@ class NestedReducerBuilder<
Reducer<NestedState, NestedStateBuilder, Payload> reducer) {
_map[actionName.name] = (state, action, builder) => reducer(
_stateMapper(state),
action,
action as Action<Payload>,
_builderMapper(builder),
);
}
Expand All @@ -144,7 +146,9 @@ class AbstractReducerBuilder<AState, AStateBuilder> {
/// Registers [reducer] function to the given [actionName]
void add<Payload>(ActionName<Payload> actionName,
CReducer<AState, AStateBuilder, Payload> reducer) {
_map[actionName.name] = reducer;
_map[actionName.name] = (state, action, builder) {
reducer(state, action as Action<Payload>, builder);
};
}

Map<String, CReducer<AState, AStateBuilder, dynamic>> build() => _map;
Expand All @@ -171,7 +175,7 @@ class ListReducerBuilder<State extends Built<State, StateBuilder>,
CReducer<BuiltList<T>, ListBuilder<T>, Payload> reducer) {
_map[actionName.name] = (state, action, builder) => reducer(
_stateMapper(state),
action,
action as Action<Payload>,
_builderMapper(builder),
);
}
Expand All @@ -194,7 +198,7 @@ class ListMultimapReducerBuilder<State extends Built<State, StateBuilder>,
reducer) {
_map[actionName.name] = (state, action, builder) => reducer(
_stateMapper(state),
action,
action as Action<Payload>,
_builderMapper(builder),
);
}
Expand All @@ -215,7 +219,7 @@ class MapReducerBuilder<State extends Built<State, StateBuilder>,
CReducer<BuiltMap<K, V>, MapBuilder<K, V>, Payload> reducer) {
_map[actionName.name] = (state, action, builder) => reducer(
_stateMapper(state),
action,
action as Action<Payload>,
_builderMapper(builder),
);
}
Expand All @@ -236,7 +240,7 @@ class SetReducerBuilder<State extends Built<State, StateBuilder>,
CReducer<BuiltSet<T>, SetBuilder<T>, Payload> reducer) {
_map[actionName.name] = (state, action, builder) => reducer(
_stateMapper(state),
action,
action as Action<Payload>,
_builderMapper(builder),
);
}
Expand All @@ -259,7 +263,7 @@ class SetMultimapReducerBuilder<State extends Built<State, StateBuilder>,
reducer) {
_map[actionName.name] = (state, action, builder) => reducer(
_stateMapper(state),
action,
action as Action<Payload>,
_builderMapper(builder),
);
}
Expand Down
79 changes: 0 additions & 79 deletions lib/src/state_transformer.dart

This file was deleted.

24 changes: 18 additions & 6 deletions lib/src/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'action.dart';
import 'middleware.dart';
import 'typedefs.dart';
import 'store_change.dart';
import 'state_transformer.dart';

/// [Store] is the container of your state. It listens for actions, invokes reducers,
/// and publishes changes to the state
Expand All @@ -34,8 +33,7 @@ class Store<

_actions = actions;

final MiddlewareApi api =
new MiddlewareApi<State, StateBuilder, Actions>(this);
final api = new MiddlewareApi<State, StateBuilder, Actions>(this);

// setup the dispatch chain
ActionHandler handler = (action) {
Expand Down Expand Up @@ -68,7 +66,7 @@ class Store<
}

/// [dispose] removes closes both the dispatch and subscription stream
dispose() {
void dispose() {
_stateController.close();
_state = null;
_actions = null;
Expand All @@ -79,7 +77,7 @@ class Store<
void replaceState(State state) {
if (_state != state) {
_stateController.add(new StoreChange<State, StateBuilder, dynamic>(
state, _state, new Action('replaceState', null)));
state, _state, new Action<Null>('replaceState', null)));
_state = state;
}
}
Expand All @@ -104,12 +102,26 @@ class Store<
Stream<SubstateChange<Substate>> substateStream<Substate>(
StateMapper<State, StateBuilder, Substate> mapper,
) =>
_stateController.stream.transform(new StateChangeTransformer(mapper));
stream
.map((c) => new SubstateChange<Substate>(
mapper(c.prev),
mapper(c.next),
))
.where((c) => c.prev != c.next);

/// [nextSubstate] is a stream which has a payload of the next subState value, rather than the SubstateChange event
Stream<Substate> nextSubstate<Substate>(
StateMapper<State, StateBuilder, Substate> mapper,
) =>
substateStream(mapper)
.map((SubstateChange<Substate> change) => change.next);

/// [actionStream] returns a stream the fires when a state change is caused by the action
/// with the name provided. Check out built_redux_rx if you are looking for streams to actions that do not
/// necessarily result in state changes.
Stream<StoreChange<State, StateBuilder, Payload>> actionStream<Payload>(
ActionName<Payload> actionName) =>
stream
.where((c) => c.action.name == actionName.name)
.map((c) => c as StoreChange<State, StateBuilder, Payload>);
}
8 changes: 5 additions & 3 deletions lib/src/store_change.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ class StoreChangeHandlerBuilder<
/// Registers [handler] function to the given [actionName]
void add<Payload>(ActionName<Payload> actionName,
StoreChangeHandler<Payload, State, StateBuilder> handler) {
_map[actionName.name] = handler;
_map[actionName.name] = (change) {
handler(change as StoreChange<State, StateBuilder, Payload>);
};
}

/// [build] sets up a subscription to the registered actions
build(Store<State, StateBuilder, Actions> store) {
void build(Store<State, StateBuilder, Actions> store) {
_subscription = store.stream
.listen((StoreChange<State, StateBuilder, dynamic> storeChange) {
var handler = _map[storeChange.action.name];
Expand All @@ -52,7 +54,7 @@ class StoreChangeHandlerBuilder<
}

/// [dispose] cancels the subscription to the store
dispose() {
void dispose() {
_subscription.cancel();
}
}
12 changes: 12 additions & 0 deletions lib/src/typedefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,15 @@ typedef NextActionHandler Middleware<
State extends Built<State, StateBuilder>,
StateBuilder extends Builder<State, StateBuilder>,
Actions extends ReduxActions>(MiddlewareApi<State, StateBuilder, Actions> api);

/// [SubstateChange] is the payload for `StateChangeTransformer`'s stream. It contains
/// the previous and next value of the state resulting from the mapper provided to `StateChangeTransformer`
class SubstateChange<Substate> {
Substate prev;
Substate next;
SubstateChange(this.prev, this.next);
}

/// [StateMapper] takes a state model and maps it to the values one cares about
typedef Substate StateMapper<State extends Built<State, StateBuilder>,
StateBuilder extends Builder<State, StateBuilder>, Substate>(State state);
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.1.0
version: 7.2.0
description:
A state management library written in dart that enforces immutability
authors:
Expand Down
8 changes: 5 additions & 3 deletions test/unit/action_generics_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ Reducer<ActionGenerics, ActionGenericsBuilder, dynamic>
(s, a, b) => b.count = s.count + a.payload)
..add<Null>(
ActionGenericsActionsNames.nullAction, (s, a, b) => b.count++)
..add<List<int>>(ActionGenericsActionsNames.listIntAction,
(s, a, b) => b.count += a.payload.fold(0, (c, n) => c + n))
..add<List<int>>(
ActionGenericsActionsNames.listIntAction,
(s, a, b) =>
b.count += a.payload.fold<int>(0, (c, n) => c + n))
..add<Map<String, List<int>>>(
ActionGenericsActionsNames.mapStringToListIntAction,
(s, a, b) =>
b.count += a.payload['k'].fold(0, (c, n) => c + n)))
b.count += a.payload['k'].fold<int>(0, (c, n) => c + n)))
.build();

/// Used to test code generation when the generic type of an action is a
Expand Down
5 changes: 3 additions & 2 deletions test/unit/action_generics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import 'package:test/test.dart';

import 'action_generics_models.dart';

main() {
void main() {
group('action generics', () {
Store<ActionGenerics, ActionGenericsBuilder, ActionGenericsActions> store;

setUp(() {
store = new Store(
store = new Store<ActionGenerics, ActionGenericsBuilder,
ActionGenericsActions>(
getActionGenericsReducer(),
new ActionGenerics(),
new ActionGenericsActions(),
Expand Down
4 changes: 2 additions & 2 deletions test/unit/collection_reducer_builders_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import 'package:test/test.dart';

import 'collection_models.dart';

main() {
void main() {
group('collection reducer builders', () {
Store<Collection, CollectionBuilder, CollectionActions> store;

setUp(() {
store = new Store(
store = new Store<Collection, CollectionBuilder, CollectionActions>(
getCollectionReducer(),
new Collection(),
new CollectionActions(),
Expand Down
6 changes: 3 additions & 3 deletions test/unit/inheritance_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import 'package:test/test.dart';

import 'inheritance_test_models.dart';

main() {
void main() {
group('inheritence', () {
Store<Child, ChildBuilder, ChildActions> store;

setUp(() {
store =
new Store(getInheritanceReducer(), new Child(), new ChildActions());
store = new Store<Child, ChildBuilder, ChildActions>(
getInheritanceReducer(), new Child(), new ChildActions());
});

tearDown(() {
Expand Down
Loading

0 comments on commit e76eb88

Please sign in to comment.