Skip to content

Commit 167cdce

Browse files
authored
Merge pull request #52 from andrasferenczi/master
Have dispatch return a value
2 parents a2bbdac + 01f5ad5 commit 167cdce

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

lib/src/store.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ abstract class ReducerClass<State> {
7070
/// counterReducer,
7171
/// middleware: [loggingMiddleware],
7272
/// );
73-
typedef void Middleware<State>(
73+
typedef dynamic Middleware<State>(
7474
Store<State> store,
7575
dynamic action,
7676
NextDispatcher next,
@@ -101,7 +101,7 @@ typedef void Middleware<State>(
101101
/// );
102102
abstract class MiddlewareClass<State> {
103103
/// A [Middleware] function that intercepts a dispatched action
104-
void call(Store<State> store, dynamic action, NextDispatcher next);
104+
dynamic call(Store<State> store, dynamic action, NextDispatcher next);
105105
}
106106

107107
/// The contract between one piece of middleware and the next in the chain. Use
@@ -111,7 +111,7 @@ abstract class MiddlewareClass<State> {
111111
/// Middleware can optionally pass the original action or a modified action to
112112
/// the next piece of middleware, or never call the next piece of middleware at
113113
/// all.
114-
typedef void NextDispatcher(dynamic action);
114+
typedef dynamic NextDispatcher(dynamic action);
115115

116116
/// Creates a Redux store that holds the app state tree.
117117
///
@@ -264,8 +264,7 @@ class Store<State> {
264264
/// intercept actions, and can modify actions or stop them from passing
265265
/// through to the reducer.
266266
dynamic dispatch(dynamic action) {
267-
_dispatchers[0](action);
268-
return action;
267+
return _dispatchers[0](action);
269268
}
270269

271270
/// Closes down the Store so it will no longer be operational. Only use this

lib/src/utils.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,11 @@ class TypedMiddleware<State, Action> implements MiddlewareClass<State> {
224224
TypedMiddleware(this.middleware);
225225

226226
@override
227-
void call(Store<State> store, dynamic action, NextDispatcher next) {
227+
dynamic call(Store<State> store, dynamic action, NextDispatcher next) {
228228
if (action is Action) {
229-
middleware(store, action, next);
229+
return middleware(store, action, next);
230230
} else {
231-
next(action);
231+
return next(action);
232232
}
233233
}
234234
}

test/middleware_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:async';
12
import 'package:redux/redux.dart';
23
import 'package:test/test.dart';
34

@@ -75,5 +76,28 @@ void main() {
7576

7677
expect(middleware1.counter, equals(2));
7778
});
79+
80+
test('dispatch returns the value from middleware', () async {
81+
final passthrough = PassThroughMiddleware<String>();
82+
final thunk = ThunkMiddleware<String>();
83+
Future<void> thunkAction(Store<String> store) async {
84+
await Future<void>.delayed(Duration(milliseconds: 5));
85+
store.dispatch("changed");
86+
}
87+
88+
final store = Store<String>(
89+
stringReducer,
90+
initialState: 'hello',
91+
middleware: [passthrough, thunk],
92+
);
93+
94+
final awaitableAction = store.dispatch(thunkAction) as Future<void>;
95+
96+
// Did not change yet
97+
expect(store.state, equals('hello'));
98+
await awaitableAction;
99+
// The effect has taken place
100+
expect(store.state, equals('changed'));
101+
});
78102
});
79103
}

test/test_data.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ class StringReducer extends ReducerClass<String> {
2828

2929
class IncrementMiddleware extends MiddlewareClass<String> {
3030
int counter = 0;
31-
final _invocationsController =
32-
StreamController<String>.broadcast(sync: true);
31+
final _invocationsController = StreamController<String>.broadcast(sync: true);
3332

3433
Stream<String> get invocations => _invocationsController.stream;
3534

@@ -72,6 +71,24 @@ class ExtraActionIfDispatchedIncrementMiddleware extends IncrementMiddleware {
7271
}
7372
}
7473

74+
class PassThroughMiddleware<State> implements MiddlewareClass<State> {
75+
@override
76+
dynamic call(Store<State> store, dynamic action, NextDispatcher next) {
77+
return next(action);
78+
}
79+
}
80+
81+
class ThunkMiddleware<State> implements MiddlewareClass<State> {
82+
@override
83+
dynamic call(Store<State> store, dynamic action, NextDispatcher next) {
84+
if (action is Function) {
85+
return action(store);
86+
} else {
87+
return next(action);
88+
}
89+
}
90+
}
91+
7592
class TestAction1 {}
7693

7794
class TestAction2 {}

0 commit comments

Comments
 (0)