Skip to content

Instantly share code, notes, and snippets.

@danielyogel
Last active December 10, 2016 22:40
Show Gist options
  • Save danielyogel/3859a3ab4892f17831b7e2635e5e65a8 to your computer and use it in GitHub Desktop.
Save danielyogel/3859a3ab4892f17831b7e2635e5e65a8 to your computer and use it in GitHub Desktop.
How whould you translate the following "epic" or "saga" to MobX? Just interested...
//Epic
export default function searchUsers(action$) {
return action$.ofType(ActionTypes.SEARCHED_USERS)
.map(action => action.payload.query)
.filter(q => !!q)
.switchMap(q =>
Observable.timer(800) // debounce
.takeUntil(action$.ofType(ActionTypes.CLEARED_SEARCH_RESULTS))
.mergeMap(() => Observable.merge(
Observable.of(replace(`?q=${q}`)),
ajax.getJSON(`https://api.github.com/search/users?q=${q}`)
.map(res => res.items)
.map(receiveUsers)
))
);
};
//SAGA
function* watchAuth() {
while(true) {
try {
const {name, password} = yield take(LOGIN_REQUEST)
yield race([
take(LOGOUT),
call(authAndRefreshTokenOnExpiry, name, password)
])
// user logged out, next while iteration will wait for the
// next LOGIN_REQUEST action
} catch(error) {
yield put( login.error(error) )
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment