Skip to content

Instantly share code, notes, and snippets.

@jamestthompson3
Last active May 13, 2018 16:16
Show Gist options
  • Save jamestthompson3/05a20ca0596d5ff675d6b5db4a37351a to your computer and use it in GitHub Desktop.
Save jamestthompson3/05a20ca0596d5ff675d6b5db4a37351a to your computer and use it in GitHub Desktop.
import { from , to } from 'rxjs'
import { setObservableConfig, mapPropsStream } from 'recompose'
import { isEmpty } from 'lodash'
import { switchMap, map, startWith, tap, catchError } from 'rxjs/operators'
const load = mapPropsStream(props$ =>
props$.pipe(
switchMap(
props =>
isEmpty(props.userList)
? from(fetchUsers()).pipe(
tap(users => props.setUserList(users)),
map(users => ({ ...props, users, status: 'SUCCESS' })),
startWith({ status: 'REQUEST', ...props })
)
: of(props).pipe(map(props => ({...props, status: 'SUCCESS'})))
),
catchError(() => ({ status: 'ERROR', message: 'Looks like our service is down' }))
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment