wip: error handling and nextTick for time slicing
This commit is contained in:
parent
d5862d8c51
commit
d70b7d6dd5
@ -44,7 +44,7 @@ describe('attribute fallthrough', () => {
|
|||||||
|
|
||||||
const root = document.createElement('div')
|
const root = document.createElement('div')
|
||||||
document.body.appendChild(root)
|
document.body.appendChild(root)
|
||||||
render(h(Hello), root)
|
await render(h(Hello), root)
|
||||||
|
|
||||||
const node = root.children[0] as HTMLElement
|
const node = root.children[0] as HTMLElement
|
||||||
|
|
||||||
@ -110,7 +110,7 @@ describe('attribute fallthrough', () => {
|
|||||||
|
|
||||||
const root = document.createElement('div')
|
const root = document.createElement('div')
|
||||||
document.body.appendChild(root)
|
document.body.appendChild(root)
|
||||||
render(h(Hello), root)
|
await render(h(Hello), root)
|
||||||
|
|
||||||
const node = root.children[0] as HTMLElement
|
const node = root.children[0] as HTMLElement
|
||||||
|
|
||||||
@ -190,7 +190,7 @@ describe('attribute fallthrough', () => {
|
|||||||
|
|
||||||
const root = document.createElement('div')
|
const root = document.createElement('div')
|
||||||
document.body.appendChild(root)
|
document.body.appendChild(root)
|
||||||
render(h(Hello), root)
|
await render(h(Hello), root)
|
||||||
|
|
||||||
const node = root.children[0] as HTMLElement
|
const node = root.children[0] as HTMLElement
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { withHooks, useState, h, nextTick, useEffect, Component } from '../src'
|
import { withHooks, useState, h, nextTick, useEffect, Component } from '../src'
|
||||||
import { renderIntsance, serialize, triggerEvent } from '@vue/runtime-test'
|
import { renderInstance, serialize, triggerEvent } from '@vue/runtime-test'
|
||||||
|
|
||||||
describe('hooks', () => {
|
describe('hooks', () => {
|
||||||
it('useState', async () => {
|
it('useState', async () => {
|
||||||
@ -16,7 +16,7 @@ describe('hooks', () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
const counter = renderIntsance(Counter)
|
const counter = await renderInstance(Counter)
|
||||||
expect(serialize(counter.$el)).toBe(`<div>0</div>`)
|
expect(serialize(counter.$el)).toBe(`<div>0</div>`)
|
||||||
|
|
||||||
triggerEvent(counter.$el, 'click')
|
triggerEvent(counter.$el, 'click')
|
||||||
@ -40,7 +40,7 @@ describe('hooks', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const counter = renderIntsance(Counter)
|
const counter = await renderInstance(Counter)
|
||||||
expect(serialize(counter.$el)).toBe(`<div>0</div>`)
|
expect(serialize(counter.$el)).toBe(`<div>0</div>`)
|
||||||
|
|
||||||
triggerEvent(counter.$el, 'click')
|
triggerEvent(counter.$el, 'click')
|
||||||
@ -71,7 +71,7 @@ describe('hooks', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const counter = renderIntsance(Counter)
|
const counter = await renderInstance(Counter)
|
||||||
expect(serialize(counter.$el)).toBe(`<div>0</div>`)
|
expect(serialize(counter.$el)).toBe(`<div>0</div>`)
|
||||||
|
|
||||||
triggerEvent(counter.$el, 'click')
|
triggerEvent(counter.$el, 'click')
|
||||||
@ -98,7 +98,7 @@ describe('hooks', () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
const counter = renderIntsance(Counter)
|
const counter = await renderInstance(Counter)
|
||||||
expect(effect).toBe(0)
|
expect(effect).toBe(0)
|
||||||
triggerEvent(counter.$el, 'click')
|
triggerEvent(counter.$el, 'click')
|
||||||
await nextTick()
|
await nextTick()
|
||||||
|
@ -7,7 +7,7 @@ import {
|
|||||||
ComponentPropsOptions,
|
ComponentPropsOptions,
|
||||||
ComponentWatchOptions
|
ComponentWatchOptions
|
||||||
} from '@vue/runtime-core'
|
} from '@vue/runtime-core'
|
||||||
import { createInstance, renderIntsance } from '@vue/runtime-test'
|
import { createInstance, renderInstance } from '@vue/runtime-test'
|
||||||
|
|
||||||
describe('class inheritance', () => {
|
describe('class inheritance', () => {
|
||||||
it('should merge data', () => {
|
it('should merge data', () => {
|
||||||
@ -136,7 +136,7 @@ describe('class inheritance', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const container = renderIntsance(Container)
|
const container = await renderInstance(Container)
|
||||||
expect(calls).toEqual([
|
expect(calls).toEqual([
|
||||||
'base beforeCreate',
|
'base beforeCreate',
|
||||||
'child beforeCreate',
|
'child beforeCreate',
|
||||||
@ -200,7 +200,7 @@ describe('class inheritance', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const container = renderIntsance(Container)
|
const container = await renderInstance(Container)
|
||||||
expect(container.$el.text).toBe('foo')
|
expect(container.$el.text).toBe('foo')
|
||||||
|
|
||||||
container.ok = false
|
container.ok = false
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { h, Component, memoize, nextTick } from '../src'
|
import { h, Component, memoize, nextTick } from '../src'
|
||||||
import { renderIntsance, serialize } from '@vue/runtime-test'
|
import { renderInstance, serialize } from '@vue/runtime-test'
|
||||||
|
|
||||||
describe('memoize', () => {
|
describe('memoize', () => {
|
||||||
it('should work', async () => {
|
it('should work', async () => {
|
||||||
@ -16,7 +16,7 @@ describe('memoize', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const app = renderIntsance(App)
|
const app = await renderInstance(App)
|
||||||
expect(serialize(app.$el)).toBe(`<div>1<div>A1</div><div>B1</div></div>`)
|
expect(serialize(app.$el)).toBe(`<div>1<div>A1</div><div>B1</div></div>`)
|
||||||
|
|
||||||
app.count++
|
app.count++
|
||||||
@ -38,7 +38,7 @@ describe('memoize', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const app = renderIntsance(App)
|
const app = await renderInstance(App)
|
||||||
expect(serialize(app.$el)).toBe(`<div>2</div>`)
|
expect(serialize(app.$el)).toBe(`<div>2</div>`)
|
||||||
|
|
||||||
app.foo++
|
app.foo++
|
||||||
|
@ -40,7 +40,7 @@ describe('Parent chain management', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const root = nodeOps.createElement('div')
|
const root = nodeOps.createElement('div')
|
||||||
const parent = render(h(Parent), root) as Component
|
const parent = (await render(h(Parent), root)) as Component
|
||||||
|
|
||||||
expect(child.$parent).toBe(parent)
|
expect(child.$parent).toBe(parent)
|
||||||
expect(child.$root).toBe(parent)
|
expect(child.$root).toBe(parent)
|
||||||
@ -99,7 +99,7 @@ describe('Parent chain management', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const root = nodeOps.createElement('div')
|
const root = nodeOps.createElement('div')
|
||||||
const parent = render(h(Parent), root) as Component
|
const parent = (await render(h(Parent), root)) as Component
|
||||||
|
|
||||||
expect(child.$parent).toBe(parent)
|
expect(child.$parent).toBe(parent)
|
||||||
expect(child.$root).toBe(parent)
|
expect(child.$root).toBe(parent)
|
||||||
|
@ -18,7 +18,11 @@ import {
|
|||||||
resolveComponentOptionsFromClass
|
resolveComponentOptionsFromClass
|
||||||
} from './componentOptions'
|
} from './componentOptions'
|
||||||
import { createRenderProxy } from './componentProxy'
|
import { createRenderProxy } from './componentProxy'
|
||||||
import { handleError, ErrorTypes } from './errorHandling'
|
import {
|
||||||
|
handleError,
|
||||||
|
ErrorTypes,
|
||||||
|
callLifecycleHookWithHandle
|
||||||
|
} from './errorHandling'
|
||||||
import { warn } from './warning'
|
import { warn } from './warning'
|
||||||
import { setCurrentInstance, unsetCurrentInstance } from './experimental/hooks'
|
import { setCurrentInstance, unsetCurrentInstance } from './experimental/hooks'
|
||||||
|
|
||||||
@ -52,7 +56,7 @@ export function createComponentInstance<T extends Component>(
|
|||||||
instance.$slots = currentVNode.slots || EMPTY_OBJ
|
instance.$slots = currentVNode.slots || EMPTY_OBJ
|
||||||
|
|
||||||
if (created) {
|
if (created) {
|
||||||
created.call($proxy)
|
callLifecycleHookWithHandle(created, $proxy, ErrorTypes.CREATED)
|
||||||
}
|
}
|
||||||
|
|
||||||
currentVNode = currentContextVNode = null
|
currentVNode = currentContextVNode = null
|
||||||
@ -96,7 +100,7 @@ export function initializeComponentInstance(instance: ComponentInstance) {
|
|||||||
// beforeCreate hook is called right in the constructor
|
// beforeCreate hook is called right in the constructor
|
||||||
const { beforeCreate, props } = instance.$options
|
const { beforeCreate, props } = instance.$options
|
||||||
if (beforeCreate) {
|
if (beforeCreate) {
|
||||||
beforeCreate.call(proxy)
|
callLifecycleHookWithHandle(beforeCreate, proxy, ErrorTypes.BEFORE_CREATE)
|
||||||
}
|
}
|
||||||
initializeProps(instance, props, (currentVNode as VNode).data)
|
initializeProps(instance, props, (currentVNode as VNode).data)
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { autorun, stop, Autorun, immutable } from '@vue/observer'
|
import { autorun, stop, Autorun, immutable } from '@vue/observer'
|
||||||
import { queueJob } from '@vue/scheduler'
|
import { queueJob, handleSchedulerError, nextTick } from '@vue/scheduler'
|
||||||
import { VNodeFlags, ChildrenFlags } from './flags'
|
import { VNodeFlags, ChildrenFlags } from './flags'
|
||||||
import { EMPTY_OBJ, reservedPropRE, isString } from '@vue/shared'
|
import { EMPTY_OBJ, reservedPropRE, isString } from '@vue/shared'
|
||||||
import {
|
import {
|
||||||
@ -20,8 +20,12 @@ import {
|
|||||||
} from './componentUtils'
|
} from './componentUtils'
|
||||||
import { KeepAliveSymbol } from './optional/keepAlive'
|
import { KeepAliveSymbol } from './optional/keepAlive'
|
||||||
import { pushWarningContext, popWarningContext, warn } from './warning'
|
import { pushWarningContext, popWarningContext, warn } from './warning'
|
||||||
import { handleError, ErrorTypes } from './errorHandling'
|
|
||||||
import { resolveProps } from './componentProps'
|
import { resolveProps } from './componentProps'
|
||||||
|
import {
|
||||||
|
handleError,
|
||||||
|
ErrorTypes,
|
||||||
|
callLifecycleHookWithHandle
|
||||||
|
} from './errorHandling'
|
||||||
|
|
||||||
export interface NodeOps {
|
export interface NodeOps {
|
||||||
createElement: (tag: string, isSVG?: boolean) => any
|
createElement: (tag: string, isSVG?: boolean) => any
|
||||||
@ -64,6 +68,8 @@ export interface FunctionalHandle {
|
|||||||
forceUpdate: () => void
|
forceUpdate: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleSchedulerError(err => handleError(err, null, ErrorTypes.SCHEDULER))
|
||||||
|
|
||||||
// The whole mounting / patching / unmouting logic is placed inside this
|
// The whole mounting / patching / unmouting logic is placed inside this
|
||||||
// single function so that we can create multiple renderes with different
|
// single function so that we can create multiple renderes with different
|
||||||
// platform definitions. This allows for use cases like creating a test
|
// platform definitions. This allows for use cases like creating a test
|
||||||
@ -184,7 +190,7 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
mountRef(ref, el)
|
mountRef(ref, el)
|
||||||
}
|
}
|
||||||
if (data != null && data.vnodeMounted) {
|
if (data != null && data.vnodeMounted) {
|
||||||
lifecycleHooks.push(() => {
|
lifecycleHooks.unshift(() => {
|
||||||
data.vnodeMounted(vnode)
|
data.vnodeMounted(vnode)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -204,18 +210,12 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
endNode: RenderNode | null
|
endNode: RenderNode | null
|
||||||
) {
|
) {
|
||||||
vnode.contextVNode = contextVNode
|
vnode.contextVNode = contextVNode
|
||||||
if (__DEV__) {
|
|
||||||
pushWarningContext(vnode)
|
|
||||||
}
|
|
||||||
const { flags } = vnode
|
const { flags } = vnode
|
||||||
if (flags & VNodeFlags.COMPONENT_STATEFUL) {
|
if (flags & VNodeFlags.COMPONENT_STATEFUL) {
|
||||||
mountStatefulComponent(vnode, container, isSVG, endNode)
|
mountStatefulComponent(vnode, container, isSVG, endNode)
|
||||||
} else {
|
} else {
|
||||||
mountFunctionalComponent(vnode, container, isSVG, endNode)
|
mountFunctionalComponent(vnode, container, isSVG, endNode)
|
||||||
}
|
}
|
||||||
if (__DEV__) {
|
|
||||||
popWarningContext()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function mountStatefulComponent(
|
function mountStatefulComponent(
|
||||||
@ -228,15 +228,13 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
// kept-alive
|
// kept-alive
|
||||||
activateComponentInstance(vnode, container, endNode)
|
activateComponentInstance(vnode, container, endNode)
|
||||||
} else {
|
} else {
|
||||||
queueJob(
|
if (__JSDOM__) {
|
||||||
() => {
|
|
||||||
mountComponentInstance(vnode, container, isSVG, endNode)
|
mountComponentInstance(vnode, container, isSVG, endNode)
|
||||||
},
|
} else {
|
||||||
flushHooks,
|
queueJob(() => {
|
||||||
err => {
|
mountComponentInstance(vnode, container, isSVG, endNode)
|
||||||
handleError(err, vnode.contextVNode as VNode, ErrorTypes.SCHEDULER)
|
}, flushHooks)
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -260,22 +258,20 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
forceUpdate: null as any
|
forceUpdate: null as any
|
||||||
})
|
})
|
||||||
|
|
||||||
const handleSchedulerError = (err: Error) => {
|
|
||||||
handleError(err, handle.current as VNode, ErrorTypes.SCHEDULER)
|
|
||||||
}
|
|
||||||
|
|
||||||
const queueUpdate = (handle.forceUpdate = () => {
|
const queueUpdate = (handle.forceUpdate = () => {
|
||||||
queueJob(handle.runner, null, handleSchedulerError)
|
queueJob(handle.runner)
|
||||||
})
|
})
|
||||||
|
|
||||||
// we are using vnode.ref to store the functional component's update job
|
// we are using vnode.ref to store the functional component's update job
|
||||||
queueJob(
|
queueJob(() => {
|
||||||
() => {
|
|
||||||
handle.runner = autorun(
|
handle.runner = autorun(
|
||||||
() => {
|
() => {
|
||||||
if (handle.prevTree) {
|
if (handle.prevTree) {
|
||||||
// mounted
|
// mounted
|
||||||
const { prevTree, current } = handle
|
const { prevTree, current } = handle
|
||||||
|
if (__DEV__) {
|
||||||
|
pushWarningContext(current)
|
||||||
|
}
|
||||||
const nextTree = (handle.prevTree = current.children = renderFunctionalRoot(
|
const nextTree = (handle.prevTree = current.children = renderFunctionalRoot(
|
||||||
current
|
current
|
||||||
))
|
))
|
||||||
@ -287,23 +283,29 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
isSVG
|
isSVG
|
||||||
)
|
)
|
||||||
current.el = nextTree.el
|
current.el = nextTree.el
|
||||||
|
if (__DEV__) {
|
||||||
|
popWarningContext()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// initial mount
|
// initial mount
|
||||||
|
if (__DEV__) {
|
||||||
|
pushWarningContext(vnode)
|
||||||
|
}
|
||||||
const subTree = (handle.prevTree = vnode.children = renderFunctionalRoot(
|
const subTree = (handle.prevTree = vnode.children = renderFunctionalRoot(
|
||||||
vnode
|
vnode
|
||||||
))
|
))
|
||||||
mount(subTree, container, vnode as MountedVNode, isSVG, endNode)
|
mount(subTree, container, vnode as MountedVNode, isSVG, endNode)
|
||||||
vnode.el = subTree.el as RenderNode
|
vnode.el = subTree.el as RenderNode
|
||||||
|
if (__DEV__) {
|
||||||
|
popWarningContext()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
scheduler: queueUpdate
|
scheduler: queueUpdate
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
},
|
})
|
||||||
null,
|
|
||||||
handleSchedulerError
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function mountText(
|
function mountText(
|
||||||
@ -514,9 +516,6 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
contextVNode: MountedVNode | null,
|
contextVNode: MountedVNode | null,
|
||||||
isSVG: boolean
|
isSVG: boolean
|
||||||
) {
|
) {
|
||||||
if (__DEV__) {
|
|
||||||
pushWarningContext(nextVNode)
|
|
||||||
}
|
|
||||||
nextVNode.contextVNode = contextVNode
|
nextVNode.contextVNode = contextVNode
|
||||||
const { tag, flags } = nextVNode
|
const { tag, flags } = nextVNode
|
||||||
if (tag !== prevVNode.tag) {
|
if (tag !== prevVNode.tag) {
|
||||||
@ -526,9 +525,6 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
} else {
|
} else {
|
||||||
patchFunctionalComponent(prevVNode, nextVNode)
|
patchFunctionalComponent(prevVNode, nextVNode)
|
||||||
}
|
}
|
||||||
if (__DEV__) {
|
|
||||||
popWarningContext()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function patchStatefulComponent(prevVNode: MountedVNode, nextVNode: VNode) {
|
function patchStatefulComponent(prevVNode: MountedVNode, nextVNode: VNode) {
|
||||||
@ -1161,6 +1157,10 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
isSVG: boolean,
|
isSVG: boolean,
|
||||||
endNode: RenderNode | null
|
endNode: RenderNode | null
|
||||||
): RenderNode {
|
): RenderNode {
|
||||||
|
if (__DEV__) {
|
||||||
|
pushWarningContext(vnode)
|
||||||
|
}
|
||||||
|
|
||||||
// a vnode may already have an instance if this is a compat call with
|
// a vnode may already have an instance if this is a compat call with
|
||||||
// new Vue()
|
// new Vue()
|
||||||
const instance = ((__COMPAT__ && vnode.children) ||
|
const instance = ((__COMPAT__ && vnode.children) ||
|
||||||
@ -1177,15 +1177,11 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
} = instance
|
} = instance
|
||||||
|
|
||||||
if (beforeMount) {
|
if (beforeMount) {
|
||||||
beforeMount.call($proxy)
|
callLifecycleHookWithHandle(beforeMount, $proxy, ErrorTypes.BEFORE_MOUNT)
|
||||||
}
|
|
||||||
|
|
||||||
const handleSchedulerError = (err: Error) => {
|
|
||||||
handleError(err, instance, ErrorTypes.SCHEDULER)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const queueUpdate = (instance.$forceUpdate = () => {
|
const queueUpdate = (instance.$forceUpdate = () => {
|
||||||
queueJob(instance._updateHandle, flushHooks, handleSchedulerError)
|
queueJob(instance._updateHandle, flushHooks)
|
||||||
})
|
})
|
||||||
|
|
||||||
instance._updateHandle = autorun(
|
instance._updateHandle = autorun(
|
||||||
@ -1222,7 +1218,7 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
const { mounted } = instance.$options
|
const { mounted } = instance.$options
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
lifecycleHooks.unshift(() => {
|
lifecycleHooks.unshift(() => {
|
||||||
mounted.call($proxy)
|
callLifecycleHookWithHandle(mounted, $proxy, ErrorTypes.MOUNTED)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1234,6 +1230,10 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (__DEV__) {
|
||||||
|
popWarningContext()
|
||||||
|
}
|
||||||
|
|
||||||
return vnode.el as RenderNode
|
return vnode.el as RenderNode
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1252,7 +1252,12 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
$options: { beforeUpdate }
|
$options: { beforeUpdate }
|
||||||
} = instance
|
} = instance
|
||||||
if (beforeUpdate) {
|
if (beforeUpdate) {
|
||||||
beforeUpdate.call($proxy, prevVNode)
|
callLifecycleHookWithHandle(
|
||||||
|
beforeUpdate,
|
||||||
|
$proxy,
|
||||||
|
ErrorTypes.BEFORE_UPDATE,
|
||||||
|
prevVNode
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const nextVNode = (instance.$vnode = renderInstanceRoot(
|
const nextVNode = (instance.$vnode = renderInstanceRoot(
|
||||||
@ -1286,7 +1291,12 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
// invoked BEFORE the parent's. Therefore we add them to the head of the
|
// invoked BEFORE the parent's. Therefore we add them to the head of the
|
||||||
// queue instead.
|
// queue instead.
|
||||||
lifecycleHooks.unshift(() => {
|
lifecycleHooks.unshift(() => {
|
||||||
updated.call($proxy, nextVNode)
|
callLifecycleHookWithHandle(
|
||||||
|
updated,
|
||||||
|
$proxy,
|
||||||
|
ErrorTypes.UPDATED,
|
||||||
|
nextVNode
|
||||||
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1316,7 +1326,11 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
$options: { beforeUnmount, unmounted }
|
$options: { beforeUnmount, unmounted }
|
||||||
} = instance
|
} = instance
|
||||||
if (beforeUnmount) {
|
if (beforeUnmount) {
|
||||||
beforeUnmount.call($proxy)
|
callLifecycleHookWithHandle(
|
||||||
|
beforeUnmount,
|
||||||
|
$proxy,
|
||||||
|
ErrorTypes.BEFORE_UNMOUNT
|
||||||
|
)
|
||||||
}
|
}
|
||||||
if ($vnode) {
|
if ($vnode) {
|
||||||
unmount($vnode)
|
unmount($vnode)
|
||||||
@ -1325,7 +1339,7 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
teardownComponentInstance(instance)
|
teardownComponentInstance(instance)
|
||||||
instance._unmounted = true
|
instance._unmounted = true
|
||||||
if (unmounted) {
|
if (unmounted) {
|
||||||
unmounted.call($proxy)
|
callLifecycleHookWithHandle(unmounted, $proxy, ErrorTypes.UNMOUNTED)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1336,11 +1350,17 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
container: RenderNode | null,
|
container: RenderNode | null,
|
||||||
endNode: RenderNode | null
|
endNode: RenderNode | null
|
||||||
) {
|
) {
|
||||||
|
if (__DEV__) {
|
||||||
|
pushWarningContext(vnode)
|
||||||
|
}
|
||||||
const instance = vnode.children as ComponentInstance
|
const instance = vnode.children as ComponentInstance
|
||||||
vnode.el = instance.$el as RenderNode
|
vnode.el = instance.$el as RenderNode
|
||||||
if (container != null) {
|
if (container != null) {
|
||||||
insertVNode(instance.$vnode, container, endNode)
|
insertVNode(instance.$vnode, container, endNode)
|
||||||
}
|
}
|
||||||
|
if (__DEV__) {
|
||||||
|
popWarningContext()
|
||||||
|
}
|
||||||
lifecycleHooks.push(() => {
|
lifecycleHooks.push(() => {
|
||||||
callActivatedHook(instance, true)
|
callActivatedHook(instance, true)
|
||||||
})
|
})
|
||||||
@ -1363,7 +1383,7 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
callActivatedHook($children[i], false)
|
callActivatedHook($children[i], false)
|
||||||
}
|
}
|
||||||
if (activated) {
|
if (activated) {
|
||||||
activated.call($proxy)
|
callLifecycleHookWithHandle(activated, $proxy, ErrorTypes.ACTIVATED)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1388,7 +1408,7 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
callDeactivateHook($children[i], false)
|
callDeactivateHook($children[i], false)
|
||||||
}
|
}
|
||||||
if (deactivated) {
|
if (deactivated) {
|
||||||
deactivated.call($proxy)
|
callLifecycleHookWithHandle(deactivated, $proxy, ErrorTypes.DEACTIVATED)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1420,10 +1440,12 @@ export function createRenderer(options: RendererOptions) {
|
|||||||
container.vnode = null
|
container.vnode = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// flushHooks()
|
return nextTick(() => {
|
||||||
// return vnode && vnode.flags & VNodeFlags.COMPONENT_STATEFUL
|
debugger
|
||||||
// ? (vnode.children as ComponentInstance).$proxy
|
return vnode && vnode.flags & VNodeFlags.COMPONENT_STATEFUL
|
||||||
// : null
|
? (vnode.children as ComponentInstance).$proxy
|
||||||
|
: null
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return { render }
|
return { render }
|
||||||
|
@ -10,8 +10,10 @@ export const enum ErrorTypes {
|
|||||||
MOUNTED,
|
MOUNTED,
|
||||||
BEFORE_UPDATE,
|
BEFORE_UPDATE,
|
||||||
UPDATED,
|
UPDATED,
|
||||||
BEFORE_DESTROY,
|
BEFORE_UNMOUNT,
|
||||||
DESTROYED,
|
UNMOUNTED,
|
||||||
|
ACTIVATED,
|
||||||
|
DEACTIVATED,
|
||||||
ERROR_CAPTURED,
|
ERROR_CAPTURED,
|
||||||
RENDER,
|
RENDER,
|
||||||
WATCH_CALLBACK,
|
WATCH_CALLBACK,
|
||||||
@ -27,8 +29,10 @@ const ErrorTypeStrings: Record<number, string> = {
|
|||||||
[ErrorTypes.MOUNTED]: 'in mounted lifecycle hook',
|
[ErrorTypes.MOUNTED]: 'in mounted lifecycle hook',
|
||||||
[ErrorTypes.BEFORE_UPDATE]: 'in beforeUpdate lifecycle hook',
|
[ErrorTypes.BEFORE_UPDATE]: 'in beforeUpdate lifecycle hook',
|
||||||
[ErrorTypes.UPDATED]: 'in updated lifecycle hook',
|
[ErrorTypes.UPDATED]: 'in updated lifecycle hook',
|
||||||
[ErrorTypes.BEFORE_DESTROY]: 'in beforeDestroy lifecycle hook',
|
[ErrorTypes.BEFORE_UNMOUNT]: 'in beforeUnmount lifecycle hook',
|
||||||
[ErrorTypes.DESTROYED]: 'in destroyed lifecycle hook',
|
[ErrorTypes.UNMOUNTED]: 'in unmounted lifecycle hook',
|
||||||
|
[ErrorTypes.ACTIVATED]: 'in activated lifecycle hook',
|
||||||
|
[ErrorTypes.DEACTIVATED]: 'in deactivated lifecycle hook',
|
||||||
[ErrorTypes.ERROR_CAPTURED]: 'in errorCaptured lifecycle hook',
|
[ErrorTypes.ERROR_CAPTURED]: 'in errorCaptured lifecycle hook',
|
||||||
[ErrorTypes.RENDER]: 'in render function',
|
[ErrorTypes.RENDER]: 'in render function',
|
||||||
[ErrorTypes.WATCH_CALLBACK]: 'in watcher callback',
|
[ErrorTypes.WATCH_CALLBACK]: 'in watcher callback',
|
||||||
@ -38,6 +42,24 @@ const ErrorTypeStrings: Record<number, string> = {
|
|||||||
'when flushing updates. This may be a Vue internals bug.'
|
'when flushing updates. This may be a Vue internals bug.'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function callLifecycleHookWithHandle(
|
||||||
|
hook: Function,
|
||||||
|
instanceProxy: ComponentInstance,
|
||||||
|
type: ErrorTypes,
|
||||||
|
arg?: any
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const res = hook.call(instanceProxy, arg)
|
||||||
|
if (res && typeof res.then === 'function') {
|
||||||
|
;(res as Promise<any>).catch(err => {
|
||||||
|
handleError(err, instanceProxy._self, type)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
handleError(err, instanceProxy._self, type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function handleError(
|
export function handleError(
|
||||||
err: Error,
|
err: Error,
|
||||||
instance: ComponentInstance | VNode | null,
|
instance: ComponentInstance | VNode | null,
|
||||||
|
@ -12,7 +12,7 @@ const { render: _render } = createRenderer({
|
|||||||
type publicRender = (
|
type publicRender = (
|
||||||
node: {} | null,
|
node: {} | null,
|
||||||
container: HTMLElement
|
container: HTMLElement
|
||||||
) => Component | null
|
) => Promise<Component | null>
|
||||||
export const render = _render as publicRender
|
export const render = _render as publicRender
|
||||||
|
|
||||||
// re-export everything from core
|
// re-export everything from core
|
||||||
|
@ -12,7 +12,7 @@ import {
|
|||||||
observable,
|
observable,
|
||||||
resetOps,
|
resetOps,
|
||||||
serialize,
|
serialize,
|
||||||
renderIntsance,
|
renderInstance,
|
||||||
triggerEvent
|
triggerEvent
|
||||||
} from '../src'
|
} from '../src'
|
||||||
|
|
||||||
@ -171,7 +171,7 @@ describe('test renderer', () => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const app = renderIntsance(App)
|
const app = await renderInstance(App)
|
||||||
triggerEvent(app.$el, 'click')
|
triggerEvent(app.$el, 'click')
|
||||||
expect(app.count).toBe(1)
|
expect(app.count).toBe(1)
|
||||||
await nextTick()
|
await nextTick()
|
@ -15,7 +15,7 @@ const { render: _render } = createRenderer({
|
|||||||
type publicRender = (
|
type publicRender = (
|
||||||
node: {} | null,
|
node: {} | null,
|
||||||
container: TestElement
|
container: TestElement
|
||||||
) => Component | null
|
) => Promise<Component | null>
|
||||||
export const render = _render as publicRender
|
export const render = _render as publicRender
|
||||||
|
|
||||||
export function createInstance<T extends Component>(
|
export function createInstance<T extends Component>(
|
||||||
@ -25,10 +25,10 @@ export function createInstance<T extends Component>(
|
|||||||
return createComponentInstance(h(Class, props)).$proxy as any
|
return createComponentInstance(h(Class, props)).$proxy as any
|
||||||
}
|
}
|
||||||
|
|
||||||
export function renderIntsance<T extends Component>(
|
export function renderInstance<T extends Component>(
|
||||||
Class: new () => T,
|
Class: new () => T,
|
||||||
props?: any
|
props?: any
|
||||||
): T {
|
): Promise<T> {
|
||||||
return render(h(Class, props), nodeOps.createElement('div')) as any
|
return render(h(Class, props), nodeOps.createElement('div')) as any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,48 +1,4 @@
|
|||||||
import { NodeOps } from '@vue/runtime-core'
|
import { Op, setCurrentOps } from './patchNodeOps'
|
||||||
import { nodeOps } from '../../runtime-dom/src/nodeOps'
|
|
||||||
|
|
||||||
const enum Priorities {
|
|
||||||
NORMAL = 500
|
|
||||||
}
|
|
||||||
|
|
||||||
const frameBudget = 1000 / 60
|
|
||||||
|
|
||||||
let start: number = 0
|
|
||||||
let currentOps: Op[]
|
|
||||||
|
|
||||||
const getNow = () => window.performance.now()
|
|
||||||
|
|
||||||
const evaluate = (v: any) => {
|
|
||||||
return typeof v === 'function' ? v() : v
|
|
||||||
}
|
|
||||||
|
|
||||||
// patch nodeOps to record operations without touching the DOM
|
|
||||||
Object.keys(nodeOps).forEach((key: keyof NodeOps) => {
|
|
||||||
const original = nodeOps[key] as Function
|
|
||||||
if (key === 'querySelector') {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (/create/.test(key)) {
|
|
||||||
nodeOps[key] = (...args: any[]) => {
|
|
||||||
let res: any
|
|
||||||
if (currentOps) {
|
|
||||||
return () => res || (res = original(...args))
|
|
||||||
} else {
|
|
||||||
return original(...args)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
nodeOps[key] = (...args: any[]) => {
|
|
||||||
if (currentOps) {
|
|
||||||
currentOps.push([original, ...args.map(evaluate)])
|
|
||||||
} else {
|
|
||||||
original(...args)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
type Op = [Function, ...any[]]
|
|
||||||
|
|
||||||
interface Job extends Function {
|
interface Job extends Function {
|
||||||
ops: Op[]
|
ops: Op[]
|
||||||
@ -50,11 +6,29 @@ interface Job extends Function {
|
|||||||
expiration: number
|
expiration: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const enum Priorities {
|
||||||
|
NORMAL = 500
|
||||||
|
}
|
||||||
|
|
||||||
|
type ErrorHandler = (err: Error) => any
|
||||||
|
|
||||||
|
let start: number = 0
|
||||||
|
const getNow = () => window.performance.now()
|
||||||
|
const frameBudget = __JSDOM__ ? Infinity : 1000 / 60
|
||||||
|
|
||||||
|
const patchQueue: Job[] = []
|
||||||
|
const commitQueue: Job[] = []
|
||||||
|
const postCommitQueue: Function[] = []
|
||||||
|
const nextTickQueue: Function[] = []
|
||||||
|
|
||||||
|
let globalHandler: ErrorHandler
|
||||||
|
const pendingRejectors: ErrorHandler[] = []
|
||||||
|
|
||||||
// Microtask for batching state mutations
|
// Microtask for batching state mutations
|
||||||
const p = Promise.resolve()
|
const p = Promise.resolve()
|
||||||
|
|
||||||
export function nextTick(fn?: () => void): Promise<void> {
|
function flushAfterMicroTask() {
|
||||||
return p.then(fn)
|
return p.then(flush).catch(handleError)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Macrotask for time slicing
|
// Macrotask for time slicing
|
||||||
@ -67,46 +41,48 @@ window.addEventListener(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
start = getNow()
|
start = getNow()
|
||||||
|
try {
|
||||||
flush()
|
flush()
|
||||||
|
} catch (e) {
|
||||||
|
handleError(e)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
false
|
false
|
||||||
)
|
)
|
||||||
|
|
||||||
function flushAfterYield() {
|
function flushAfterMacroTask() {
|
||||||
window.postMessage(key, `*`)
|
window.postMessage(key, `*`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const patchQueue: Job[] = []
|
export function nextTick<T>(fn?: () => T): Promise<T> {
|
||||||
const commitQueue: Job[] = []
|
return new Promise((resolve, reject) => {
|
||||||
|
p.then(() => {
|
||||||
function patch(job: Job) {
|
if (hasPendingFlush) {
|
||||||
// job with existing ops means it's already been patched in a low priority queue
|
nextTickQueue.push(() => {
|
||||||
if (job.ops.length === 0) {
|
resolve(fn ? fn() : undefined)
|
||||||
currentOps = job.ops
|
})
|
||||||
job()
|
pendingRejectors.push(reject)
|
||||||
commitQueue.push(job)
|
} else {
|
||||||
|
resolve(fn ? fn() : undefined)
|
||||||
}
|
}
|
||||||
|
}).catch(reject)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function commit({ ops }: Job) {
|
function handleError(err: Error) {
|
||||||
for (let i = 0; i < ops.length; i++) {
|
if (globalHandler) globalHandler(err)
|
||||||
const [fn, ...args] = ops[i]
|
pendingRejectors.forEach(handler => {
|
||||||
fn(...args)
|
handler(err)
|
||||||
}
|
})
|
||||||
ops.length = 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function invalidate(job: Job) {
|
export function handleSchedulerError(handler: ErrorHandler) {
|
||||||
job.ops.length = 0
|
globalHandler = handler
|
||||||
}
|
}
|
||||||
|
|
||||||
let hasPendingFlush = false
|
let hasPendingFlush = false
|
||||||
|
|
||||||
export function queueJob(
|
export function queueJob(rawJob: Function, postJob?: Function | null) {
|
||||||
rawJob: Function,
|
|
||||||
postJob?: Function | null,
|
|
||||||
onError?: (reason: any) => void
|
|
||||||
) {
|
|
||||||
const job = rawJob as Job
|
const job = rawJob as Job
|
||||||
job.post = postJob || null
|
job.post = postJob || null
|
||||||
job.ops = job.ops || []
|
job.ops = job.ops || []
|
||||||
@ -117,7 +93,7 @@ export function queueJob(
|
|||||||
// invalidated. remove from commit queue
|
// invalidated. remove from commit queue
|
||||||
// and move it back to the patch queue
|
// and move it back to the patch queue
|
||||||
commitQueue.splice(commitIndex, 1)
|
commitQueue.splice(commitIndex, 1)
|
||||||
invalidate(job)
|
invalidateJob(job)
|
||||||
// With varying priorities we should insert job at correct position
|
// With varying priorities we should insert job at correct position
|
||||||
// based on expiration time.
|
// based on expiration time.
|
||||||
for (let i = 0; i < patchQueue.length; i++) {
|
for (let i = 0; i < patchQueue.length; i++) {
|
||||||
@ -135,17 +111,16 @@ export function queueJob(
|
|||||||
if (!hasPendingFlush) {
|
if (!hasPendingFlush) {
|
||||||
hasPendingFlush = true
|
hasPendingFlush = true
|
||||||
start = getNow()
|
start = getNow()
|
||||||
const p = nextTick(flush)
|
flushAfterMicroTask()
|
||||||
if (onError) p.catch(onError)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function flush() {
|
function flush(): void {
|
||||||
let job
|
let job
|
||||||
while (true) {
|
while (true) {
|
||||||
job = patchQueue.shift()
|
job = patchQueue.shift()
|
||||||
if (job) {
|
if (job) {
|
||||||
patch(job)
|
patchJob(job)
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -156,23 +131,55 @@ function flush() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (patchQueue.length === 0) {
|
if (patchQueue.length === 0) {
|
||||||
const postQueue: Function[] = []
|
|
||||||
// all done, time to commit!
|
// all done, time to commit!
|
||||||
while ((job = commitQueue.shift())) {
|
while ((job = commitQueue.shift())) {
|
||||||
commit(job)
|
commitJob(job)
|
||||||
if (job.post && postQueue.indexOf(job.post) < 0) {
|
if (job.post && postCommitQueue.indexOf(job.post) < 0) {
|
||||||
postQueue.push(job.post)
|
postCommitQueue.push(job.post)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while ((job = postQueue.shift())) {
|
// post commit hooks (updated, mounted)
|
||||||
|
while ((job = postCommitQueue.shift())) {
|
||||||
job()
|
job()
|
||||||
}
|
}
|
||||||
|
// some post commit hook triggered more updates...
|
||||||
if (patchQueue.length > 0) {
|
if (patchQueue.length > 0) {
|
||||||
return flushAfterYield()
|
if (getNow() - start > frameBudget) {
|
||||||
|
return flushAfterMacroTask()
|
||||||
|
} else {
|
||||||
|
// not out of budget yet, flush sync
|
||||||
|
return flush()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
// now we are really done
|
||||||
hasPendingFlush = false
|
hasPendingFlush = false
|
||||||
|
pendingRejectors.length = 0
|
||||||
|
while ((job = nextTickQueue.shift())) {
|
||||||
|
job()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// got more job to do
|
// got more job to do
|
||||||
flushAfterYield()
|
flushAfterMacroTask()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function patchJob(job: Job) {
|
||||||
|
// job with existing ops means it's already been patched in a low priority queue
|
||||||
|
if (job.ops.length === 0) {
|
||||||
|
setCurrentOps(job.ops)
|
||||||
|
job()
|
||||||
|
commitQueue.push(job)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function commitJob({ ops }: Job) {
|
||||||
|
for (let i = 0; i < ops.length; i++) {
|
||||||
|
const [fn, ...args] = ops[i]
|
||||||
|
fn(...args)
|
||||||
|
}
|
||||||
|
ops.length = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
function invalidateJob(job: Job) {
|
||||||
|
job.ops.length = 0
|
||||||
|
}
|
||||||
|
40
packages/scheduler/src/patchNodeOps.ts
Normal file
40
packages/scheduler/src/patchNodeOps.ts
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import { NodeOps } from '@vue/runtime-core'
|
||||||
|
import { nodeOps } from '../../runtime-dom/src/nodeOps'
|
||||||
|
|
||||||
|
export type Op = [Function, ...any[]]
|
||||||
|
|
||||||
|
let currentOps: Op[]
|
||||||
|
|
||||||
|
export function setCurrentOps(ops: Op[]) {
|
||||||
|
currentOps = ops
|
||||||
|
}
|
||||||
|
|
||||||
|
const evaluate = (v: any) => {
|
||||||
|
return typeof v === 'function' ? v() : v
|
||||||
|
}
|
||||||
|
|
||||||
|
// patch nodeOps to record operations without touching the DOM
|
||||||
|
Object.keys(nodeOps).forEach((key: keyof NodeOps) => {
|
||||||
|
const original = nodeOps[key] as Function
|
||||||
|
if (key === 'querySelector') {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (/create/.test(key)) {
|
||||||
|
nodeOps[key] = (...args: any[]) => {
|
||||||
|
let res: any
|
||||||
|
if (currentOps) {
|
||||||
|
return () => res || (res = original(...args))
|
||||||
|
} else {
|
||||||
|
return original(...args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
nodeOps[key] = (...args: any[]) => {
|
||||||
|
if (currentOps) {
|
||||||
|
currentOps.push([original, ...args.map(evaluate)])
|
||||||
|
} else {
|
||||||
|
original(...args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user