MCPcopy Create free account
hub / github.com/MMmaXingXing/mini-vue / patchKeyedChildren

Function patchKeyedChildren

code/src/runtime-core/renderer.ts:110–259  ·  view source on GitHub ↗
(
    c1,
    c2,
    container,
    parentComponent,
    parentAnchor
  )

Source from the content-addressed store, hash-verified

108 };
109
110 const patchKeyedChildren = (
111 c1,
112 c2,
113 container,
114 parentComponent,
115 parentAnchor
116 ) => {
117 let i = 0;
118 const l2 = c2.length;
119 let e1 = c1.length - 1;
120 let e2 = l2 - 1;
121
122 const isSomeVNdoeType = (n1, n2) => {
123 // 根据type 和 key来判断两个节点是否一样
124 return n1.type === n2.type && n1.key === n2.key;
125 };
126
127 // 从左往右找到不同节点的i的位置
128 while (i <= e1 && i <= e2) {
129 const n1 = c1[i];
130 const n2 = c2[i];
131 // 判断两个节点是否一样,一样的话再次调用patch去递归来处理
132 if (isSomeVNdoeType(n1, n2)) {
133 patch(n1, n2, container, parentComponent, parentAnchor);
134 } else {
135 break;
136 }
137 i++;
138 }
139 // 右边的处理
140 while (i <= e1 && i <= e2) {
141 const n1 = c1[e1];
142 const n2 = c2[e2];
143
144 if (isSomeVNdoeType(n1, n2)) {
145 patch(n1, n2, container, parentComponent, parentAnchor);
146 } else {
147 break;
148 }
149
150 e1--;
151 e2--;
152 }
153
154 // 3. 新的比老的多,创建
155 if (i > e1) {
156 if (i <= e2) {
157 const nextPos = e2 + 1;
158 const anchor = nextPos < l2 ? c2[nextPos].el : null;
159 // 可能是多个节点
160 while (i <= e2) {
161 patch(null, c2[i], container, parentComponent, anchor);
162 i++;
163 }
164 }
165 } else if (i > e2) {
166 // 4. 新的比老的少
167 while (i <= e1) {

Callers 1

patchChildernFunction · 0.70

Calls 5

setMethod · 0.80
getMethod · 0.80
isSomeVNdoeTypeFunction · 0.70
patchFunction · 0.70
getSequenceFunction · 0.70

Tested by

no test coverage detected