{"size": 1458, "ext": "js", "max_stars_repo_path": "examples/with-redux-observable/redux/epics.js", "max_stars_repo_name": "scaasic/next.js", "max_stars_repo_stars_event_min_datetime": "2018-03-25T20:40:51.000Z", "max_stars_repo_stars_event_max_datetime": "2021-02-03T14:18:45.000Z", "max_issues_repo_path": "examples/with-redux-observable/redux/epics.js", "max_issues_repo_name": "scaasic/next.js", "max_issues_repo_issues_event_min_datetime": "2017-12-20T00:36:10.000Z", "max_issues_repo_issues_event_max_datetime": "2018-03-22T13:15:37.000Z", "max_forks_repo_path": "examples/with-redux-observable/redux/epics.js", "max_forks_repo_name": "scaasic/next.js", "max_forks_repo_forks_event_min_datetime": "2018-08-17T17:52:29.000Z", "max_forks_repo_forks_event_max_datetime": "2019-01-07T07:02:10.000Z"}
{"max_stars_count": 7, "max_issues_count": 1, "max_forks_count": 2, "avg_line_length": 27.0, "max_line_length": 96, "alphanum_fraction": 0.6104252401}
import { interval } from 'rxjs/observable/interval'
import { of } from 'rxjs/observable/of'
import { takeUntil, mergeMap, catchError, map } 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 =>
          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(
        map(response =>
          actions.fetchCharacterSuccess(
            response.body,
            store.getState().isServer
          )
        ),
        catchError(error =>
          of(
            actions.fetchCharacterFailure(
              error.response.body,
              store.getState().isServer
            )
          )
        )
      )
    )
  )

export const rootEpic = combineEpics(
  fetchUserEpic,
  fetchCharacterEpic
)
