Skip to content

Commit 14db2aa

Browse files
committed
refactor: remove unnecessary frees
1 parent aad8c73 commit 14db2aa

File tree

5 files changed

+38
-40
lines changed

5 files changed

+38
-40
lines changed

e2e/guards-instances/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ const Foo = defineComponent({
6666

6767
setup() {
6868
onBeforeRouteUpdate((to, from) => {
69-
console.log(`setup update ${from.path} - ${to.path}`)
69+
logs.value.push(`setup:update ${from.path} - ${to.path}`)
7070
})
7171
onBeforeRouteLeave((to, from) => {
72-
console.log(`setup leave ${from.path} - ${to.path}`)
72+
logs.value.push(`setup:leave ${from.path} - ${to.path}`)
7373
})
7474
return {}
7575
},

e2e/specs/guards-instances.js

+19
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ function testCase(browser) {
1717
[
1818
'enter / - /foo',
1919
'leave /foo - /f/1',
20+
'setup:leave /foo - /f/1',
2021
'enter /foo - /f/1',
2122
'update /f/1 - /f/2',
23+
'setup:update /f/1 - /f/2',
2224
'update /f/2 - /f/2',
25+
'setup:update /f/2 - /f/2',
2326
'leave /f/2 - /foo',
27+
'setup:leave /f/2 - /foo',
2428
'enter /f/2 - /foo',
2529
].join('\n')
2630
)
@@ -70,6 +74,21 @@ module.exports = {
7074
.assert.containsText('.view', 'foo 4')
7175
.click('li:nth-child(3) a')
7276
.assert.containsText('.view', 'foo 2')
77+
// leave the update view and enter it again
78+
.click('li:nth-child(1) a')
79+
.click('li:nth-child(3) a')
80+
.click('#resetLogs')
81+
.click('li:nth-child(4) a')
82+
.click('li:nth-child(1) a')
83+
.assert.containsText(
84+
'#logs',
85+
[
86+
'update /f/1 - /f/2',
87+
'setup:update /f/1 - /f/2',
88+
'leave /f/2 - /',
89+
'setup:leave /f/2 - /',
90+
].join('\n')
91+
)
7392

7493
browser.end()
7594
},

src/RouterView.ts

-19
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,6 @@ export const RouterViewImpl = defineComponent({
5555

5656
const viewRef = ref<ComponentPublicInstance>()
5757

58-
// when the same component is used in different routes, the onVnodeMounted
59-
// hook doesn't trigger, so we need to observe the changing route to update
60-
// the instance on the record
61-
// watch(matchedRouteRef, to => {
62-
// const currentName = props.name
63-
// // to can be null if there isn't a matched route, e.g. not found
64-
// if (to && !to.instances[currentName]) {
65-
// to.instances[currentName] = viewRef.value
66-
// // trigger enter callbacks when different routes only
67-
// if (viewRef.value) {
68-
// ;(to.enterCallbacks[currentName] || []).forEach(callback =>
69-
// callback(viewRef.value!)
70-
// )
71-
// // avoid double calls since watch is called before the onVnodeMounted
72-
// to.enterCallbacks[currentName] = []
73-
// }
74-
// }
75-
// })
76-
7758
return () => {
7859
const route = props.route || injectedRoute
7960
const matchedRoute = matchedRouteRef.value

src/navigationGuards.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
NavigationFailure,
1818
NavigationRedirectError,
1919
} from './errors'
20-
import { ComponentOptions } from 'vue'
20+
import { ComponentOptions, onUnmounted } from 'vue'
2121
import { inject, getCurrentInstance, warn } from 'vue'
2222
import { matchedRouteKey } from './injectionSymbols'
2323
import { RouteRecordNormalized } from './matcher/types'
@@ -49,10 +49,14 @@ export function onBeforeRouteLeave(leaveGuard: NavigationGuard) {
4949
return
5050
}
5151

52-
activeRecord.leaveGuards.push(
53-
// @ts-ignore do we even want to allow that? Passing the context in a composition api hook doesn't make sense
54-
leaveGuard.bind(instance.proxy)
55-
)
52+
// @ts-ignore do we even want to allow that? Passing the context in a composition api hook doesn't make sense
53+
const fn = leaveGuard.bind(instance.proxy)
54+
onUnmounted(() => {
55+
const index = activeRecord.leaveGuards.indexOf(fn)
56+
if (index > -1) activeRecord.leaveGuards.splice(index, 1)
57+
})
58+
59+
activeRecord.leaveGuards.push(fn)
5660
}
5761

5862
/**
@@ -81,10 +85,14 @@ export function onBeforeRouteUpdate(updateGuard: NavigationGuard) {
8185
return
8286
}
8387

84-
activeRecord.updateGuards.push(
85-
// @ts-ignore do we even want to allow that? Passing the context in a composition api hook doesn't make sense
86-
updateGuard.bind(instance.proxy)
87-
)
88+
// @ts-ignore do we even want to allow that? Passing the context in a composition api hook doesn't make sense
89+
const fn = updateGuard.bind(instance.proxy)
90+
onUnmounted(() => {
91+
const index = activeRecord.updateGuards.indexOf(fn)
92+
if (index > -1) activeRecord.updateGuards.splice(index, 1)
93+
})
94+
95+
activeRecord.updateGuards.push(fn)
8896
}
8997

9098
export function guardToPromiseFn(

src/router.ts

-10
Original file line numberDiff line numberDiff line change
@@ -709,16 +709,6 @@ export function createRouter(options: RouterOptions): Router {
709709
const error = checkCanceledNavigation(toLocation, from)
710710
if (error) return error
711711

712-
const [leavingRecords] = extractChangingRecords(toLocation, from)
713-
for (const record of leavingRecords) {
714-
// remove registered guards from removed matched records
715-
record.leaveGuards = []
716-
record.updateGuards = []
717-
// free the references
718-
record.instances = {}
719-
record.enterCallbacks = {}
720-
}
721-
722712
// only consider as push if it's not the first navigation
723713
const isFirstNavigation = from === START_LOCATION_NORMALIZED
724714
const state = !isBrowser ? {} : history.state

0 commit comments

Comments
 (0)