My team is currently maintaining a stateful node server that uses rxjs. We would like to use Redux in the project to clean up the logic. I have found in several places that you can use rxjs to achieve the same result.
However they all seem to address only using one reducer and we would like to use a Redux middleware that we have. We found rxdx but its a little hard to follow, would be better to have a condensed version that fits in one file.
So my question is can someone share or point me to a small rxjs example with support for adding redux middleware?
FROM: http://rudiyardley.com/redux-single-line-of-code-rxjs/
const Subject = Rx.Subject;
// create our stream as a subject so arbitrary data can be sent on the stream
const action$ = new Subject();
// Initial State
const initState = { name: 'Harry' };
// Redux reducer
const reducer = (state, action) => {
switch(action.type) {
case 'NAME_CHANGED':
return {
...state,
name: action.payload
};
default:
return state;
}
}
// Reduxification
const store$ = action$
.startWith(initState)
.scan(reducer);
// Higher order function to send actions to the stream
const actionDispatcher = (func) => (...args) =>
action$.next(func(...args));
// Example action function
const changeName = actionDispatcher((payload) => ({
type: 'NAME_CHANGED',
payload
}));
store$.subscribe((state) => console.log(state));
changeName('Ben')
changeName('Sally')
I was trying..
const customMiddleWare = store => next => action => {
console.log("Current state:",store.getState());
console.log("Middleware triggered:", action);
next(action);
}
store$.subscribe((state) => lastState = state);
let lastState = initState;
const store = {
getState: () => lastState,
dispatch: action$.next
}
customMiddleWare(store)
But I can't workout how to wire it into the rx store? What should next & action be in the context of the middleware?
Thanks for your help on this
from Easy to follow redux core using rxjs that support middleware?
No comments:
Post a Comment