-
Notifications
You must be signed in to change notification settings - Fork 215
Options for persistence relating to where we put our business logic and using middleware. #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hey, happy to help out. I think it all comes down to this question:
In that case, just use Example: logPreviousStateMiddleware(Store<int> store, action, NextDispatcher next) {
// Print the *previous* state
print('${new DateTime.now()}: ${store.state}');
// Then forward the action to your reducer
next(action);
} vs logNextStateMiddleware(Store<int> store, action, NextDispatcher next) {
// Forward the action to your reducer synchronously
next(action);
// Print the *new* state
print('${new DateTime.now()}: ${store.state}');
} Does this work for ya? Might be a simpler approach than some of the solutions proposed above. |
This seems like it should help yes...I understand the theory. I have another problem which I think, in a round about way, affects my first question and the general pickle I seem to have stumbled into. I have a few bits of middleware which then dispatch other actions, and those actions are sent to the reducers. The order in which they hit the reducers seems to be out of sync to when I called initially 'actioned' the middleware. Is there any way to force these dispatches to middleware to 'complete' before the next, synchronously? |
(I'm guessing the order is just based on where they are defined in the store's middleware). Should I use a thunk instead, there are two reasons for using middleware or a thunk
|
I guess following on from this, is there a way to ensure that the thunks are dispatched and caught by the reducer in order? Also, another problem, I do in my thunk2 I think the state.people value is old and hasn't taken into account the work done in thunk1 (ie the dispatch(thunk2) executes before dispatch(newUpdatePersonAction()) in thunk1 |
Would it be ok to pass the additional state required into the reducer?
https://github.com/twistedinferno/flutter_redux_counter |
again...I'm probably overcomplicating things (and sharing too many of my thoughts!!!) but...I created a new combineReducers2 which allows a typed reducer to take further data
|
Yo yo -- sorry for the delay, busy times!
Yep! If you need to coordinate when actions are dispatched from a middleware to the Store, it might make more sense to coordinate all of that logic within a single Middleware (since it seems these things are related).
Kinda the same answer as above. If order is important, I'd coordinate that stuff in a central place where you have full control.
Fa sho! As long as your creating a |
Hey @twistedinferno -- did this help? Happy to close the issue if you're feeling good, or answer more Qs if ya got more :) |
Hi Brian, thanks again for the responses :-) I think I've got it, yup happy for you to close this. Co-ordinating things in the middleware seems to have solved my problems along with the knowledge that my reducer will be in a post appstate after next(action) has been called (I had some problems here mostly due to making calls in the wrong order and misunderstanding how thunks will be processed). I'm not directly in this space right now (back in the front end) but my examples I have been tinkering with seem to be getting me where I want to be...I think the combineReducers2 thing is overcomplicating my scenario but it's good to get further into a library and understand it more (maybe it might be fine too!) (btw, thanks for a great talk in London a few weeks ago! I had to rush off so didn't have a chance to say hi but hopefully another time) |
Cool, glad it worked out :) Would have been fun to meet, but I'm sure I'll be in London again in the coming months! If there's another Flutter meetup I'll definitely join. |
Yup, I'll definitely make sure I have time to say hello this time :-) |
Uh oh!
There was an error while loading. Please reload this page.
Hi Brian, many thanks for the library once again 👍
What options do we have / what recomendations can you give for persistance? The main problem I'm having is that I put a lot of my business logic in my reducers. This seemed to be working out until I tried to add some persistence middleware
(as you can probably work out the persist will be calculated too early and will save old information, also some of my actions just pass an id such as the following action so I need to lookup the record...
ToggleExcludedAction(int myId)
this is handled in my reducer where I look up the object and do a copyWith to alter my record.
So this was my first problem, I am running my logic before I need to persist it, so...my next plan was to put more of my logic in my actions and I started to use redux thunk where it was necessary to get at the state (ie lookup an existing record by id). My next problem is that now my actions are tied to AppState and my tests aren't as clean becase I need to create AppState (previously I could create a simple Store based on just the data relevant to the reducer).
...here is the new Action...
which I feel makes it a little less clear in the test because now I have to create the AppState in my tests where a lot of the data is irrelevant to the test (previously I was just creating just the data that the reducer was based on which made it clear what the data dependencies were). It's not the end of the world but it's just not as clean and wondered if I had missed something or if there is an alternative option.
I'm now considering just putting the code into a function that accepts the required dependencies and letting the action (which has a dependency on AppState) call that function and only testing my function.
so I test this
but not this
ok...now I'm overthinking this problem but here is a more type safe class based approach
calling it like so
dispatch(new Action<UserFlag>().byId(new HasSeen(), 1))
there may be an easier way to this though!
The text was updated successfully, but these errors were encountered: