Skip to content

Commit 2d2356c

Browse files
committed
wip: better queue generation mechanism
1 parent 95558dc commit 2d2356c

File tree

3 files changed

+32
-44
lines changed

3 files changed

+32
-44
lines changed

src/directives/view.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ export default function (Vue) {
3333
// finally, init by delegating to v-component
3434
componentDef.bind.call(this)
3535

36-
// all we need to do here is registering this view
37-
// in the router. actual component switching will be
38-
// managed by the pipeline.
39-
let router = this.router = route.router
40-
router._views.unshift(this)
41-
4236
// locate the parent view
4337
let parentView
4438
let parent = this.vm
@@ -57,6 +51,8 @@ export default function (Vue) {
5751
this.parentView = parentView
5852
parentView.childView = this
5953
} else {
54+
// this is the root view!
55+
let router = route.router
6056
router._rootView = this
6157
}
6258

@@ -78,7 +74,6 @@ export default function (Vue) {
7874
if (this.parentView) {
7975
this.parentView.childView = null
8076
}
81-
this.router._views.$remove(this)
8277
componentDef.unbind.call(this)
8378
}
8479
})

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ class Router {
4747

4848
// Vue instances
4949
this.app = null
50-
this._views = []
5150
this._children = []
5251

5352
// route recognizer

src/transition.js

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,6 @@ export default class RouteTransition {
2323
this.next = null
2424
this.aborted = false
2525
this.done = false
26-
27-
// start by determine the queues
28-
29-
// the deactivate queue is an array of router-view
30-
// directive instances that need to be deactivated,
31-
// deepest first.
32-
let view = router._rootView
33-
this.deactivateQueue = router._views = []
34-
while (view) {
35-
this.deactivateQueue.unshift(view)
36-
view = view.childView
37-
}
38-
39-
// check the default handler of the deepest match
40-
let matched = to.matched
41-
? Array.prototype.slice.call(to.matched)
42-
: []
43-
44-
// the activate queue is an array of route handlers
45-
// that need to be activated
46-
this.activateQueue = matched.map(match => match.handler)
4726
}
4827

4928
/**
@@ -114,28 +93,37 @@ export default class RouteTransition {
11493

11594
start (cb) {
11695
let transition = this
117-
let daq = this.deactivateQueue
118-
let aq = this.activateQueue
119-
let rdaq = daq.slice().reverse()
120-
let reuseQueue
96+
97+
// determine the queue of views to deactivate
98+
let deactivateQueue = []
99+
let view = this.router._rootView
100+
while (view) {
101+
deactivateQueue.unshift(view)
102+
view = view.childView
103+
}
104+
let reverseDeactivateQueue = deactivateQueue.slice().reverse()
105+
106+
// determine the queue of route handlers to activate
107+
let activateQueue = this.activateQueue =
108+
toArray(this.to.matched).map(match => match.handler)
121109

122110
// 1. Reusability phase
123-
let i
124-
for (i = 0; i < rdaq.length; i++) {
125-
if (!canReuse(rdaq[i], aq[i], transition)) {
111+
let i, reuseQueue
112+
for (i = 0; i < reverseDeactivateQueue.length; i++) {
113+
if (!canReuse(reverseDeactivateQueue[i], activateQueue[i], transition)) {
126114
break
127115
}
128116
}
129117
if (i > 0) {
130-
reuseQueue = rdaq.slice(0, i)
131-
daq = rdaq.slice(i).reverse()
132-
aq = aq.slice(i)
118+
reuseQueue = reverseDeactivateQueue.slice(0, i)
119+
deactivateQueue = reverseDeactivateQueue.slice(i).reverse()
120+
activateQueue = activateQueue.slice(i)
133121
}
134122

135123
// 2. Validation phase
136-
transition.runQueue(daq, canDeactivate, () => {
137-
transition.runQueue(aq, canActivate, () => {
138-
transition.runQueue(daq, deactivate, () => {
124+
transition.runQueue(deactivateQueue, canDeactivate, () => {
125+
transition.runQueue(activateQueue, canActivate, () => {
126+
transition.runQueue(deactivateQueue, deactivate, () => {
139127
// 3. Activation phase
140128

141129
// Update router current route
@@ -146,8 +134,8 @@ export default class RouteTransition {
146134

147135
// the root of the chain that needs to be replaced
148136
// is the top-most non-reusable view.
149-
if (daq.length) {
150-
let view = daq[daq.length - 1]
137+
if (deactivateQueue.length) {
138+
let view = deactivateQueue[deactivateQueue.length - 1]
151139
let depth = reuseQueue ? reuseQueue.length : 0
152140
activate(view, transition, depth, cb)
153141
} else {
@@ -312,3 +300,9 @@ export default class RouteTransition {
312300
function isPlainOjbect (val) {
313301
return Object.prototype.toString.call(val) === '[object Object]'
314302
}
303+
304+
function toArray (val) {
305+
return val
306+
? Array.prototype.slice.call(val)
307+
: []
308+
}

0 commit comments

Comments
 (0)