-
-
Notifications
You must be signed in to change notification settings - Fork 15.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
Return value of dispatch #61
Comments
My usage example export function runTransition(url) {
return async perform => {
const route = routing
.filter(({route}) => route === url)
.shift() || notFound
const transition = transitions[route.transition]
const componentKey = route.component
const {title, status, ...props} = await perform(transition())
perform(changePage(componentKey, props))
return {
url, title, status, componentKey, props
}
}
}
export function navigateTo(url) {
return async perform => {
progressIndicator(async () => {
const {title} = await perform(runTransition(url))
document.title = title
history.pushState({url}, title, url)
})
}
} I have |
Server side usage example: export default async function renderPage(url) {
const dispatcher = createDispatcher()
const {title, status} = await dispatcher.perform(runTransition(url))
const atom = dispatcher.getAtom()
const pageContent = React.renderToString(
<Application dispatcher={dispatcher} />
)
return {
status,
body: appTemplate(title, pageContent, atom)
}
} |
Thank you for the examples! Let's leave this open for now, I'll come back to it later.. |
Returning a promise is the uber example. In all the Flux code I've ever written, I've only ever needed the return value for that reason. |
I'm not sure if the dispatch should return anything at all, the whole idea around the dispatcher is to do "fire and forget" and handle data that follows that action as app state, whenever it is received. |
I'm in favor of the the base function promiseMiddleware(next) {
return action =>
action && typeof action.then === 'function'
? action.then(next)
: next(action);
} The dispatcher is still fire and forget because it has no notion that a promise was ever involved. It merely receives a payload object and sends it through the store, synchronously. But the caller can still know the dispatcher has completed because the call to the dispatcher was proxied by the middleware. In Flummox, the original behavior for async actions was to return a promise to the caller, but it would always resolve to Furthermore, in Redux, we can let the users decide if and how they want promise handling to work by making it easy to write their own custom middleware. |
I also think the dispatcher should just "fire and forget" |
But how to implement action creators from my example if |
I don't know exactly what those |
@vslinko Couldn't you just get it from the atom? You have access to it right there a few lines down. |
Middleware can always return things, but the base dispatch method would not. |
Transition is action creator that fetches all data for page. return function homepageTransition() {
return async perform => {
const apiResponse = (await homepageApiRequest())
perform(mergeResources(collectResources(apiResponse)))
return {
status,
title,
queryParams,
apiResponse,
}
}
}
export function runTransition(url) {
return async perform => {
const route = routing
.map(route => {
return {...route, match: matchRoutePattern(route.route, url)}
})
.filter(({match}) => !!match)
.shift()
if (!route) {
return perform(runNotFoundTransition(url))
}
const transition = transitions[route.transition]
const componentKey = route.component
const {title, status, ...props} = await perform(transition(route.match || {}))
if (status === 404) {
return perform(runNotFoundTransition(url))
}
if (status === 500) {
return perform(runErrorTransition(url, props))
}
perform(changePage(componentKey, props))
return {
url, title, status
}
}
}
export function navigateTo(url) {
return async perform => {
const {title} = await perform(runTransition(url))
document.title = title
history.pushState({url}, title, url)
}
}
Only on server side. But how to get it in |
I think you can solve your problem if you manually call the other actionCreators by manually passing the instead of this: const {title} = await perform(runTransition(url)) You do this: const {title} = await runTransition(url)(perform) And the same inside the Instead of this: const {title, status, ...props} = await perform(transition(route.match || {})) You do this: const {title, status, ...props} = await transition(route.match || {})(perform) So conceptually you only call the dispatcher with one action creator from outside, and that action creator dispatches several actions, only the logic is spread in other functions that receive the dispatch function. Another analysis that should be done is if you really need the transitions and the This is just an idea, let me know if it works at all or not |
Yes, I know that. |
Yes, you're right about that, you don't want to pass state just because. Anyway, I still think you shouldn't need to use the returned values from functions. For example in the The server side part is trickier I think, since it's hard to know when there are no remaining actions to be dispatched if we cannot |
You can await a promise if you use middleware. Just because the dispatcher doesn't return anything doesn't mean your middleware can't. See my example above #61 (comment) |
Makes sense. But now I'm wondering if I didn't want to use promises but still wanted to know when the dispatch ends on the server side in order to send the request, how would I do that if i have ACs using the async function way? |
@leoasis if you don't use promises and async/await you could subscribe to dispatcher and wait for store changes. Your AC could set flag @acdlite I don't mind about dispatcher "fire and forget" if middleware could change that behaviour. As I see middleware already in master so I'm satisfied. |
Is this solved my middleware in 0.10? Can we close? |
I didn't updated to 0.10 yet, so I don't know. |
@vslinko
What should
redux.dispatch()
return? Currently the code isbut I don't remember the circumstances when the return value of the callback form can be useful.
Because the callback form may schedule additional
dispatch
es, it seems that the calling code still doesn't have enough information. Can we just return whateveraction
was passed??
The text was updated successfully, but these errors were encountered: