-
Notifications
You must be signed in to change notification settings - Fork 107
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
Problem with dispatchMultiple #91
Comments
While I was reviewing other topics in the issue section. I saw a similar one: #18. The resolution made me believe that redux-logic should function for multiple dispatching. In my case, I have a axios api call along with a normal action. Does it make any difference? |
More information about my environment: "rxjs": "5.5.6", |
More information on the redux-logic internal monitoring: 09:18:06.560 configureStore.ts:23 Object {action: Object, op: "top"} Also, from network tab in chrome dev tools, i can see the XHR API call to my server was fulfilled with data. |
More observations: if i remove latest: true, both dispatches worked in sequential manner as expected. |
I am trying out this option. Typescript source code is attached below:
=======================
import { createAction, handleActions } from 'redux-actions'
import { createLogic } from 'redux-logic';
import { Observable } from 'rxjs'
// -----------------
// STATE - This defines the type of data maintained in the Redux store.
export interface WeatherForecastsState {
isLoading: boolean;
startDateIndex?: number;
forecasts: WeatherForecast[];
}
export interface WeatherForecast {
dateFormatted: string;
temperatureC: number;
temperatureF: number;
summary: string;
}
// -----------------
// ACTIONS - These are serializable (hence replayable) descriptions of state transitions.
// They do not themselves have any side-effects; they just describe something that is going to happen.
enum WeatherForecastsActions {
WEATHERLOGIC_MAIN = 'WEATHERLOGIC_MAIN',
REQUEST_WEATHER_FORECASTS = 'REQUEST_WEATHER_FORECASTS',
RECEIVE_WEATHER_FORECASTS = 'RECEIVE_WEATHER_FORECASTS',
RECEIVE_FAILED = 'RECEIVE_FAILED'
}
//
const fetchFulfilled = createAction(WeatherForecastsActions.RECEIVE_WEATHER_FORECASTS);
const fetchRejected = (params) => ({
type: WeatherForecastsActions.RECEIVE_FAILED,
payload: params,
error: true
});
const ReqStart = createAction(WeatherForecastsActions.REQUEST_WEATHER_FORECASTS);
const ReqMain = createAction(WeatherForecastsActions.WEATHERLOGIC_MAIN);
export const actionCreators = { ReqMain };
// ----------------
export const weatherLogic = createLogic({
//
type: WeatherForecastsActions.WEATHERLOGIC_MAIN,
latest: true,
warnTimeout: 0,
//
processOptions: {
dispatchMultiple: true // turn on multiple dispatch mode
},
//
process({ httpClient, action, getState, store}, dispatch, done) {
//
let startDateIndex: number = action.payload;
//
if (startDateIndex !== getState().weatherForecasts.startDateIndex) {
//dispatch(ReqStart(startDateIndex));
const ob1$ = Observable.of(ReqStart(startDateIndex));
dispatch(ob1$);
});
// Note:
// object that is returned will be dispatched to the respective action creator function which takes appropriate parameters
//export const weatherLogic = createLogic({
// //
// type: WeatherForecastsActions.WEATHERLOGIC_MAIN,
// latest: true,
// //
// processOptions: {
// dispatchReturn: true,
// successType: fetchFulfilled,
// failType: fetchRejected
// },
// //
// async process({ httpClient, action, getState }, dispatch) {
// let startDateIndex: number = action.payload;
// if (startDateIndex !== getState().weatherForecasts.startDateIndex) {
// const data = await httpClient.get(
api/SampleData/WeatherForecasts?startDateIndex=${startDateIndex}
)// .then(resp => resp.data);
// return { data: data, startDateIndex: startDateIndex };
// }
// }
//});
// ----------------
export const reducer = handleActions(
{
[WeatherForecastsActions.REQUEST_WEATHER_FORECASTS]: (state, action) =>
{
return {
startDateIndex: action.payload,
forecasts: state.forecasts,
isLoading: true
};
},
====================
In the process method of weatherLogic, i have two observables: one for a normal action and the other one is for axios API call. The ReqStart action invokes reducer to set loading flag to true. Then the axios API call fetches data from server. The strange thing is that I can dispatch either independently and it works. But if i dispatch both one after another as shown in the source code above, the fetchFulfilled action seems never dispatched to reducer to display the list. I am at a loss what the cause would be.
Thanks for your time.
The text was updated successfully, but these errors were encountered: