-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Experimentation: Action listener middleware #272
Conversation
Deploy preview for redux-starter-kit-docs ready! Built with commit 32c03cc https://deploy-preview-272--redux-starter-kit-docs.netlify.com |
…in development mode
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 32c03cc:
|
Also take a look at the console - it warns now if you use the same slice reducer twice, two slices with the same name and (which is why I added that initially) when you use a slice that has a middleware, but you forget adding the middleware. But it also opens up a new question:
|
This experiment looks interesting. I am looking forward to it! I have also been reviewing rematch which also offers similar and also an interesting API. One thing that is neat about rematch is how their export const count = {
/* truncated for brevity */
effects: dispatch => ({
// handle state changes with impure functions.
// use async/await for async actions
async incrementAsync(payload, rootState) {
await new Promise(resolve => setTimeout(resolve, 1000))
dispatch.count.increment(payload)
},
}),
} |
Hm... looking at rematch, I'm not a big fan. While their implicit binding may sure save some imports, it takes away a lot of clarity. Also, it might make usage with TS more difficult. While now, custom typing of Also, it adds the assumption that these properties have to exist on The core question is another: do we want to follow this approach at all? Is it worth it? Can we find good solutions for the questions I brought up in #272 (comment) ? |
since I think self referencing the slice is fine too, like you did in your example Actually, I think referencing them might be best ( |
Honestly, I don't see the value of In the best case, it may save a few characters (only in code, minification might even be worse), but add a whole new concept to reach that - and one that can't be used everywhere, so it will be confusing for people in a "what can I use in which context" way. If you spend a few hours in the redux reactiflux channel, you'll see people struggling with all these concepts they have to learn at once, I'd rather not add another concept if it doesn't offer great value, and I can't see that value there. |
I agree! |
This is pretty much superseded by #547 |
This is some experimentation around a action listener middleware. (See #237)
@markerikson I would love your input on this approach.
I've added it as a standalone middleware creation function, as an option to createSlice, and added development mode runtime validations that add a few sanity checks, most importantly that if a slice has a middleware, it that middleware has been included when the reducer is being used.
This currently has not tests or documentation whatsoever, it's really just a draft.
Also, I haven't tried this out myself... opening up this PR so that I can play around with it in a shiny codesandbox :D