From: Eduardo San Martin Morote Date: Fri, 11 Sep 2020 16:40:30 +0000 (+0200) Subject: chore: add sample go to playground X-Git-Tag: v4.0.0-beta.10~22 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f39928c5508224ee99a8b880b935b19830c57c51;p=thirdparty%2Fvuejs%2Frouter.git chore: add sample go to playground --- diff --git a/playground/router.ts b/playground/router.ts index 6e6e7138..9a2a32b7 100644 --- a/playground/router.ts +++ b/playground/router.ts @@ -207,6 +207,44 @@ router.afterEach((to, from) => { // ) }) +export function go(delta: number) { + return new Promise((resolve, reject) => { + function popStateListener() { + clearTimeout(timeout) + } + window.addEventListener('popstate', popStateListener) + + function clearHooks() { + removeAfterEach() + removeOnError() + window.removeEventListener('popstate', popStateListener) + } + + // if the popstate event is not called, consider this a failure + const timeout = setTimeout(() => { + clearHooks() + reject(new Error('Failed to use router.go()')) + // using 0 leads to false positives + }, 1) + + setImmediate + + const removeAfterEach = router.afterEach((_to, _from, failure) => { + clearHooks() + resolve(failure) + }) + const removeOnError = router.onError(err => { + clearHooks() + reject(err) + }) + + router.go(delta) + }) +} + +// @ts-ignore +window._go = go + router.beforeEach((to, from, next) => { // console.log('second guard') if (to.query.to) next(to.query.to as string)