Last active
May 13, 2018 16:16
-
-
Save jamestthompson3/05a20ca0596d5ff675d6b5db4a37351a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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