Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions lib/src/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ class Store<
State extends Built<State, StateBuilder>,
StateBuilder extends Builder<State, StateBuilder>,
Actions extends ReduxActions> {
// stream used for dispatching actions
final StreamController<Action<dynamic>> _dispatch = new StreamController();

// stream used to dispatch changes to the state
final StreamController<StoreChange<State, StateBuilder, dynamic>>
_stateController = new StreamController.broadcast();
Expand All @@ -37,9 +34,6 @@ class Store<

_actions = actions;

// register the actions to dispatch onto this store's dispatcher
_actions.setDispatcher(_dispatch.add);

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

Expand Down Expand Up @@ -70,13 +64,12 @@ class Store<
}

// call the handler when an action is dispatched
_dispatch.stream.listen(handler);
actions.setDispatcher(handler);
}

/// [dispose] removes closes both the dispatch and subscription stream
dispose() {
_stateController.close();
_dispatch.close();
_state = null;
_actions = null;
}
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: 6.1.1
version: 7.0.0
description:
A state management library written in dart that enforces immutability
authors:
Expand Down
62 changes: 29 additions & 33 deletions test/unit/redux_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,23 @@ main() {

test('base action updates state', () async {
setup();
expect(store.state.count, 1);
store.actions.increment(4);
var stateChange = await store.stream.first;
expect(stateChange.prev.count, 1);
expect(stateChange.next.count, 5);
expect(store.state.count, 5);
});

test('nested built value', () async {
setup();
expect(store.state.nestedCounter.count, 1);
store.actions.nestedCounterActions.increment(4);
var stateChange = await store.stream.first;
expect(stateChange.prev.nestedCounter.count, 1);
expect(stateChange.next.nestedCounter.count, 5);
expect(store.state.nestedCounter.count, 5);
});

test('middleware action doubles count and updates state', () async {
setup();
expect(store.state.count, 1);
store.actions.middlewareActions.increment(0);
var stateChange = await store.stream.first;
expect(stateChange.prev.count, 1);
expect(stateChange.next.count, 3);
expect(store.state.count, 3);
});

test('2 middlewares doubles count twice and updates state', () async {
Expand All @@ -70,14 +67,15 @@ main() {
else
onStateChangeCompleter2.complete(state);
});
// should add 2 twice

// should add double the current state twice
store.actions.middlewareActions.increment(0);
var stateChange = await store.stream.first;
var stateChange = await onStateChangeCompleter.future;
expect(stateChange.prev.count, 1);
expect(stateChange.next.count, 3);
stateChange = await store.stream.first;
stateChange = await onStateChangeCompleter2.future;
expect(stateChange.prev.count, 3);
expect(stateChange.next.count, 5);
expect(stateChange.next.count, 9);
});

test('store change handler', () async {
Expand Down Expand Up @@ -125,14 +123,15 @@ main() {

test('state transformer', () async {
setup();

final completer = new Completer<SubstateChange<int>>();
final sub = store.substateStream<int>((BaseCounter state) => state.count);
sub.first.then(completer.complete);

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

var change = await sub.first;
var change = await completer.future;
expect(change.prev, 1);
expect(change.next, 5);
});
Expand Down Expand Up @@ -172,65 +171,62 @@ main() {

test('nextState stream', () async {
setup();
final completer = new Completer<BaseCounter>();
store.nextState.first.then(completer.complete);
store.actions.increment(4);
var stateChange = await store.nextState.first;
var stateChange = await completer.future;
expect(stateChange.count, 5);
});

test('nextSubstate stream', () async {
setup();

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

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

var change = await sub.first;
var change = await completer.future;
expect(change, 5);
});

test('ActionDispatcher<Null>', () async {
setup();
expect(store.state.count, 1);
store.actions.incrementOne();
var stateChange = await store.stream.first;
expect(stateChange.prev.count, 1);
expect(stateChange.next.count, 2);
expect(store.state.count, 2);
});

test('ActionDispatcher<Null> with null payload', () async {
setup();
expect(store.state.count, 1);
store.actions.incrementOne(null);
var stateChange = await store.stream.first;
expect(stateChange.prev.count, 1);
expect(stateChange.next.count, 2);
expect(store.state.count, 2);
});

test('ActionDispatcher<SomeTypeDef>', () async {
setup();
expect(store.state.count, 1);
store.actions.thunkDispatcher(
(MiddlewareApi<BaseCounter, BaseCounterBuilder, BaseCounterActions>
api) {
api.actions.incrementOne();
});
var stateChange = await store.stream.first;
expect(stateChange.prev.count, 1);
expect(stateChange.next.count, 2);
expect(store.state.count, 2);
});

test('payload with generic type', () async {
setup();
expect(store.state.count, 1);
store.actions.genericAction1(<int>[1, 2, 3]);
var stateChange = await store.stream.first;
expect(stateChange.prev.count, 1);
expect(stateChange.next.count, 7);

expect(store.state.count, 7);
store.actions.genericAction2(<String, List<int>>{
'add': [1, 2, 3]
});
stateChange = await store.stream.first;
expect(stateChange.prev.count, 7);
expect(stateChange.next.count, 13);
expect(store.state.count, 13);
});
});
}