Skip to content

Commit

Permalink
feat: allow true in next
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Mar 17, 2020
1 parent 5dce7bc commit d76c6aa
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
7 changes: 5 additions & 2 deletions __tests__/guards/component-beforeRouteEnter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ describe('beforeRouteEnter', () => {
expect(router.currentRoute.value.fullPath).toBe('/foo')
})

// not implemented yet as it depends on Vue 3 Suspense
it.skip('calls next callback', done => {
// TODO:
it.skip('calls next callback', async done => {
const router = createRouter({ routes })
beforeRouteEnter.mockImplementationOnce((to, from, next) => {
next(vm => {
Expand All @@ -225,6 +225,9 @@ describe('beforeRouteEnter', () => {
done()
})
})

await router.push('/')
await router.push('/guard/2')
})

it.skip('calls next callback after waiting', async done => {
Expand Down
8 changes: 8 additions & 0 deletions playground/views/User.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,13 @@ export default defineComponent({
props: {
id: String,
},
beforeRouteUpdate(to, from, next) {
// TODO: these do not work yet
console.log('this', this)
next(vm => {
console.log('in next', vm)
})
},
})
</script>
4 changes: 3 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,12 @@ export interface MatcherLocationRedirect {
export interface NavigationGuardCallback {
(): void
(location: RouteLocation): void
(valid: false): void
(valid: boolean): void
(cb: (vm: any) => void): void
}

export type NavigationGuardNextCallback = (vm: any) => any

export interface NavigationGuard<V = void> {
(
this: V,
Expand Down
16 changes: 12 additions & 4 deletions src/utils/guardToPromiseFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
RouteLocationNormalized,
NavigationGuardCallback,
RouteLocation,
RouteLocationNormalizedResolved,
NavigationGuardNextCallback,
} from '../types'

import { isRouteLocation } from '../types'
Expand All @@ -11,18 +13,24 @@ import { NavigationGuardRedirect, NavigationAborted } from '../errors'
export function guardToPromiseFn(
guard: NavigationGuard,
to: RouteLocationNormalized,
from: RouteLocationNormalized
from: RouteLocationNormalizedResolved
// record?: RouteRecordNormalized
): () => Promise<void> {
return () =>
new Promise((resolve, reject) => {
const next: NavigationGuardCallback = (
valid?: boolean | RouteLocation
valid?: boolean | RouteLocation | NavigationGuardNextCallback
) => {
// TODO: handle callback
if (valid === false) reject(new NavigationAborted(to, from))
else if (isRouteLocation(valid)) {
reject(new NavigationGuardRedirect(to, valid))
} else resolve()
} else if (!valid || valid === true) {
resolve()
} else {
// TODO: call the in component enter callbacks. Maybe somewhere else
// record && record.enterCallbacks.push(valid)
resolve()
}
}

guard(to, from, next)
Expand Down
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
RouteParams,
Immutable,
RouteComponent,
RouteLocationNormalizedResolved,
} from '../types'
import { guardToPromiseFn } from './guardToPromiseFn'
import { RouteRecordNormalized } from '../matcher/types'
Expand All @@ -23,7 +24,7 @@ export function extractComponentsGuards(
matched: RouteRecordNormalized[],
guardType: GuardType,
to: RouteLocationNormalized,
from: RouteLocationNormalized
from: RouteLocationNormalizedResolved
) {
const guards: Array<() => Promise<void>> = []

Expand Down

0 comments on commit d76c6aa

Please sign in to comment.