Skip to content

Commit f62b1cf

Browse files
authored
Merge pull request #3 from davidmarne/strong_mode
Strong mode
2 parents 76d29f0 + ff5cb3a commit f62b1cf

File tree

11 files changed

+68
-48
lines changed

11 files changed

+68
-48
lines changed

example/lib/middleware/creation_middleware.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ abstract class CreatorActions extends ReduxActions {
1717
factory CreatorActions() => new _$CreatorActions();
1818
}
1919

20-
var creatorMiddeware = (new MiddlwareBuilder<AppState, AppStateActions>()
20+
var creatorMiddeware = (new MiddlwareBuilder<AppState, AppStateBuilder, AppStateActions>()
2121
..add<String>(CreatorActionsNames.createGroup, _createGroup)
2222
..add<String>(CreatorActionsNames.createTodo, _createTodo))
2323
.build();
2424

25-
_createGroup(
26-
MiddlewareApi<AppState, AppStateActions> api, ActionHandler next, Action<String> action) {
25+
_createGroup(MiddlewareApi<AppState, AppStateBuilder, AppStateActions> api, ActionHandler next,
26+
Action<String> action) {
2727
var newGroup = _newGroup(action.payload);
2828
api.actions.groupActions.addGroup(newGroup);
2929
api.actions.setCurrentGroup(newGroup.id);
3030
}
3131

32-
_createTodo(
33-
MiddlewareApi<AppState, AppStateActions> api, ActionHandler next, Action<String> action) {
32+
_createTodo(MiddlewareApi<AppState, AppStateBuilder, AppStateActions> api, ActionHandler next,
33+
Action<String> action) {
3434
var newTodo = _newTodo(action.payload);
3535
api.actions.todosActions.addTodo(newTodo);
3636
api.actions.groupActions.addTodoToGroup(new AddTodoToGroupPayload()

example/lib/reducers/app_state.g.dart

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

example/web/index.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'package:example/example.dart';
1313
Future main() async {
1414
react_client.setClientConfiguration();
1515

16-
var reduxStore = new Store<AppState, AppStateActions>(
16+
var reduxStore = new Store<AppState, AppStateBuilder, AppStateActions>(
1717
new AppState(),
1818
new AppStateActions(),
1919
middleware: [creatorMiddeware],

lib/src/built_reducer.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ abstract class BuiltReducer<V extends Built<V, B>, B extends Builder<V, B>> impl
2525
/// with many different payload types, while maintaining type safety.
2626
/// Each [Reducer] added with add<T> must take a state of type V, an Action of type
2727
/// Action<T>, and a builder of type B
28-
class ReducerBuilder<V extends Built<V, B>, B extends Builder<V, B>> {
28+
class ReducerBuilder<V extends BuiltReducer<V, B>, B extends Builder<V, B>> {
2929
var _map = new Map<String, Reducer<dynamic, V, B>>();
3030

3131
add<T>(ActionName<T> aName, Reducer<T, V, B> reducer) => _map[aName.name] = reducer;

lib/src/middleware.dart

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import 'package:built_value/built_value.dart';
2+
13
import 'action.dart';
24
import 'built_reducer.dart';
35
import 'store.dart';
46
import 'typedefs.dart';
57

68
/// [MiddlewareApi] put in scope to your [Middleware] function by redux.
79
/// When using [MiddlwareBuilder] (recommended) [MiddlewareApi] is passed to your [MiddlewareHandler]
8-
class MiddlewareApi<State extends BuiltReducer, Actions extends ReduxActions> {
9-
Store<State, Actions> _store;
10+
class MiddlewareApi<State extends BuiltReducer<State, StateBuilder>,
11+
StateBuilder extends Builder<State, StateBuilder>, Actions extends ReduxActions> {
12+
Store<State, StateBuilder, Actions> _store;
1013
MiddlewareApi(this._store);
1114

1215
/// [state] returns the current state
@@ -18,28 +21,32 @@ class MiddlewareApi<State extends BuiltReducer, Actions extends ReduxActions> {
1821

1922
/// [MiddlwareBuilder] allows you to build a reducer that handles many different actions
2023
/// with many different payload types, while maintaining type safety.
21-
/// Each [MiddlewareHandler] added with add<T> must take a state of type V, an Action of type
24+
/// Each [MiddlewareHandler] added with add<T> must take a state of type State, an Action of type
2225
/// Action<T>, and a builder of type B
23-
class MiddlwareBuilder<State extends BuiltReducer, Actions extends ReduxActions> {
24-
var _map = new Map<String, MiddlewareHandler<State, Actions>>();
26+
class MiddlwareBuilder<State extends BuiltReducer<State, StateBuilder>,
27+
StateBuilder extends Builder<State, StateBuilder>, Actions extends ReduxActions> {
28+
var _map = new Map<String, MiddlewareHandler<State, StateBuilder, Actions>>();
2529

26-
add<T>(ActionName<T> aMgr, MiddlewareHandler<State, Actions> handler) => _map[aMgr.name] =
27-
handler;
30+
add<T>(ActionName<T> aMgr, MiddlewareHandler<State, StateBuilder, Actions> handler) =>
31+
_map[aMgr.name] = handler;
2832

2933
/// build returns a [Middlware] function that handles all actions added with [add]
30-
build() => (MiddlewareApi<State, Actions> api) => (ActionHandler next) => (Action action) {
31-
var handler = _map[action.name];
32-
if (handler != null) {
33-
handler(api, next, action);
34-
return;
35-
}
36-
37-
next(action);
38-
};
34+
build() =>
35+
(MiddlewareApi<State, StateBuilder, Actions> api) => (ActionHandler next) => (Action action) {
36+
var handler = _map[action.name];
37+
if (handler != null) {
38+
handler(api, next, action);
39+
return;
40+
}
41+
42+
next(action);
43+
};
3944
}
4045

4146
/// [MiddlewareHandler] is a function that handles an action in a middleware. Its is only for
4247
/// use with [MiddlwareBuilder]. If you are not using [MiddlwareBuilder] middleware must be
4348
/// decalred as a [Middleware] function.
44-
typedef MiddlewareHandler<State extends BuiltReducer, Actions extends ReduxActions>(
45-
MiddlewareApi<State, Actions> api, ActionHandler next, Action action);
49+
typedef MiddlewareHandler<
50+
State extends BuiltReducer<State, StateBuilder>,
51+
StateBuilder extends Builder<State, StateBuilder>,
52+
Actions extends ReduxActions>(MiddlewareApi<State, StateBuilder, Actions> api, ActionHandler next, Action action);

lib/src/store.dart

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
import 'dart:async';
22
import 'dart:core';
33

4+
import 'package:built_value/built_value.dart';
5+
46
import 'action.dart';
57
import 'built_reducer.dart';
68
import 'middleware.dart';
79
import 'typedefs.dart';
810

911
// TODO: extend disposable
10-
class Store<State extends BuiltReducer, Actions extends ReduxActions> {
12+
class Store<V extends BuiltReducer<V, B>, B extends Builder<V, B>, Actions extends ReduxActions> {
1113
// stream used for dispatching actions
1214
final StreamController<Action<dynamic>> _dispatch = new StreamController.broadcast();
1315

1416
// stream used to dispatch changes to the state
15-
final StreamController<State> _stateController = new StreamController.broadcast();
17+
final StreamController<V> _stateController = new StreamController.broadcast();
1618

1719
// the current state
18-
State _state;
20+
V _state;
1921
Actions _actions;
2022

2123
Store(
22-
State defaultState,
24+
V defaultState,
2325
Actions actions, {
24-
Iterable<Middleware<State>> middleware: const [],
26+
Iterable<Middleware<V, B, Actions>> middleware: const [],
2527
}) {
2628
// set the initial state
2729
_state = defaultState;
2830
_actions = actions;
2931
_actions.syncWithStore(_dispatch.add);
3032

31-
final MiddlewareApi api = new MiddlewareApi<State, Actions>(this);
33+
final MiddlewareApi api = new MiddlewareApi<V, B, Actions>(this);
3234

3335
// setup the middleware dispatch chain
3436
ActionHandler handler = (action) {
@@ -67,10 +69,10 @@ class Store<State extends BuiltReducer, Actions extends ReduxActions> {
6769
}
6870

6971
/// [subscribe] returns a stream that will be dispatched whenever the state changes
70-
Stream<State> get subscribe => _stateController.stream;
72+
Stream<V> get subscribe => _stateController.stream;
7173

7274
/// [state] returns the current state
73-
State get state => _state;
75+
V get state => _state;
7476

7577
/// [actions] returns the synced actions
7678
Actions get actions => _actions;

lib/src/typedefs.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:built_value/built_value.dart';
22

33
import 'action.dart';
4+
import 'built_reducer.dart';
45
import 'middleware.dart';
56

67
/// [Reducer] is a function that given a state of type V, an Action of type Action<P>, and a
@@ -16,4 +17,5 @@ typedef ActionHandler(Action a);
1617
typedef ActionHandler NextActionHandler(ActionHandler next);
1718

1819
/// [Middleware] is a function that given the store's [MiddlewareApi] returns a [NextActionHandler].
19-
typedef NextActionHandler Middleware<State>(MiddlewareApi api);
20+
typedef NextActionHandler Middleware<V extends BuiltReducer<V, B>, B extends Builder<V, B>,
21+
Actions extends ReduxActions>(MiddlewareApi<V, B, Actions> api);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: built_redux
2-
version: 0.0.7
2+
version: 0.1.0
33
description:
44
A state management library written in dart that enforces immutability
55
authors:

test/unit/redux_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import 'test_counter.dart';
77

88
main() {
99
group('redux', () {
10-
Store<BaseCounter, BaseCounterActions> store;
10+
Store<BaseCounter, BaseCounterBuilder, BaseCounterActions> store;
1111

1212
setup({int numMiddleware: 1}) {
1313
var actions = new BaseCounterActions();

test/unit/test_counter.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,13 @@ abstract class MiddlewareActions extends ReduxActions {
8787
factory MiddlewareActions() => new _$MiddlewareActions();
8888
}
8989

90-
var createCounterMiddleware = (new MiddlwareBuilder<BaseCounter, BaseCounterActions>()
91-
..add<int>(MiddlewareActionsNames.increment, _doubleIt))
92-
.build();
90+
var createCounterMiddleware =
91+
(new MiddlwareBuilder<BaseCounter, BaseCounterBuilder, BaseCounterActions>()
92+
..add<int>(MiddlewareActionsNames.increment, _doubleIt))
93+
.build();
9394

94-
_doubleIt(
95-
MiddlewareApi<BaseCounter, BaseCounterActions> api, ActionHandler next, Action<int> action) {
95+
_doubleIt(MiddlewareApi<BaseCounter, BaseCounterBuilder, BaseCounterActions> api,
96+
ActionHandler next, Action<int> action) {
9697
api.actions.increment(api.state.count * 2);
9798
next(action);
9899
}

0 commit comments

Comments
 (0)