Skip to content

Commit 94dadf6

Browse files
authored
Merge pull request #4 from davidmarne/better_generic_names
better generic names
2 parents 8431f8f + 8bf9967 commit 94dadf6

File tree

7 files changed

+42
-31
lines changed

7 files changed

+42
-31
lines changed

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ packages:
6666
path: ".."
6767
relative: true
6868
source: path
69-
version: "0.0.6"
69+
version: "0.1.0"
7070
built_value:
7171
description:
7272
name: built_value

lib/src/action.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Action<Payload> {
99
}
1010

1111
// Dispatches an action to the store
12-
typedef Dispatcher<P>(Action<P> action);
12+
typedef void Dispatcher<P>(Action<P> action);
1313

1414
/// [ActionDispatcher] dispatches an action with the name provided
1515
/// in the constructor and the payload supplied when called. You will notice
@@ -26,13 +26,13 @@ class ActionDispatcher<P> {
2626

2727
String get name => _name;
2828

29-
call(P payload) => _dispatcher(new Action<P>()
29+
void call(P payload) => _dispatcher(new Action<P>()
3030
..name = name
3131
..payload = payload);
3232

3333
ActionDispatcher(this._name);
3434

35-
syncWithStore(dispatcher) {
35+
void syncWithStore(dispatcher) {
3636
_dispatcher = dispatcher;
3737
}
3838
}
@@ -87,7 +87,7 @@ class ActionDispatcher<P> {
8787
/// ```
8888
///
8989
abstract class ReduxActions {
90-
syncWithStore(dispatcher);
90+
void syncWithStore(dispatcher);
9191
}
9292

9393
/// [ActionName] is an object that simply contains the action name but is typed with a generic that

lib/src/built_reducer.dart

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import 'package:built_value/built_value.dart';
33
import 'action.dart';
44
import 'typedefs.dart';
55

6-
abstract class BuiltReducer<V extends Built<V, B>, B extends Builder<V, B>> implements Built<V, B> {
6+
abstract class BuiltReducer<State extends Built<State, StateBuilder>,
7+
StateBuilder extends Builder<State, StateBuilder>> implements Built<State, StateBuilder> {
78
/// This is a map, not a single function with switch statement as per js redux implementation.
89
/// This is so each reducer can have the action payload generic be a different non dynamic value
9-
Map<String, Reducer<dynamic, V, B>> get reducer;
10+
Map<String, Reducer<dynamic, State, StateBuilder>> get reducer;
1011

11-
void reduce(V state, Action<dynamic> a, B builder) {
12+
void reduce(State state, Action<dynamic> a, StateBuilder builder) {
1213
var handler = reducer[a.name];
1314
// TODO: warn if payload type doesn't match reducer
1415
if (handler != null) handler(state, a, builder);
@@ -18,17 +19,20 @@ abstract class BuiltReducer<V extends Built<V, B>, B extends Builder<V, B>> impl
1819

1920
/// Generated by the transformer for developer convienience
2021
/// It will call reduce on any properties of your [BuiltReduer] that are also of type [BuiltReducer]
21-
void reduceChildren(V state, Action<dynamic> a, B builder) {}
22+
void reduceChildren(State state, Action<dynamic> a, StateBuilder builder) {}
2223
}
2324

2425
/// [ReducerBuilder] allows you to build a reducer that handles many different actions
2526
/// with many different payload types, while maintaining type safety.
26-
/// Each [Reducer] added with add<T> must take a state of type V, an Action of type
27+
/// Each [Reducer] added with add<T> must take a state of type State, an Action of type
2728
/// Action<T>, and a builder of type B
28-
class ReducerBuilder<V extends BuiltReducer<V, B>, B extends Builder<V, B>> {
29-
var _map = new Map<String, Reducer<dynamic, V, B>>();
29+
class ReducerBuilder<State extends BuiltReducer<State, StateBuilder>,
30+
StateBuilder extends Builder<State, StateBuilder>> {
31+
var _map = new Map<String, Reducer<dynamic, State, StateBuilder>>();
3032

31-
add<T>(ActionName<T> aName, Reducer<T, V, B> reducer) => _map[aName.name] = reducer;
33+
void add<Payload>(ActionName<Payload> aName, Reducer<Payload, State, StateBuilder> reducer) {
34+
_map[aName.name] = reducer;
35+
}
3236

33-
build() => _map;
37+
Map<String, Reducer<dynamic, State, StateBuilder>> build() => _map;
3438
}

lib/src/middleware.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ class MiddlwareBuilder<State extends BuiltReducer<State, StateBuilder>,
2727
StateBuilder extends Builder<State, StateBuilder>, Actions extends ReduxActions> {
2828
var _map = new Map<String, MiddlewareHandler<State, StateBuilder, Actions>>();
2929

30-
add<T>(ActionName<T> aMgr, MiddlewareHandler<State, StateBuilder, Actions> handler) =>
31-
_map[aMgr.name] = handler;
30+
void add<T>(ActionName<T> aMgr, MiddlewareHandler<State, StateBuilder, Actions> handler) {
31+
_map[aMgr.name] = handler;
32+
}
3233

3334
/// build returns a [Middlware] function that handles all actions added with [add]
34-
build() =>
35+
Middleware<State, StateBuilder, Actions> build() =>
3536
(MiddlewareApi<State, StateBuilder, Actions> api) => (ActionHandler next) => (Action action) {
3637
var handler = _map[action.name];
3738
if (handler != null) {

lib/src/store.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,30 @@ import 'built_reducer.dart';
88
import 'middleware.dart';
99
import 'typedefs.dart';
1010

11-
// TODO: extend disposable
12-
class Store<V extends BuiltReducer<V, B>, B extends Builder<V, B>, Actions extends ReduxActions> {
11+
/// [Store] is the container of your state.
12+
class Store<State extends BuiltReducer<State, StateBuilder>,
13+
StateBuilder extends Builder<State, StateBuilder>, Actions extends ReduxActions> {
1314
// stream used for dispatching actions
1415
final StreamController<Action<dynamic>> _dispatch = new StreamController.broadcast();
1516

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

1920
// the current state
20-
V _state;
21+
State _state;
2122
Actions _actions;
2223

2324
Store(
24-
V defaultState,
25+
State defaultState,
2526
Actions actions, {
26-
Iterable<Middleware<V, B, Actions>> middleware: const [],
27+
Iterable<Middleware<State, StateBuilder, Actions>> middleware: const [],
2728
}) {
2829
// set the initial state
2930
_state = defaultState;
3031
_actions = actions;
3132
_actions.syncWithStore(_dispatch.add);
3233

33-
final MiddlewareApi api = new MiddlewareApi<V, B, Actions>(this);
34+
final MiddlewareApi api = new MiddlewareApi<State, StateBuilder, Actions>(this);
3435

3536
// setup the middleware dispatch chain
3637
ActionHandler handler = (action) {
@@ -69,10 +70,10 @@ class Store<V extends BuiltReducer<V, B>, B extends Builder<V, B>, Actions exten
6970
}
7071

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

7475
/// [state] returns the current state
75-
V get state => _state;
76+
State get state => _state;
7677

7778
/// [actions] returns the synced actions
7879
Actions get actions => _actions;

lib/src/typedefs.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import 'middleware.dart';
66

77
/// [Reducer] is a function that given a state of type V, an Action of type Action<P>, and a
88
/// builder of type B builds the next state
9-
typedef void Reducer<P, V extends Built<V, B>, B extends Builder<V, B>>(
10-
V state, Action<P> action, B builder);
9+
typedef void Reducer<
10+
Payload,
11+
State extends Built<State, StateBuilder>,
12+
StateBuilder extends Builder<State,
13+
StateBuilder>>(State state, Action<Payload> action, StateBuilder builder);
1114

1215
/// [ActionHandler] handles an action, this will contain the actual middleware logic
1316
typedef ActionHandler(Action a);
@@ -17,5 +20,7 @@ typedef ActionHandler(Action a);
1720
typedef ActionHandler NextActionHandler(ActionHandler next);
1821

1922
/// [Middleware] is a function that given the store's [MiddlewareApi] returns a [NextActionHandler].
20-
typedef NextActionHandler Middleware<V extends BuiltReducer<V, B>, B extends Builder<V, B>,
21-
Actions extends ReduxActions>(MiddlewareApi<V, B, Actions> api);
23+
typedef NextActionHandler Middleware<
24+
State extends BuiltReducer<State, StateBuilder>,
25+
StateBuilder extends Builder<State, StateBuilder>,
26+
Actions extends ReduxActions>(MiddlewareApi<State, StateBuilder, 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.1.0
2+
version: 0.1.1
33
description:
44
A state management library written in dart that enforces immutability
55
authors:

0 commit comments

Comments
 (0)