Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes or rejection route type guard and types in hook runner methods #261

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/services/createCurrentRoute.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { reactive } from 'vue'
import { createRouterRoute } from '@/services/createRouterRoute'
import { isRejectionRouteSymbol } from '@/services/isRejectionRoute'
import { RouterRoutes } from '@/types'
import { ResolvedRoute } from '@/types/resolved'
import { Routes } from '@/types/route'
Expand All @@ -18,7 +19,7 @@ export function createCurrentRoute(fallbackRoute: ResolvedRoute, push: RouterPus
const route = reactive({ ...fallbackRoute })

const updateRoute: ResolvedRouteUpdate = (newRoute) => {
Object.assign(route, { ...newRoute })
Object.assign(route, { [isRejectionRouteSymbol]: false, ...newRoute })
stackoverfloweth marked this conversation as resolved.
Show resolved Hide resolved
}

const currentRoute = route
Expand Down
9 changes: 1 addition & 8 deletions src/services/createRouterReject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Ref, markRaw, ref, Component } from 'vue'
import { genericRejection } from '@/components/rejection'
import { createResolvedRouteQuery } from '@/services/createResolvedRouteQuery'
import { isRejectionRouteSymbol } from '@/services/isRejectionRoute'
import { RegisteredRejectionType } from '@/types/register'
import { ResolvedRoute } from '@/types/resolved'

Expand All @@ -10,21 +11,18 @@ export type BuiltInRejectionType = typeof builtInRejections[number]
export type RouterSetReject = (type: RegisteredRejectionType | null) => void

type GetRejectionRoute = (type: RegisteredRejectionType) => ResolvedRoute
type IsRejectionRoute = (route: ResolvedRoute) => boolean

export type RouterRejection = Ref<null | { type: RegisteredRejectionType, component: Component }>

type CreateRouterRejectContext = {
rejections?: Partial<Record<string, Component>>,
}

const isRejectionRouteSymbol = Symbol()

export type CreateRouterReject = {
setRejection: RouterSetReject,
rejection: RouterRejection,
getRejectionRoute: GetRejectionRoute,
isRejectionRoute: IsRejectionRoute,
}

export function createRouterReject({
Expand Down Expand Up @@ -62,10 +60,6 @@ export function createRouterReject({
return resolved
}

const isRejectionRoute: IsRejectionRoute = (route) => {
return isRejectionRouteSymbol in route
}

const setRejection: RouterSetReject = (type) => {
if (!type) {
rejection.value = null
Expand All @@ -83,6 +77,5 @@ export function createRouterReject({
setRejection,
rejection,
getRejectionRoute,
isRejectionRoute,
}
}
4 changes: 2 additions & 2 deletions src/services/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function createRouteHookRunners<const T extends Routes>(): RouteHookRunne
throw new NavigationAbortError()
}

async function runBeforeRouteHooks<const T extends Routes>({ to, from, hooks }: BeforeContext): Promise<BeforeRouteHookResponse<T>> {
async function runBeforeRouteHooks({ to, from, hooks }: BeforeContext): Promise<BeforeRouteHookResponse<T>> {
const { global, component } = hooks
const route = getBeforeRouteHooksFromRoutes(to, from)

Expand Down Expand Up @@ -112,7 +112,7 @@ export function createRouteHookRunners<const T extends Routes>(): RouteHookRunne
}
}

async function runAfterRouteHooks<const T extends Routes>({ to, from, hooks }: AfterContext): Promise<AfterRouteHookResponse<T>> {
async function runAfterRouteHooks({ to, from, hooks }: AfterContext): Promise<AfterRouteHookResponse<T>> {
const { global, component } = hooks
const route = getAfterRouteHooksFromRoutes(to, from)

Expand Down
7 changes: 7 additions & 0 deletions src/services/isRejectionRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ResolvedRoute } from '@/types/resolved'

export const isRejectionRouteSymbol = Symbol()

export function isRejectionRoute(route: ResolvedRoute): boolean {
return isRejectionRouteSymbol in route && !!route[isRejectionRouteSymbol]
}
Loading