Skip to content

Commit aeddcd2

Browse files
authored
Merge pull request #37 from davidmarne/sync_actions
synchronous actions
2 parents 2101010 + 9a2266a commit aeddcd2

File tree

3 files changed

+31
-42
lines changed

3 files changed

+31
-42
lines changed

Diff for: lib/src/store.dart

+1-8
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ class Store<
1515
State extends Built<State, StateBuilder>,
1616
StateBuilder extends Builder<State, StateBuilder>,
1717
Actions extends ReduxActions> {
18-
// stream used for dispatching actions
19-
final StreamController<Action<dynamic>> _dispatch = new StreamController();
20-
2118
// stream used to dispatch changes to the state
2219
final StreamController<StoreChange<State, StateBuilder, dynamic>>
2320
_stateController = new StreamController.broadcast();
@@ -37,9 +34,6 @@ class Store<
3734

3835
_actions = actions;
3936

40-
// register the actions to dispatch onto this store's dispatcher
41-
_actions.setDispatcher(_dispatch.add);
42-
4337
final MiddlewareApi api =
4438
new MiddlewareApi<State, StateBuilder, Actions>(this);
4539

@@ -70,13 +64,12 @@ class Store<
7064
}
7165

7266
// call the handler when an action is dispatched
73-
_dispatch.stream.listen(handler);
67+
actions.setDispatcher(handler);
7468
}
7569

7670
/// [dispose] removes closes both the dispatch and subscription stream
7771
dispose() {
7872
_stateController.close();
79-
_dispatch.close();
8073
_state = null;
8174
_actions = null;
8275
}

Diff for: pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: built_redux
2-
version: 6.1.1
2+
version: 7.0.0
33
description:
44
A state management library written in dart that enforces immutability
55
authors:

Diff for: test/unit/redux_test.dart

+29-33
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,23 @@ main() {
3535

3636
test('base action updates state', () async {
3737
setup();
38+
expect(store.state.count, 1);
3839
store.actions.increment(4);
39-
var stateChange = await store.stream.first;
40-
expect(stateChange.prev.count, 1);
41-
expect(stateChange.next.count, 5);
40+
expect(store.state.count, 5);
4241
});
4342

4443
test('nested built value', () async {
4544
setup();
45+
expect(store.state.nestedCounter.count, 1);
4646
store.actions.nestedCounterActions.increment(4);
47-
var stateChange = await store.stream.first;
48-
expect(stateChange.prev.nestedCounter.count, 1);
49-
expect(stateChange.next.nestedCounter.count, 5);
47+
expect(store.state.nestedCounter.count, 5);
5048
});
5149

5250
test('middleware action doubles count and updates state', () async {
5351
setup();
52+
expect(store.state.count, 1);
5453
store.actions.middlewareActions.increment(0);
55-
var stateChange = await store.stream.first;
56-
expect(stateChange.prev.count, 1);
57-
expect(stateChange.next.count, 3);
54+
expect(store.state.count, 3);
5855
});
5956

6057
test('2 middlewares doubles count twice and updates state', () async {
@@ -70,14 +67,15 @@ main() {
7067
else
7168
onStateChangeCompleter2.complete(state);
7269
});
73-
// should add 2 twice
70+
71+
// should add double the current state twice
7472
store.actions.middlewareActions.increment(0);
75-
var stateChange = await store.stream.first;
73+
var stateChange = await onStateChangeCompleter.future;
7674
expect(stateChange.prev.count, 1);
7775
expect(stateChange.next.count, 3);
78-
stateChange = await store.stream.first;
76+
stateChange = await onStateChangeCompleter2.future;
7977
expect(stateChange.prev.count, 3);
80-
expect(stateChange.next.count, 5);
78+
expect(stateChange.next.count, 9);
8179
});
8280

8381
test('store change handler', () async {
@@ -125,14 +123,15 @@ main() {
125123

126124
test('state transformer', () async {
127125
setup();
128-
126+
final completer = new Completer<SubstateChange<int>>();
129127
final sub = store.substateStream<int>((BaseCounter state) => state.count);
128+
sub.first.then(completer.complete);
130129

131130
store.actions.increment(4);
132131
// would cause completer to complete twice and fail the test
133132
store.actions.nestedCounterActions.increment(1);
134133

135-
var change = await sub.first;
134+
var change = await completer.future;
136135
expect(change.prev, 1);
137136
expect(change.next, 5);
138137
});
@@ -172,65 +171,62 @@ main() {
172171

173172
test('nextState stream', () async {
174173
setup();
174+
final completer = new Completer<BaseCounter>();
175+
store.nextState.first.then(completer.complete);
175176
store.actions.increment(4);
176-
var stateChange = await store.nextState.first;
177+
var stateChange = await completer.future;
177178
expect(stateChange.count, 5);
178179
});
179180

180181
test('nextSubstate stream', () async {
181182
setup();
182183

183184
final sub = store.nextSubstate<int>((BaseCounter state) => state.count);
185+
final completer = new Completer<int>();
186+
sub.first.then(completer.complete);
184187

185188
store.actions.increment(4);
186189
// would cause completer to complete twice and fail the test
187190
store.actions.nestedCounterActions.increment(1);
188191

189-
var change = await sub.first;
192+
var change = await completer.future;
190193
expect(change, 5);
191194
});
192195

193196
test('ActionDispatcher<Null>', () async {
194197
setup();
198+
expect(store.state.count, 1);
195199
store.actions.incrementOne();
196-
var stateChange = await store.stream.first;
197-
expect(stateChange.prev.count, 1);
198-
expect(stateChange.next.count, 2);
200+
expect(store.state.count, 2);
199201
});
200202

201203
test('ActionDispatcher<Null> with null payload', () async {
202204
setup();
205+
expect(store.state.count, 1);
203206
store.actions.incrementOne(null);
204-
var stateChange = await store.stream.first;
205-
expect(stateChange.prev.count, 1);
206-
expect(stateChange.next.count, 2);
207+
expect(store.state.count, 2);
207208
});
208209

209210
test('ActionDispatcher<SomeTypeDef>', () async {
210211
setup();
212+
expect(store.state.count, 1);
211213
store.actions.thunkDispatcher(
212214
(MiddlewareApi<BaseCounter, BaseCounterBuilder, BaseCounterActions>
213215
api) {
214216
api.actions.incrementOne();
215217
});
216-
var stateChange = await store.stream.first;
217-
expect(stateChange.prev.count, 1);
218-
expect(stateChange.next.count, 2);
218+
expect(store.state.count, 2);
219219
});
220220

221221
test('payload with generic type', () async {
222222
setup();
223+
expect(store.state.count, 1);
223224
store.actions.genericAction1(<int>[1, 2, 3]);
224-
var stateChange = await store.stream.first;
225-
expect(stateChange.prev.count, 1);
226-
expect(stateChange.next.count, 7);
227-
225+
expect(store.state.count, 7);
228226
store.actions.genericAction2(<String, List<int>>{
229227
'add': [1, 2, 3]
230228
});
231-
stateChange = await store.stream.first;
232-
expect(stateChange.prev.count, 7);
233-
expect(stateChange.next.count, 13);
229+
expect(store.state.count, 13);
234230
});
235231
});
236232
}

0 commit comments

Comments
 (0)