From: Eduardo San Martin Morote Date: Fri, 1 May 2020 09:35:07 +0000 (+0200) Subject: feat(router): go, back and forward can be awaited X-Git-Tag: v4.0.0-alpha.10~25 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eb87757ed189958c8c9955a10ece9306fa99f6d8;p=thirdparty%2Fvuejs%2Frouter.git feat(router): go, back and forward can be awaited --- diff --git a/__tests__/router.spec.ts b/__tests__/router.spec.ts index 11287091..1d6c2a8c 100644 --- a/__tests__/router.spec.ts +++ b/__tests__/router.spec.ts @@ -176,6 +176,23 @@ describe('Router', () => { expect(router.currentRoute.value).not.toBe(START_LOCATION_NORMALIZED) }) + it('can await router.go', async () => { + const { router } = await newRouter() + await router.push('/foo') + let currentRoute = router.currentRoute.value + const [p1, r1] = fakePromise() + router.beforeEach(async (to, from, next) => { + await p1 + next() + }) + let p = router.go(-1) + expect(router.currentRoute.value).toBe(currentRoute) + r1() + // resolves to undefined as a working navigation + await expect(p).resolves.toBe(undefined) + expect(router.currentRoute.value).not.toBe(currentRoute) + }) + it('can pass replace option to push', async () => { const { router, history } = await newRouter() jest.spyOn(history, 'replace') diff --git a/src/router.ts b/src/router.ts index 4cabdfb4..2795a42b 100644 --- a/src/router.ts +++ b/src/router.ts @@ -139,13 +139,11 @@ export interface Router { resolve(to: RouteLocationRaw): RouteLocation & { href: string } - push(to: RouteLocationRaw): Promise - replace(to: RouteLocationRaw): Promise - // TODO: return a promise when https://github.com/vuejs/rfcs/pull/150 is - // merged - back(): void - forward(): void - go(delta: number): void + push(to: RouteLocationRaw): Promise + replace(to: RouteLocationRaw): Promise + back(): Promise + forward(): Promise + go(delta: number): Promise beforeEach(guard: NavigationGuardWithThis): () => void beforeResolve(guard: NavigationGuardWithThis): () => void @@ -313,7 +311,7 @@ export function createRouter(options: RouterOptions): Router { function pushWithRedirect( to: RouteLocationRaw | RouteLocation, redirectedFrom?: RouteLocation - ): Promise { + ): Promise { const targetLocation: RouteLocation = (pendingLocation = resolve(to)) const from = currentRoute.value const data: HistoryState | undefined = (to as RouteLocationOptions).state @@ -346,7 +344,7 @@ export function createRouter(options: RouterOptions): Router { const toLocation = targetLocation as RouteLocationNormalized toLocation.redirectedFrom = redirectedFrom - let failure: NavigationFailure | void + let failure: NavigationFailure | void | undefined if (!force && isSameRouteLocation(from, targetLocation)) failure = createRouterError( @@ -629,7 +627,7 @@ export function createRouter(options: RouterOptions): Router { (error as NavigationRedirectError).to, toLocation ).catch(() => { - // TODO: in dev show warning, in prod noop, same as initial navigation + // TODO: in dev show warning, in prod triggerError, same as initial navigation }) // avoid the then branch return Promise.reject() @@ -659,7 +657,7 @@ export function createRouter(options: RouterOptions): Router { ) }) .catch(() => { - // TODO: same as above: in dev show warning, in prod noop, same as initial navigation + // TODO: same as above }) }) @@ -724,6 +722,25 @@ export function createRouter(options: RouterOptions): Router { .then(position => position && scrollToPosition(position)) } + function go(delta: number) { + return new Promise( + (resolve, reject) => { + let removeError = errorHandlers.add(err => { + removeError() + removeAfterEach() + reject(err) + }) + let removeAfterEach = afterGuards.add((_to, _from, failure) => { + removeError() + removeAfterEach() + resolve(failure) + }) + + routerHistory.go(delta) + } + ) + } + const router: Router = { currentRoute, @@ -736,9 +753,9 @@ export function createRouter(options: RouterOptions): Router { push, replace, - go: routerHistory.go, - back: () => routerHistory.go(-1), - forward: () => routerHistory.go(1), + go, + back: () => go(-1), + forward: () => go(1), beforeEach: beforeGuards.add, beforeResolve: beforeResolveGuards.add,