Skip to content

Commit 846f4a2

Browse files
committed
implement progressive boot with requestIdleCallback
1 parent 68abdc3 commit 846f4a2

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"extends": "vue",
55
"plugins": ["flowtype"],
66
"globals": {
7-
"__WEEX__": true
7+
"__WEEX__": true,
8+
"requestIdleCallback": true
89
},
910
"rules": {
1011
"no-useless-escape": 0,

src/core/vdom/patch.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import { registerRef } from './modules/ref'
2020

2121
export const emptyNode = new VNode('', {}, [])
2222

23+
// used for delaying component mounting for "progressive boot"
24+
const hasRIC = typeof requestIdleCallback !== 'undefined'
25+
2326
const hooks = ['create', 'activate', 'update', 'remove', 'destroy']
2427

2528
function isUndef (s) {
@@ -87,7 +90,14 @@ export function createPatchFunction (backend) {
8790
let inPre = 0
8891
function createElm (vnode, insertedVnodeQueue, parentElm, refElm, nested) {
8992
vnode.isRootInsert = !nested // for transition enter check
90-
if (createComponent(vnode, insertedVnodeQueue, parentElm, refElm)) {
93+
if (isDef(vnode.componentOptions)) {
94+
if (hasRIC && vnode.componentOptions.Ctor.options.bootAsync) {
95+
requestIdleCallback(() => {
96+
createComponent(vnode, insertedVnodeQueue, parentElm, refElm)
97+
})
98+
} else {
99+
createComponent(vnode, insertedVnodeQueue, parentElm, refElm)
100+
}
91101
return
92102
}
93103

@@ -158,23 +168,11 @@ export function createPatchFunction (backend) {
158168
}
159169

160170
function createComponent (vnode, insertedVnodeQueue, parentElm, refElm) {
161-
let i = vnode.data
162-
if (isDef(i)) {
163-
const isReactivated = isDef(vnode.child) && i.keepAlive
164-
if (isDef(i = i.hook) && isDef(i = i.init)) {
165-
i(vnode, false /* hydrating */, parentElm, refElm)
166-
}
167-
// after calling the init hook, if the vnode is a child component
168-
// it should've created a child instance and mounted it. the child
169-
// component also has set the placeholder vnode's elm.
170-
// in that case we can just return the element and be done.
171-
if (isDef(vnode.child)) {
172-
initComponent(vnode, insertedVnodeQueue)
173-
if (isReactivated) {
174-
reactivateComponent(vnode, insertedVnodeQueue, parentElm, refElm)
175-
}
176-
return true
177-
}
171+
const isReactivated = isDef(vnode.child) && vnode.data.keepAlive
172+
vnode.data.hook.init(vnode, false /* hydrating */, parentElm, refElm)
173+
initComponent(vnode, insertedVnodeQueue)
174+
if (isReactivated) {
175+
reactivateComponent(vnode, insertedVnodeQueue, parentElm, refElm)
178176
}
179177
}
180178

@@ -481,13 +479,16 @@ export function createPatchFunction (backend) {
481479
}
482480
vnode.elm = elm
483481
const { tag, data, children } = vnode
484-
if (isDef(data)) {
485-
if (isDef(i = data.hook) && isDef(i = i.init)) i(vnode, true /* hydrating */)
486-
if (isDef(i = vnode.child)) {
487-
// child component. it should have hydrated its own tree.
488-
initComponent(vnode, insertedVnodeQueue)
489-
return true
482+
if (isDef(vnode.componentOptions)) {
483+
// child component. it should have hydrated its own tree.
484+
if (hasRIC && vnode.componentOptions.Ctor.options.bootAsync) {
485+
requestIdleCallback(() => {
486+
hydrateComponent(vnode, insertedVnodeQueue)
487+
})
488+
} else {
489+
hydrateComponent(vnode, insertedVnodeQueue)
490490
}
491+
return true
491492
}
492493
if (isDef(tag)) {
493494
if (isDef(children)) {
@@ -530,6 +531,11 @@ export function createPatchFunction (backend) {
530531
return true
531532
}
532533

534+
function hydrateComponent (vnode, insertedVnodeQueue) {
535+
vnode.data.hook.init(vnode, true /* hydrating */)
536+
initComponent(vnode, insertedVnodeQueue)
537+
}
538+
533539
function assertNodeMatch (node, vnode) {
534540
if (vnode.tag) {
535541
return (

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy