Comments (4)
Hey hey -- the example you've posted will produce an infinite loop (b/c it will always keep dispatching a new action), but I think I get the overall Q.
The next
function will forward the action to the next middleware in the chain synchronously. If all of your Middleware synchronously call next
, it should reach the reducer first. Do you have any other middleware further down the chain that might be delaying the call to next
by wrapping the invocation in an async call of some kind?
This test works, for example. It verifies that the action forwarded by the next
function reaches the reducer first.
test('next actions reach reducer first', () async {
final List<dynamic> actions = <dynamic>[];
void middleware(
Store<String> store,
dynamic action,
NextDispatcher next,
) {
next(action);
if (action == 'I') {
store.dispatch('F');
}
}
String reducer(String state, dynamic action) {
actions.add(action);
return state;
}
final store = new Store<String>(
reducer,
middleware: [middleware],
);
store.dispatch('I');
expect(actions, ['I', 'F']);
});
from flutter_redux.
Thanks Brian. I figured out what confused me yesterday when looking at this. Check this out:
test('next actions reach reducer first', () async {
final List<dynamic> actions = <dynamic>[];
void middleware(
Store<String> store,
dynamic action,
NextDispatcher next,
) {
next(action);
if (action == 'Approve') {
store.dispatch('Save');
}
}
String reducer(String state, dynamic action) {
actions.add(action);
return state;
}
final store = new Store<String>(
reducer,
middleware: [
new LoggingMiddleware.printer(),
middleware],
);
store.dispatch('Approve');
expect(actions, ['Approve', 'Save']);
});
Even though this test passes (proving that reducers are running when expected), the logging middleware (which is all that I was using to analyze the situation yesterday) outputs:
[INFO] LoggingMiddleware: {Action: Save, State: null, ts: 2018-05-04 10:48:06.755677}
[INFO] LoggingMiddleware: {Action: Approve, State: null, ts: 2018-05-04 10:48:06.764199}
This is the opposite of what I would intuitively expect, and I don't quite have my head around why this is. I note that swapping the order of my middleware helps:
middleware: [
middleware,
new LoggingMiddleware.printer(),
],
Gives me:
[INFO] LoggingMiddleware: {Action: Approve, State: null, ts: 2018-05-04 10:49:44.685586}
[INFO] LoggingMiddleware: {Action: Save, State: null, ts: 2018-05-04 10:49:44.694610}
Should my logging middleware always be at the end of my middleware pipeline? Conceptually, when I think of dispatching an action, I imagine it traveling through the middleware in the order of definition (so putting the logging middleware first made sense to me, since I figured it would be the first thing to run on each action received). Am I misguided here?
from flutter_redux.
Heya Kent,
Sorry, traveling the past few days! So, it looks like you found a bug in the Logger implementation :) I'll push up a fix next week when I'm back in action :)
Best,
Brian
from flutter_redux.
Heya @kentcb -- I tried to fix this bug, but it caused other issues. The only way to get it to work consistently is to put the logger as the last middleware in the chain, which I found is the same restriction as the logging middleware for Redux.js as well.
The problem? The middleware must call next
before logging so it has access to the updated State. However, this also triggers the bug you're seeing here. The only fix is to put the loggingMiddleware as the last middleware in the array.
I've updated the documentation to reflect this! Sorry about the confusion.
from flutter_redux.
Related Issues (20)
- How to not accept change notifications when the Router is not at the top level, and then take the initiative to obtain the changes when the Router returns to the top level HOT 1
- onDidChange called twice HOT 8
- Is this package still actively maintained?
- `onWillChange` and `onDidChange` errors are swallowed HOT 4
- Warning related to `?` operator on Linux/Dev channel. HOT 2
- Import Store data type
- Widgets binding error after upgrading flutter version to 2.13.0-0.1.pre HOT 1
- onDidChange not working as expected HOT 3
- Build warning with Flutter 3.0 HOT 5
- flutter_redux depends on flutter_redux, version solving failed HOT 3
- onWillChange and onDidChange called twice after state update HOT 2
- Passing Store as an argument to MethodChannel and Background Isolate
- Question on how to use the reducer right way HOT 2
- [Question]: Any thoughts on difference between ignoreChange and distinct?
- Socket and Flutter_redux HOT 18
- Dispatching an action that does not alter state rerenders the widget, causing an infinite loop HOT 5
- How to watch redux store change inside useEffect? HOT 5
- [Question] is it expected that the store doesn't run until frames render? HOT 1
- After push a new page, and Dispatching an action, there is an infinite loop
- Is flutter redux actively maintained HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter_redux.