MCPcopy
hub / github.com/NervJS/nerv / patch

Function patch

packages/nerv/src/vdom/patch.ts:26–99  ·  view source on GitHub ↗
(
  lastVnode,
  nextVnode,
  parentNode: Element,
  context: object,
  isSvg?: boolean
)

Source from the content-addressed store, hash-verified

24import SVGPropertyConfig from './svg-property-config'
25
26export function patch (
27 lastVnode,
28 nextVnode,
29 parentNode: Element,
30 context: object,
31 isSvg?: boolean
32) {
33 const lastDom = lastVnode.dom
34 let newDom
35 const lastVnodeIsArray = isArray(lastVnode)
36 const nextVnodeisArray = isArray(nextVnode)
37 if (isSameVNode(lastVnode, nextVnode)) {
38 const vtype = nextVnode.vtype
39 if (vtype & VType.Node) {
40 isSvg = isNullOrUndef(isSvg) ? lastVnode.isSvg : isSvg
41 if (isSvg) {
42 nextVnode.isSvg = isSvg
43 }
44 patchProps(lastDom, nextVnode.props, lastVnode.props, lastVnode, isSvg)
45 patchChildren(
46 lastDom,
47 lastVnode.children,
48 nextVnode.children,
49 context,
50 isSvg as boolean
51 )
52 if (nextVnode.ref !== null) {
53 Ref.update(lastVnode, nextVnode, lastDom)
54 }
55 newDom = lastDom
56 } else if ((vtype & (VType.Composite)) > 0) {
57 newDom = nextVnode.update(lastVnode, nextVnode, context)
58 } else if (vtype & VType.Text) {
59 return patchVText(lastVnode, nextVnode)
60 } else if (vtype & VType.Portal) {
61 patchChildren(lastVnode.type, lastVnode.children, nextVnode.children, context, isSvg as boolean)
62 }
63 // @TODO: test case
64 nextVnode.dom = newDom || lastDom
65 } else if (isArray(lastVnode) && isArray(nextVnode)) {
66 patchArrayChildren(lastDom, lastVnode, nextVnode, context, false)
67 } else if (lastVnodeIsArray && !nextVnodeisArray) {
68 patchArrayChildren(parentNode, lastVnode, [nextVnode], context, false)
69 } else if (!lastVnodeIsArray && nextVnodeisArray) {
70 newDom = createElement(nextVnode, isSvg, context)
71 insertElement(newDom, parentNode, lastDom)
72 parentNode.removeChild(lastDom)
73 } else {
74 unmount(lastVnode)
75 newDom = createElement(nextVnode, isSvg, context)
76 if (nextVnode !== null) {
77 nextVnode.dom = newDom
78 }
79 const newDomIsArray = isArray(newDom)
80 const lastDomIsArray = isArray(lastDom)
81 if (newDomIsArray) {
82 insertElement(newDom, parentNode, lastDom)
83 parentNode.removeChild(lastDom)

Callers 5

renderFunction · 0.90
updateComponentFunction · 0.85
patchChildrenFunction · 0.85
patchNonKeyedChildrenFunction · 0.85
patchKeyedChildrenFunction · 0.85

Calls 10

isNullOrUndefFunction · 0.90
insertElementFunction · 0.90
unmountFunction · 0.90
isSameVNodeFunction · 0.85
patchPropsFunction · 0.85
patchChildrenFunction · 0.85
patchVTextFunction · 0.85
patchArrayChildrenFunction · 0.85
createElementFunction · 0.70
updateMethod · 0.65

Tested by

no test coverage detected