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

perf: use getter instead of computed for route location #1916

Merged
merged 2 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 8 additions & 12 deletions packages/router/__tests__/mount.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { reactive, nextTick, ComputedRef, computed, shallowRef } from 'vue'
import { nextTick, shallowRef, shallowReactive } from 'vue'
import { RouteLocationNormalizedLoose } from './utils'
import {
routeLocationKey,
Expand All @@ -9,12 +9,6 @@ import { RouteLocationNormalized } from '../src'
export function createMockedRoute(
initialValue: RouteLocationNormalizedLoose | RouteLocationNormalized
) {
const route = {} as {
[k in keyof RouteLocationNormalizedLoose]: ComputedRef<
RouteLocationNormalizedLoose[k]
>
}

const routeRef = shallowRef<
RouteLocationNormalized | RouteLocationNormalizedLoose
>(initialValue)
Expand All @@ -26,14 +20,16 @@ export function createMockedRoute(
return nextTick()
}

const route = {} as RouteLocationNormalizedLoose

for (let key in initialValue) {
// @ts-expect-error
route[key] =
// new line to still get errors here
computed(() => routeRef.value[key as keyof RouteLocationNormalizedLoose])
Object.defineProperty(route, key, {
enumerable: true,
get: () => routeRef.value[key as keyof RouteLocationNormalizedLoose],
})
}

const value = reactive(route)
const value = shallowReactive(route)

return {
value,
Expand Down
25 changes: 7 additions & 18 deletions packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,7 @@ import {
stringifyQuery as originalStringifyQuery,
LocationQuery,
} from './query'
import {
shallowRef,
Ref,
nextTick,
App,
ComputedRef,
reactive,
unref,
computed,
} from 'vue'
import { shallowRef, Ref, nextTick, App, unref, shallowReactive } from 'vue'
import { RouteRecord, RouteRecordNormalized } from './matcher/types'
import {
parseURL,
Expand Down Expand Up @@ -1235,18 +1226,16 @@ export function createRouter(options: RouterOptions): Router {
})
}

const reactiveRoute = {} as {
[k in keyof RouteLocationNormalizedLoaded]: ComputedRef<
RouteLocationNormalizedLoaded[k]
>
}
const reactiveRoute = {} as RouteLocationNormalizedLoaded
for (const key in START_LOCATION_NORMALIZED) {
// @ts-expect-error: the key matches
reactiveRoute[key] = computed(() => currentRoute.value[key])
Object.defineProperty(reactiveRoute, key, {
get: () => currentRoute.value[key as keyof RouteLocationNormalized],
enumerable: true,
})
}

app.provide(routerKey, router)
app.provide(routeLocationKey, reactive(reactiveRoute))
app.provide(routeLocationKey, shallowReactive(reactiveRoute))
app.provide(routerViewLocationKey, currentRoute)

const unmountApp = app.unmount
Expand Down