순수 Rxj로 기본 redux/redux-observable 동작을 다시 구현하는 것이 얼마나 복잡한지를 (호기심에서) 보려고합니다.RxJs에서만 관측 가능한 reduxplement?
여기 내 걸리 겠지만 옳기는 매우 간단합니다. 아무도 내 논리에서 오류/결함으로 나를 가리킬 수 있습니까?
당신에게 대단히 감사합니다// set up the store.dispatch functionnaly through a subject (action$.next() is like store.dispatch())
var action$ = new Rx.Subject()
// Create epics that do nothing interesting
function epic1(action$) {
return action$.filter(action => action.type == "test").delay(1000).mapTo({
type: "PONG"
})
}
function epic2(action$) {
return action$.filter(action => action.type == "test2").delay(2000).mapTo({
type: "PING"
})
}
//....
//Later on, Merge all epic into one observable
//
function activateAndMergeEpics(action$, ...epics) {
// give the action$ stream to each epic
var activatedArray = epics.map(epic => epic(action$))
// merge them all into one megaObservable
var merged = Rx.Observable.merge(...activatedArray)
return merged
}
var merged = activateAndMergeEpics(action$, epic1, epic2)
// Pipe your megaObservable back inside the loop so
// you can process the action in your reducers
var subscription = merged.subscribe(action$)
function rootReducer(state = {}, action) {
console.log(action)
return (state)
}
// Generate your state from your actions
var state$ = action$.scan(rootReducer, {})
// Do whatever your want now, like...
// state$.map(route).map(renderdom)
// Let's juste subscribe to nothing to get the stream pumping
state$.subscribe()
// Simulate a dispatch
action$.next({
type: "test"
})
// Another one
action$.next({type:"test2"})
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.4.3/Rx.min.js"></script>
와우, 제작자의 답변은 멋집니다. 귀하의 조언과 당신의 대답에 대해 고마워요! 나는 이미 "왜 Redux로하고 처음부터하지 않았습니까?"라고 읽었습니다. Ben Lesh 기사 : [Ben Lesh article] (https://medium.com/@benlesh/redux-observable-ec0b00d2eb52) – TheoH
여러분을 환영합니다! – jayphelps