You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, we are trying to create modules made of a redux reducer and/or a middleware. The modules are to be reused and distributed, mostly like Ducks does, but also supporting middlewares.
We are using redux actions and trying to do something useful asynchronously, like reading a file or something from the database, and also allow a chain of dependency where modules' middlewares/reducers can rely on the state/payload of the module it depends on, and use it to build and return a new state.
Something like:
module 1
-> dispatch action to read something async
-> query database middleware await data from database
-> run all other modules (async/sync) middlewares
-> reducer consumes data and build a new state
-> then it's passed to module 2 reducer and so on...
For doing so we are trying to run through a chain of (synchronous and asynchronous) side-effects in middlewares.
We don't want to do anything other than returning the FSA object when defining actions, so we don't restrict the data gathering logic and any other business logic only to the module that creates the action and centralize them in middlewares.
import{createStore,applyMiddleware}from'redux'import{createAction,handleActions}from'redux-actions'importpromiseMiddlewarefrom'redux-promise'constBOOT='BOOT'constAFTER_BOOT='AFTER_BOOT'constAFTER_AFTER_BOOT='AFTER_AFTER_BOOT'constsomeApi={get(id){returnPromise.resolve({name: 'yeah'})}}constbootAction=createAction(BOOT)constafterBootAction=createAction(AFTER_BOOT,async(id)=>{constresult=awaitsomeApi.get(id)returnresult})constafterAfterBootAction=createAction(AFTER_AFTER_BOOT,async(id)=>{constresult=awaitsomeApi.get(id)returnresult})constinitialState={foo: 'bar'}constreducer=(state=initialState,action)=>{switch(action.type){caseAFTER_BOOT:
return{
...state,foo: 'baz'}caseAFTER_AFTER_BOOT:
return{
...state,foo: 'rock'}default:
returnstatebreak}}constmiddlewares=[functionmiddle({getState, dispatch}){// Note the async keyword.returnnext=>async(action)=>{if(action.type==BOOT){constsideffectA=awaitdispatch(afterBootAction(2))}if(action.type==AFTER_BOOT){constsideffectB=awaitdispatch(afterAterBootAction(3))}returnnext(action)}},promiseMiddleware]conststore=createStore(reducer,initialState,applyMiddleware(...middlewares))// First action, and should fire other side-effects.store.dispatch(bootAction())console.log(store.getState(),"It should print {foo: 'rock'}")
We are doing this as part of our Choko Redux project.
The text was updated successfully, but these errors were encountered:
Hello, we are trying to create modules made of a redux reducer and/or a middleware. The modules are to be reused and distributed, mostly like Ducks does, but also supporting middlewares.
We are using redux actions and trying to do something useful asynchronously, like reading a file or something from the database, and also allow a chain of dependency where modules' middlewares/reducers can rely on the state/payload of the module it depends on, and use it to build and return a new state.
Something like:
module 1
-> dispatch action to read something async
-> query database middleware await data from database
-> run all other modules (async/sync) middlewares
-> reducer consumes data and build a new state
-> then it's passed to module 2 reducer and so on...
For doing so we are trying to run through a chain of (synchronous and asynchronous) side-effects in middlewares.
We don't want to do anything other than returning the FSA object when defining actions, so we don't restrict the data gathering logic and any other business logic only to the module that creates the action and centralize them in middlewares.
We are doing this as part of our Choko Redux project.
The text was updated successfully, but these errors were encountered: