-
Notifications
You must be signed in to change notification settings - Fork 468
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
Compatibility with redux-thunk middleware #605
Comments
This issue deserves some more attention. However the code is working as described above and I can create "type safe" epics by typing its arguments and its return without using the Epic type. I just would rather a way of typing ThunkActions in the Epic generics. |
@AylanBoscarino we are also using TS now, can you share code same where you have fully type safe epics with thunks? Many thanks. |
Sure but I'm not totally sure they are completely type safe tho, hence the quotes. I'm creating epics like this: const stepsActivities = (
action$: ActionsObservable<GenericAction<ActivitiesState>>,
_state$: StateObservable<StoreState>,
_dependencies: DependableServices,
): Observable<
ThunkAction<
Promise<void>,
StoreState,
DependableServices,
GenericAction<ActivitiesState>
>
> =>
action$.pipe(
ofType(ACTIVITIES_FETCH_STEPS),
flatMap(() => [
activitiesStepsPeriod('1 day'),
activitiesStepsPeriod('1 week'),
activitiesStepsPeriod('1 month'),
]),
); The function export function activitiesStepsPeriod(
period: timePeriod,
): ThunkAction<
Promise<void>,
StoreState,
DependableServices,
GenericAction<types.ActivitiesState>
> {
return async (dispatch, getState, { activitiesService }) => {
activitiesService.getStepCount(period, steps => {
switch (period) {
case '1 day':
dispatch({
type: types.ACTIVITIES_STEP_DAILY,
payload: { dailySteps: steps },
});
case '1 week':
dispatch({
type: types.ACTIVITIES_STEP_WEEKLY,
payload: { weeklySteps: steps },
});
case '1 month':
dispatch({
type: types.ACTIVITIES_STEP_MONTHLY,
payload: { monthlySteps: steps },
});
}
});
};
} How are you creating the epics in your project? |
Created PR to fix this #780 |
I'm not sure if it's a feature a bug and it might be a usage question, but it seems that it better belongs to Github, then SO.
We have started to use
redux-observable
since version1.0.0
, so by this time all the docs mentioning compatibility with thunks have already been eradicated, and my search across Github have found only a bunch of deprecations and some very old issues. #13Our app has used
redux-thunk
middleware mostly related to API calls, so when integratingredux-observable
, we used something like this:Later on I discovered that this way dispatching thunks JUST WORKS, and after reading source code and trying to source docs I am still unsure whether this is expected behaviour or some bug (feature) being exploited accidentally.
Code looks like this:
Putting debugger inside
getDataAction
does indeed approve that thunk gets properly executed and everything works as expected.The only problem with this approach which I was able to find, that it is impossible to handle errors via
catchError
(because I assume that since it happens afterstore.dispatch
,redux-observable
does not know anything about it. Which is also not a large issue, since it's possible to create another epic if necessary to handleREQUEST_DATA_FAILURE
.So is it "idiomatically correct" to use this behaviour, or this is something that shouldn't be relied on, and may not work in future versions?
And if it's not correct is there a way to keep these two operations atomic and independently testable? For example thunk could be used independently in some other place, which relies on the fact that it's a promise, and as far as I know this is not currently possible with pure epics.
Thank you in advance!
Let me know if I should delete this issue and re-post to SO, or maybe file a PR for entry in docs.
The text was updated successfully, but these errors were encountered: