-
Notifications
You must be signed in to change notification settings - Fork 40
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
"enhancer" is actually a middleware, not an enhancer #24
Comments
@rickyvetter, @hedgepigdaniel: It would be great to have some progress on this. I personally don't think We can then have those types mentioned above available in reductive: type store('action, 'state) = Store.t('action, 'state);
type reducer('action, 'state) = ('state, 'action) => 'state;
type middleware('action, 'state) =
(store('action, 'state), 'action => unit, 'action) => unit;
type storeCreator('action, 'state) =
(
~reducer: reducer('action, 'state),
~preloadedState: 'state,
~middleware: middleware('action, 'state)=?,
unit
) =>
store('action, 'state);
type storeEnhancer('action, 'state) =
storeCreator('action, 'state) => storeCreator('action, 'state); so we can define store enhancers with: let logIt: storeEnhancer('action, 'state) = (storeCreator: storeCreator('action, 'state)) => (~reducer, ~preloadedState, ~middleware=?, ()) => {
let someEffect = anything => Js.log(anything)
let store = {
...storeCreator(~reducer, ~preloadedState, ~middleware?, ()),
customDispatcher: Some((store, next, action) => {
action |> someEffect;
next(action);
Store.getState(store) |> someEffect;
})
};
Store.getState(store) |> someEffect;
store
}; and apply in the following way: let storeCreator = Reductive.Enhancers.logIt @@ Reductive.Store.create;
let store = storeCreator(
~reducer=appReducer,
~preloadedState={counter: 0, content: ""},
(),
); Those renaming enhancer to middleware will result in breaking change. Is there anything like |
Yeah I'm hesitant to do anything here because it would be a breaking change. I agree that enhancer probably isn't the best name because it doesn't match Redux, but it's still descriptive. @ambientlight makes good points that there really isn't a need for a dedicated applyMiddleware function since it's just function application. So we'd basically be making a breaking change from enhancer to middleware without actually using the enhancer name anywhere. It seems wasteful to introduce this churn without good reason. |
@rickyvetter: so we can technically introduce another storeCreator function with |
Definitely on board with mentioning in docs and adding types that might help clarify things. As far as making a breaking change I'm more hesitant. Do you feel like there is enough confusion here to warrant a breaking change? |
@rickyvetter: let's then maybe start with the pull requests with these types, docs and examples. I would personally just attach deprecation notice that will emit warning to existing storeCreator and give an alternative storeCreator. I am not super familiar, do we have enough editor/compiler support to give smart error messages here: |
I think there is a misunderstanding in Reductive about what a Redux enhancer is. From the redux docs:
getState
(returns the current state of the store),dispatch
(dispatches an action to the store), andnext
(dispatches an action to the next middleware in the chain). A middleware intercepts actions dispatched to the store and can block, change, or delay them or dispatch other actions as a result.Everything that a middleware can do can be done by an enhancer (as evidenced by the fact that the
applyMiddlewares
function from redux returns an enhancer), but the opposite is not true! Some things that enhancers can do that middlewares cannot:A middleware cannot do these things because it is only called when an action is dispatched to the store. An enhancer wraps the storeCreator, so it can change the state before the store is created and dispatch actions before other actions are dispatched.
I propose the following alternative:
enhancer
argument toStore.create
be removedapplyMiddleware
function, that returns an enhancer that applies the (possibly composed) middleware that is passed to it.This way it is clear how a store enhancer in the Redux sense can be applied, and the user controls the order in which enhancers and middlewares are applied.
Some types:
The text was updated successfully, but these errors were encountered: