Last active
December 10, 2016 22:40
-
-
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...
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
//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