From 1de4b10378addbc3cef0f09d3a870edf88df4fa7 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 12 Sep 2019 06:56:43 +0400 Subject: [PATCH 1/4] Getting return value from middleware through dispatch. --- lib/src/store.dart | 10 +++++----- lib/src/utils.dart | 6 +++--- test/middleware_test.dart | 26 ++++++++++++++++++++++++++ test/test_data.dart | 25 ++++++++++++++++++++++++- 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/lib/src/store.dart b/lib/src/store.dart index aa27432..8439677 100644 --- a/lib/src/store.dart +++ b/lib/src/store.dart @@ -68,7 +68,7 @@ abstract class ReducerClass { /// counterReducer, /// middleware: [loggingMiddleware], /// ); -typedef void Middleware( +typedef dynamic Middleware( Store store, dynamic action, NextDispatcher next, @@ -98,7 +98,7 @@ typedef void Middleware( /// middleware: [new LoggingMiddleware()], /// ); abstract class MiddlewareClass { - void call(Store store, dynamic action, NextDispatcher next); + dynamic call(Store store, dynamic action, NextDispatcher next); } /// The contract between one piece of middleware and the next in the chain. Use @@ -108,7 +108,7 @@ abstract class MiddlewareClass { /// Middleware can optionally pass the original action or a modified action to /// the next piece of middleware, or never call the next piece of middleware at /// all. -typedef void NextDispatcher(dynamic action); +typedef dynamic NextDispatcher(dynamic action); /// Creates a Redux store that holds the app state tree. /// @@ -246,8 +246,8 @@ class Store { /// to the state using the given [Reducer]. Please note: [Middleware] can /// intercept actions, and can modify actions or stop them from passing /// through to the reducer. - void dispatch(dynamic action) { - _dispatchers[0](action); + dynamic dispatch(dynamic action) { + return _dispatchers[0](action); } /// Closes down the Store so it will no longer be operational. Only use this diff --git a/lib/src/utils.dart b/lib/src/utils.dart index c5c8957..459d0c1 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -218,11 +218,11 @@ class TypedMiddleware implements MiddlewareClass { TypedMiddleware(this.middleware); @override - void call(Store store, dynamic action, NextDispatcher next) { + dynamic call(Store store, dynamic action, NextDispatcher next) { if (action is Action) { - middleware(store, action, next); + return middleware(store, action, next); } else { - next(action); + return next(action); } } } diff --git a/test/middleware_test.dart b/test/middleware_test.dart index 2c41e54..4795c68 100644 --- a/test/middleware_test.dart +++ b/test/middleware_test.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:redux/redux.dart'; import 'package:test/test.dart'; @@ -75,5 +77,29 @@ void main() { expect(middleware1.counter, equals(2)); }); + + test('dispatch returns the value from middleware', () async { + final middleware1 = new PassThroughMiddleware(); + final middleware2 = new ThunkMiddleware(); + final ThunkAction thunkAction = (store) async { + await sleep(Duration(milliseconds: 300)); + store.dispatch("changed"); + }; + + final store = new Store( + stringReducer, + initialState: 'hello', + middleware: [middleware1, middleware2] + ); + + final awaitableAction = store.dispatch(thunkAction); + + // Did not change yet + expect(store.state, equals('hello')); + await awaitableAction; + // The effect has taken place + expect(store.state, equals('changed')); + }); + }); } diff --git a/test/test_data.dart b/test/test_data.dart index 47302b6..1f934c0 100644 --- a/test/test_data.dart +++ b/test/test_data.dart @@ -29,7 +29,7 @@ class StringReducer extends ReducerClass { class IncrementMiddleware extends MiddlewareClass { int counter = 0; final _invocationsController = - new StreamController.broadcast(sync: true); + new StreamController.broadcast(sync: true); Stream get invocations => _invocationsController.stream; @@ -72,6 +72,29 @@ class ExtraActionIfDispatchedIncrementMiddleware extends IncrementMiddleware { } } +class PassThroughMiddleware implements MiddlewareClass { + + @override + dynamic call(Store store, dynamic action, NextDispatcher next) { + return next(action); + } + +} + +typedef void ThunkAction(Store store); + +class ThunkMiddleware implements MiddlewareClass { + + @override + dynamic call(Store store, dynamic action, NextDispatcher next) { + if (action is Function) { + return action(store); + } else { + return next(action); + } + } +} + class TestAction1 {} class TestAction2 {} From 3234f78fefa5af78318d94f8859c363cee941569 Mon Sep 17 00:00:00 2001 From: Brian Egan Date: Mon, 11 Nov 2019 14:47:32 +0100 Subject: [PATCH 2/4] Fix dartfmt --- test/middleware_test.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/middleware_test.dart b/test/middleware_test.dart index 30875ff..7b02d98 100644 --- a/test/middleware_test.dart +++ b/test/middleware_test.dart @@ -79,14 +79,14 @@ void main() { }); test('dispatch returns the value from middleware', () async { - final middleware1 = new PassThroughMiddleware(); - final middleware2 = new ThunkMiddleware(); + final middleware1 = PassThroughMiddleware(); + final middleware2 = ThunkMiddleware(); final ThunkAction thunkAction = (store) async { await sleep(Duration(milliseconds: 300)); store.dispatch("changed"); }; - final store = new Store( + final store = Store( stringReducer, initialState: 'hello', middleware: [middleware1, middleware2] From eec2ecc2f902eb4fce5d734176fa9e39835a89f5 Mon Sep 17 00:00:00 2001 From: Brian Egan Date: Mon, 11 Nov 2019 15:50:07 +0100 Subject: [PATCH 3/4] Update middleware_test.dart --- test/middleware_test.dart | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/test/middleware_test.dart b/test/middleware_test.dart index 7b02d98..2a1f0c9 100644 --- a/test/middleware_test.dart +++ b/test/middleware_test.dart @@ -1,5 +1,4 @@ -import 'dart:io'; - +import 'dart:async'; import 'package:redux/redux.dart'; import 'package:test/test.dart'; @@ -79,20 +78,20 @@ void main() { }); test('dispatch returns the value from middleware', () async { - final middleware1 = PassThroughMiddleware(); - final middleware2 = ThunkMiddleware(); - final ThunkAction thunkAction = (store) async { - await sleep(Duration(milliseconds: 300)); + final passthrough = PassThroughMiddleware(); + final thunk = ThunkMiddleware(); + Future thunkAction(Store store) async { + await Future.delayed(Duration(milliseconds: 5)); store.dispatch("changed"); - }; + } final store = Store( - stringReducer, - initialState: 'hello', - middleware: [middleware1, middleware2] + stringReducer, + initialState: 'hello', + middleware: [passthrough, thunk], ); - final awaitableAction = store.dispatch(thunkAction); + final awaitableAction = store.dispatch(thunkAction) as Future; // Did not change yet expect(store.state, equals('hello')); @@ -100,6 +99,5 @@ void main() { // The effect has taken place expect(store.state, equals('changed')); }); - }); } From 01f5ad5c19876e9b6e3bb683a03186559a1cf071 Mon Sep 17 00:00:00 2001 From: Brian Egan Date: Mon, 11 Nov 2019 15:50:21 +0100 Subject: [PATCH 4/4] Update test_data.dart --- test/test_data.dart | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/test/test_data.dart b/test/test_data.dart index 0d18509..fc1acc8 100644 --- a/test/test_data.dart +++ b/test/test_data.dart @@ -28,9 +28,8 @@ class StringReducer extends ReducerClass { class IncrementMiddleware extends MiddlewareClass { int counter = 0; - final _invocationsController = - StreamController.broadcast(sync: true); - + final _invocationsController = StreamController.broadcast(sync: true); + Stream get invocations => _invocationsController.stream; @override @@ -73,18 +72,13 @@ class ExtraActionIfDispatchedIncrementMiddleware extends IncrementMiddleware { } class PassThroughMiddleware implements MiddlewareClass { - @override dynamic call(Store store, dynamic action, NextDispatcher next) { return next(action); } - } -typedef void ThunkAction(Store store); - class ThunkMiddleware implements MiddlewareClass { - @override dynamic call(Store store, dynamic action, NextDispatcher next) { if (action is Function) {