Last active
January 18, 2017 11:58
-
-
Save hasangilak/bf5c4216283517ed10aa65d2fca1f0fb 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 {sync} from "./sync"; | |
import {handleError} from "./catchError"; | |
export function raceOnTime(generator, timeOut) { | |
return (new Promise((resolve, reject) => { | |
let _resolve; | |
const timer = setTimeout(() => { | |
reject({error: new Error('timeout')}); | |
}, timeOut); | |
const done = () => { | |
_resolve(); | |
resolve({success: 'ok'}); | |
clearTimeout(timer); | |
}; | |
new Promise((resolve) => { | |
_resolve = resolve; | |
sync(function*() { | |
try { | |
yield* generator(done); | |
} catch (error) { | |
handleError(error); | |
} | |
}); | |
}).then(); | |
})); | |
} | |
// example | |
function* categoryListController(done) { | |
loading(true); | |
const {error, data} = yield (new swagger.CategoryApi()) | |
.categoryListGet(select('user.token'), { | |
...select('queries.channel', {}), | |
def: true | |
}); | |
done(); | |
if (!error) { | |
dispatch(categoryListAction(data)); | |
loading(false); | |
} else { | |
throwError("onCategoryEnterMiddleWare", function () { | |
navigate('/v1/login'); | |
}); | |
} | |
} | |
export default (nextState, replace, next) => sync(function*() { | |
try { | |
yield* isLoginMiddleware(); | |
let {error} = yield raceOnTime(categoryListController, 20000); | |
if (error) | |
navigate('/v1/login'); | |
next(); | |
} catch (error) { | |
handleError(error); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment