-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
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
middleware - Why currying rather than (store, action, next) -> #534
Comments
Could be both actually ramda works that way. |
Sometimes we want to associate some local state with const rafScheduler = store => next => {
let queuedActions = [];
let frame = null;
function loop() {
frame = null;
try {
if (queuedActions.length) {
next(queuedActions.shift());
}
} finally {
maybeRaf();
}
}
function maybeRaf() {
if (queuedActions.length && !frame) {
frame = requestAnimationFrame(loop);
}
}
return action => {
if (!action.meta || !action.meta.raf) {
return next(action);
}
queuedActions.push(action);
maybeRaf();
return function cancel() {
queuedActions = queuedActions.filter(a => a !== action)
};
};
}; We could have made it |
Honestly when I read the docs, specifically this line:
I had expected the following: function logger(store, next) {]
return function dispatchAndLog(action) {
console.log('dispatching', action);
let result = next(action);
console.log('next state', store.getState());
return result;
};
} But was instead: function logger(store) {
return function wrapDispatchToAddLogging(next) {
return function dispatchAndLog(action) {
console.log('dispatching', action);
let result = next(action);
console.log('next state', store.getState());
return result;
};
}
} I guess I am not satisfied with "I don't see a problem with just going all the way." To me, the problem is that it is neednessly complicated. The call to this in
could simply be
This also deviates from express' middleware signature: |
Is store => next => action => yourcode more complicated to write than (store, next) => action => yourcode ? I understand your concerns, maybe you're right. Maybe it breaks some use case we haven't considered. I don't know. But this feedback would have been more useful at the time middleware was designed (it was all publicly discussed: #55). Right now there is a lot of middleware in the ecosystem that uses the current signature, and unless you see a way to change it in backwards-compatible way, I don't think “remove |
Thanks for the question. I was starting to question my understanding of the middleware lesson by believing that the currying was absolutely necessary 😄 |
I think that since the redux expectation of the function is this signature const middleware = store => next => action => {
// do something
} then you can write your middleware curried version as follows: _.curry((store, next, action) => {
// do something
}) then redux being calling middleware(store)(next)(action) will work with the curried version. |
For future reference, this is covered in the Redux FAQ entry on "why does the middleware signature use currying?". |
Hi, the new docs are great. One question I had while reading the whole middleware section is why does the middleware has to be curried? Why not simply:
Maybe a quick point explaining in the doc would be helpful.
The text was updated successfully, but these errors were encountered: