refactor: use shared options for component effect runners

This commit is contained in:
Evan You 2019-05-29 09:19:01 +08:00
parent 46524a0f0f
commit f2116054a0

View File

@ -18,6 +18,10 @@ import { TEXT, CLASS, STYLE, PROPS, KEYED, UNKEYED } from './patchFlags'
import { queueJob, queuePostFlushCb, flushPostFlushCbs } from './scheduler' import { queueJob, queuePostFlushCb, flushPostFlushCbs } from './scheduler'
import { effect, stop } from '@vue/observer' import { effect, stop } from '@vue/observer'
const sharedEffectOptions = {
scheduler: queueJob
}
function isSameType(n1: VNode, n2: VNode): boolean { function isSameType(n1: VNode, n2: VNode): boolean {
return n1.type === n2.type && n1.key === n2.key return n1.type === n2.type && n1.key === n2.key
} }
@ -347,50 +351,45 @@ export function createRenderer(options: RendererOptions) {
const instance: ComponentInstance = (vnode.component = createComponentInstance( const instance: ComponentInstance = (vnode.component = createComponentInstance(
vnode vnode
)) ))
instance.update = effect( instance.update = effect(() => {
() => { if (!instance.vnode) {
if (!instance.vnode) { // initial mount
// initial mount instance.vnode = vnode
instance.vnode = vnode const subTree = (instance.subTree = renderComponentRoot(instance))
const subTree = (instance.subTree = renderComponentRoot(instance)) if (instance.bm !== null) {
if (instance.bm !== null) { invokeHooks(instance.bm)
invokeHooks(instance.bm) }
} patch(null, subTree, container, anchor)
patch(null, subTree, container, anchor) vnode.el = subTree.el
vnode.el = subTree.el // mounted hook
// mounted hook if (instance.m !== null) {
if (instance.m !== null) { queuePostFlushCb(instance.m)
queuePostFlushCb(instance.m) }
} } else {
} else { // this is triggered by processComponent with `next` already set
// this is triggered by processComponent with `next` already set const { next } = instance
const { next } = instance if (next != null) {
if (next != null) { next.component = instance
next.component = instance instance.vnode = next
instance.vnode = next instance.next = null
instance.next = null }
} const prevTree = instance.subTree as VNode
const prevTree = instance.subTree as VNode const nextTree = (instance.subTree = renderComponentRoot(instance))
const nextTree = (instance.subTree = renderComponentRoot(instance)) patch(
patch( prevTree,
prevTree, nextTree,
nextTree, container || hostParentNode(prevTree.el),
container || hostParentNode(prevTree.el), anchor || getNextHostNode(prevTree)
anchor || getNextHostNode(prevTree) )
) if (next != null) {
if (next != null) { next.el = nextTree.el
next.el = nextTree.el }
} // upated hook
// upated hook if (instance.u !== null) {
if (instance.u !== null) { queuePostFlushCb(instance.u)
queuePostFlushCb(instance.u)
}
} }
},
{
scheduler: queueJob
} }
) }, sharedEffectOptions)
} }
function patchChildren( function patchChildren(