灵感來自 redux-observable,但在使用過程中感覺很不順暢,為什麼要 action 入 action 出呢?這樣用起來感覺很不順暢呀,有時間我並不想在這個流的末端再觸發別的 action 了,於是便自己魔改了一版:
示例代碼#
const streamerMiddleware = createStreamMiddleware()
const store = createStore(reducers, applyMiddleware(streamerMiddleware))
streamerMiddleware.run(
combineStreamers((observable$, api) =>
observable$
.pipe(
filterAction(FetchTypes.FETCH_START),
throttleTime(500),
dispatchAction({ type: FetchTypes.SET_LOADING, payload: true }),
delay(1000),
mergeMap(() => from(fetcher().then((response) => response.json()))),
)
.subscribe({
next: (result) => {
if (result) {
api.dispatch({ type: FetchTypes.FETCH_DONE, payload: result })
}
},
error: (err) => {
console.log(err)
api.dispatch({ type: FetchTypes.FETCH_FAILED, payload: err })
},
})
),
store.dispatch
)
github: redux-observable-another