Closed robinweser closed 1 year ago
@farskid What do you think on this one? Might be a better idea to add it to the createStore
method as well, having a single source of truth that can be customized etc.
I like the idea of having middlewares. is there a specific reason behind the name listeners
? cause it seems to me, they are middlewares and not subscribe funcs, right?
Actually they’re more like subscribe functions as they do not alter the state (thats what actions, effects are for).
They should rather be used for logging, caching etc.
Or is there a good reason why they should be able to alter state?
OK, I've got a proposal:
What if we have the middlewares in a way that matches the middleware interface of Redux? a function that accepts store => next => action
.
There a couple of pros on this approach:
What do you think?
That's not possible as we need to have synchronous middleware as it's executed within React's async setState already... We could have middleware with the following API: (currentState) => nextState
but that's all here.
All other options would drastically change the current behaviour which is too verbose for me as that's the whole point with this micro package.
That is true! (currentState) => [compose_all_middlewares_resutls_here] => nextState
is a fine API. we can do the setState as the final step on nextState
in order not to get into the pitfall on async state updates.
@farskid I updated the PR, would love to hear your thoughts :)
This sounds like a promising start.
question: Does middleware get called on effects too? Am I missing something here?
Effects now actually dont mutate state directly, but call actions instead which then triggers middleware again ;)
Middleware
This PR adds a minimal API for adding middleware. Middleware are functions that receive the new state and return a (modified) new state.
Doing the following:
will trigger the listeners, which logs the current state: