1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/with-redux-observable/redux/epics.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

import { interval } from 'rxjs/observable/interval'
import { of } from 'rxjs/observable/of'
import { takeUntil, mergeMap, catchError } from 'rxjs/operators'
import { combineEpics, ofType } from 'redux-observable'
import ajax from 'universal-rx-request' // because standard AjaxObservable only works in browser
import * as actions from './actions'
import * as types from './actionTypes'
export const fetchUserEpic = (action$, store) =>
action$.pipe(
ofType(types.START_FETCHING_CHARACTERS),
mergeMap(action => {
return interval(3000).pipe(
mergeMap(x =>
of(
actions.fetchCharacter({
isServer: store.getState().isServer
})
)
),
takeUntil(action$.ofType(types.STOP_FETCHING_CHARACTERS))
)
})
)
export const fetchCharacterEpic = (action$, store) =>
action$.pipe(
ofType(types.FETCH_CHARACTER),
mergeMap(action =>
ajax({
url: `https://swapi.co/api/people/${store.getState().nextCharacterId}`
}).pipe(
mergeMap(response =>
of(
actions.fetchCharacterSuccess(
response.body,
store.getState().isServer
)
)
),
catchError(error =>
of(
actions.fetchCharacterFailure(
error.response.body,
store.getState().isServer
)
)
)
)
)
)
export const rootEpic = combineEpics(
fetchUserEpic,
fetchCharacterEpic
)