isomorfeus-preact 10.6.11 → 10.6.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/browser/event.rb +2 -0
- data/lib/isomorfeus/preact_view_helper.rb +3 -2
- data/lib/isomorfeus/props/validate_hash_proxy.rb +1 -1
- data/lib/isomorfeus/props/validator.rb +6 -4
- data/lib/isomorfeus-preact.rb +3 -3
- data/lib/isomorfeus_preact/lucid_component/api.rb +1 -1
- data/lib/isomorfeus_preact/lucid_component/initializer.rb +2 -2
- data/lib/isomorfeus_preact/lucid_func/initializer.rb +1 -1
- data/lib/isomorfeus_preact/preact/function_component/initializer.rb +1 -1
- data/lib/preact/component/api.rb +1 -1
- data/lib/preact/component/callbacks.rb +6 -6
- data/lib/preact/component/initializer.rb +2 -2
- data/lib/preact/component/native_component_constructor.rb +1 -1
- data/lib/preact/props.rb +51 -0
- data/lib/preact/state.rb +67 -0
- data/lib/preact/version.rb +1 -1
- data/lib/preact.rb +2 -2
- data/node_modules/.package-lock.json +3 -3
- data/node_modules/preact/LICENSE +21 -21
- data/node_modules/preact/README.md +187 -187
- data/node_modules/preact/compat/dist/compat.js +1 -1
- data/node_modules/preact/compat/dist/compat.js.map +1 -1
- data/node_modules/preact/compat/dist/compat.mjs +1 -1
- data/node_modules/preact/compat/dist/compat.module.js +1 -1
- data/node_modules/preact/compat/dist/compat.module.js.map +1 -1
- data/node_modules/preact/compat/dist/compat.umd.js +1 -1
- data/node_modules/preact/compat/dist/compat.umd.js.map +1 -1
- data/node_modules/preact/compat/jsx-dev-runtime.js +1 -1
- data/node_modules/preact/compat/jsx-dev-runtime.mjs +1 -1
- data/node_modules/preact/compat/jsx-runtime.js +1 -1
- data/node_modules/preact/compat/jsx-runtime.mjs +1 -1
- data/node_modules/preact/compat/package.json +19 -19
- data/node_modules/preact/compat/scheduler.js +15 -15
- data/node_modules/preact/compat/scheduler.mjs +23 -23
- data/node_modules/preact/compat/server.js +15 -15
- data/node_modules/preact/compat/server.mjs +4 -4
- data/node_modules/preact/compat/src/Children.js +21 -21
- data/node_modules/preact/compat/src/PureComponent.js +15 -15
- data/node_modules/preact/compat/src/forwardRef.js +51 -51
- data/node_modules/preact/compat/src/index.d.ts +141 -141
- data/node_modules/preact/compat/src/index.js +187 -187
- data/node_modules/preact/compat/src/internal.d.ts +47 -47
- data/node_modules/preact/compat/src/memo.js +34 -34
- data/node_modules/preact/compat/src/portals.js +80 -80
- data/node_modules/preact/compat/src/render.js +229 -230
- data/node_modules/preact/compat/src/suspense-list.d.ts +14 -14
- data/node_modules/preact/compat/src/suspense-list.js +126 -126
- data/node_modules/preact/compat/src/suspense.d.ts +15 -15
- data/node_modules/preact/compat/src/suspense.js +270 -270
- data/node_modules/preact/compat/src/util.js +28 -28
- data/node_modules/preact/compat/test-utils.js +1 -1
- data/node_modules/preact/debug/dist/debug.js.map +1 -1
- data/node_modules/preact/debug/dist/debug.module.js.map +1 -1
- data/node_modules/preact/debug/dist/debug.umd.js.map +1 -1
- data/node_modules/preact/debug/package.json +18 -18
- data/node_modules/preact/debug/src/check-props.js +54 -54
- data/node_modules/preact/debug/src/component-stack.js +146 -146
- data/node_modules/preact/debug/src/constants.js +3 -3
- data/node_modules/preact/debug/src/debug.js +442 -442
- data/node_modules/preact/debug/src/index.js +6 -6
- data/node_modules/preact/debug/src/internal.d.ts +82 -82
- data/node_modules/preact/debug/src/util.js +11 -11
- data/node_modules/preact/devtools/dist/devtools.js +1 -1
- data/node_modules/preact/devtools/dist/devtools.js.map +1 -1
- data/node_modules/preact/devtools/dist/devtools.mjs +1 -1
- data/node_modules/preact/devtools/dist/devtools.module.js +1 -1
- data/node_modules/preact/devtools/dist/devtools.module.js.map +1 -1
- data/node_modules/preact/devtools/dist/devtools.umd.js +1 -1
- data/node_modules/preact/devtools/dist/devtools.umd.js.map +1 -1
- data/node_modules/preact/devtools/package.json +16 -16
- data/node_modules/preact/devtools/src/devtools.js +10 -10
- data/node_modules/preact/devtools/src/index.d.ts +8 -8
- data/node_modules/preact/devtools/src/index.js +15 -15
- data/node_modules/preact/dist/preact.js.map +1 -1
- data/node_modules/preact/dist/preact.min.js.map +1 -1
- data/node_modules/preact/dist/preact.module.js.map +1 -1
- data/node_modules/preact/dist/preact.umd.js.map +1 -1
- data/node_modules/preact/hooks/dist/hooks.js +1 -1
- data/node_modules/preact/hooks/dist/hooks.js.map +1 -1
- data/node_modules/preact/hooks/dist/hooks.mjs +1 -1
- data/node_modules/preact/hooks/dist/hooks.module.js +1 -1
- data/node_modules/preact/hooks/dist/hooks.module.js.map +1 -1
- data/node_modules/preact/hooks/dist/hooks.umd.js +1 -1
- data/node_modules/preact/hooks/dist/hooks.umd.js.map +1 -1
- data/node_modules/preact/hooks/package.json +26 -26
- data/node_modules/preact/hooks/src/index.d.ts +139 -139
- data/node_modules/preact/hooks/src/index.js +386 -388
- data/node_modules/preact/hooks/src/internal.d.ts +75 -75
- data/node_modules/preact/jsx-runtime/dist/jsxRuntime.js.map +1 -1
- data/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js.map +1 -1
- data/node_modules/preact/jsx-runtime/dist/jsxRuntime.umd.js.map +1 -1
- data/node_modules/preact/jsx-runtime/package.json +19 -19
- data/node_modules/preact/jsx-runtime/src/index.d.ts +50 -50
- data/node_modules/preact/jsx-runtime/src/index.js +77 -77
- data/node_modules/preact/package.json +291 -287
- data/node_modules/preact/src/cjs.js +3 -3
- data/node_modules/preact/src/clone-element.js +34 -34
- data/node_modules/preact/src/component.js +225 -225
- data/node_modules/preact/src/constants.js +3 -3
- data/node_modules/preact/src/create-context.js +68 -68
- data/node_modules/preact/src/create-element.js +98 -98
- data/node_modules/preact/src/diff/catch-error.js +38 -38
- data/node_modules/preact/src/diff/children.js +335 -335
- data/node_modules/preact/src/diff/index.js +516 -516
- data/node_modules/preact/src/diff/props.js +158 -158
- data/node_modules/preact/src/index.d.ts +310 -310
- data/node_modules/preact/src/index.js +13 -13
- data/node_modules/preact/src/internal.d.ts +146 -146
- data/node_modules/preact/src/jsx.d.ts +1005 -1005
- data/node_modules/preact/src/options.js +16 -16
- data/node_modules/preact/src/render.js +75 -75
- data/node_modules/preact/src/util.js +27 -27
- data/node_modules/preact/test-utils/dist/testUtils.js.map +1 -1
- data/node_modules/preact/test-utils/dist/testUtils.module.js.map +1 -1
- data/node_modules/preact/test-utils/dist/testUtils.umd.js.map +1 -1
- data/node_modules/preact/test-utils/package.json +19 -19
- data/node_modules/preact/test-utils/src/index.d.ts +3 -3
- data/node_modules/preact/test-utils/src/index.js +117 -117
- data/package.json +1 -1
- metadata +8 -8
- data/lib/preact/component/props.rb +0 -55
- data/lib/preact/component/state.rb +0 -58
@@ -1,335 +1,335 @@
|
|
1
|
-
import { diff, unmount, applyRef } from './index';
|
2
|
-
import { createVNode, Fragment } from '../create-element';
|
3
|
-
import { EMPTY_OBJ, EMPTY_ARR } from '../constants';
|
4
|
-
import { getDomSibling } from '../component';
|
5
|
-
|
6
|
-
/**
|
7
|
-
* Diff the children of a virtual node
|
8
|
-
* @param {import('../internal').PreactElement} parentDom The DOM element whose
|
9
|
-
* children are being diffed
|
10
|
-
* @param {import('../internal').ComponentChildren[]} renderResult
|
11
|
-
* @param {import('../internal').VNode} newParentVNode The new virtual
|
12
|
-
* node whose children should be diff'ed against oldParentVNode
|
13
|
-
* @param {import('../internal').VNode} oldParentVNode The old virtual
|
14
|
-
* node whose children should be diff'ed against newParentVNode
|
15
|
-
* @param {object} globalContext The current context object - modified by getChildContext
|
16
|
-
* @param {boolean} isSvg Whether or not this DOM node is an SVG node
|
17
|
-
* @param {Array<import('../internal').PreactElement>} excessDomChildren
|
18
|
-
* @param {Array<import('../internal').Component>} commitQueue List of components
|
19
|
-
* which have callbacks to invoke in commitRoot
|
20
|
-
* @param {import('../internal').PreactElement} oldDom The current attached DOM
|
21
|
-
* element any new dom elements should be placed around. Likely `null` on first
|
22
|
-
* render (except when hydrating). Can be a sibling DOM element when diffing
|
23
|
-
* Fragments that have siblings. In most cases, it starts out as `oldChildren[0]._dom`.
|
24
|
-
* @param {boolean} isHydrating Whether or not we are in hydration
|
25
|
-
*/
|
26
|
-
export function diffChildren(
|
27
|
-
parentDom,
|
28
|
-
renderResult,
|
29
|
-
newParentVNode,
|
30
|
-
oldParentVNode,
|
31
|
-
globalContext,
|
32
|
-
isSvg,
|
33
|
-
excessDomChildren,
|
34
|
-
commitQueue,
|
35
|
-
oldDom,
|
36
|
-
isHydrating
|
37
|
-
) {
|
38
|
-
let i, j, oldVNode, childVNode, newDom, firstChildDom, refs;
|
39
|
-
|
40
|
-
// This is a compression of oldParentVNode!=null && oldParentVNode != EMPTY_OBJ && oldParentVNode._children || EMPTY_ARR
|
41
|
-
// as EMPTY_OBJ._children should be `undefined`.
|
42
|
-
let oldChildren = (oldParentVNode && oldParentVNode._children) || EMPTY_ARR;
|
43
|
-
|
44
|
-
let oldChildrenLength = oldChildren.length;
|
45
|
-
|
46
|
-
newParentVNode._children = [];
|
47
|
-
for (i = 0; i < renderResult.length; i++) {
|
48
|
-
childVNode = renderResult[i];
|
49
|
-
|
50
|
-
if (childVNode == null || typeof childVNode == 'boolean') {
|
51
|
-
childVNode = newParentVNode._children[i] = null;
|
52
|
-
}
|
53
|
-
// If this newVNode is being reused (e.g. <div>{reuse}{reuse}</div>) in the same diff,
|
54
|
-
// or we are rendering a component (e.g. setState) copy the oldVNodes so it can have
|
55
|
-
// it's own DOM & etc. pointers
|
56
|
-
else if (
|
57
|
-
typeof childVNode == 'string' ||
|
58
|
-
typeof childVNode == 'number' ||
|
59
|
-
// eslint-disable-next-line valid-typeof
|
60
|
-
typeof childVNode == 'bigint'
|
61
|
-
) {
|
62
|
-
childVNode = newParentVNode._children[i] = createVNode(
|
63
|
-
null,
|
64
|
-
childVNode,
|
65
|
-
null,
|
66
|
-
null,
|
67
|
-
childVNode
|
68
|
-
);
|
69
|
-
} else if (Array.isArray(childVNode)) {
|
70
|
-
childVNode = newParentVNode._children[i] = createVNode(
|
71
|
-
Fragment,
|
72
|
-
{ children: childVNode },
|
73
|
-
null,
|
74
|
-
null,
|
75
|
-
null
|
76
|
-
);
|
77
|
-
} else if (childVNode._depth > 0) {
|
78
|
-
// VNode is already in use, clone it. This can happen in the following
|
79
|
-
// scenario:
|
80
|
-
// const reuse = <div />
|
81
|
-
// <div>{reuse}<span />{reuse}</div>
|
82
|
-
childVNode = newParentVNode._children[i] = createVNode(
|
83
|
-
childVNode.type,
|
84
|
-
childVNode.props,
|
85
|
-
childVNode.key,
|
86
|
-
null,
|
87
|
-
childVNode._original
|
88
|
-
);
|
89
|
-
} else {
|
90
|
-
childVNode = newParentVNode._children[i] = childVNode;
|
91
|
-
}
|
92
|
-
|
93
|
-
// Terser removes the `continue` here and wraps the loop body
|
94
|
-
// in a `if (childVNode) { ... } condition
|
95
|
-
if (childVNode == null) {
|
96
|
-
continue;
|
97
|
-
}
|
98
|
-
|
99
|
-
childVNode._parent = newParentVNode;
|
100
|
-
childVNode._depth = newParentVNode._depth + 1;
|
101
|
-
|
102
|
-
// Check if we find a corresponding element in oldChildren.
|
103
|
-
// If found, delete the array item by setting to `undefined`.
|
104
|
-
// We use `undefined`, as `null` is reserved for empty placeholders
|
105
|
-
// (holes).
|
106
|
-
oldVNode = oldChildren[i];
|
107
|
-
|
108
|
-
if (
|
109
|
-
oldVNode === null ||
|
110
|
-
(oldVNode &&
|
111
|
-
childVNode.key == oldVNode.key &&
|
112
|
-
childVNode.type === oldVNode.type)
|
113
|
-
) {
|
114
|
-
oldChildren[i] = undefined;
|
115
|
-
} else {
|
116
|
-
// Either oldVNode === undefined or oldChildrenLength > 0,
|
117
|
-
// so after this loop oldVNode == null or oldVNode is a valid value.
|
118
|
-
for (j = 0; j < oldChildrenLength; j++) {
|
119
|
-
oldVNode = oldChildren[j];
|
120
|
-
// If childVNode is unkeyed, we only match similarly unkeyed nodes, otherwise we match by key.
|
121
|
-
// We always match by type (in either case).
|
122
|
-
if (
|
123
|
-
oldVNode &&
|
124
|
-
childVNode.key == oldVNode.key &&
|
125
|
-
childVNode.type === oldVNode.type
|
126
|
-
) {
|
127
|
-
oldChildren[j] = undefined;
|
128
|
-
break;
|
129
|
-
}
|
130
|
-
oldVNode = null;
|
131
|
-
}
|
132
|
-
}
|
133
|
-
|
134
|
-
oldVNode = oldVNode || EMPTY_OBJ;
|
135
|
-
|
136
|
-
// Morph the old element into the new one, but don't append it to the dom yet
|
137
|
-
diff(
|
138
|
-
parentDom,
|
139
|
-
childVNode,
|
140
|
-
oldVNode,
|
141
|
-
globalContext,
|
142
|
-
isSvg,
|
143
|
-
excessDomChildren,
|
144
|
-
commitQueue,
|
145
|
-
oldDom,
|
146
|
-
isHydrating
|
147
|
-
);
|
148
|
-
|
149
|
-
newDom = childVNode._dom;
|
150
|
-
|
151
|
-
if ((j = childVNode.ref) && oldVNode.ref != j) {
|
152
|
-
if (!refs) refs = [];
|
153
|
-
if (oldVNode.ref) refs.push(oldVNode.ref, null, childVNode);
|
154
|
-
refs.push(j, childVNode._component || newDom, childVNode);
|
155
|
-
}
|
156
|
-
|
157
|
-
if (newDom != null) {
|
158
|
-
if (firstChildDom == null) {
|
159
|
-
firstChildDom = newDom;
|
160
|
-
}
|
161
|
-
|
162
|
-
if (
|
163
|
-
typeof childVNode.type == 'function' &&
|
164
|
-
childVNode._children === oldVNode._children
|
165
|
-
) {
|
166
|
-
childVNode._nextDom = oldDom = reorderChildren(
|
167
|
-
childVNode,
|
168
|
-
oldDom,
|
169
|
-
parentDom
|
170
|
-
);
|
171
|
-
} else {
|
172
|
-
oldDom = placeChild(
|
173
|
-
parentDom,
|
174
|
-
childVNode,
|
175
|
-
oldVNode,
|
176
|
-
oldChildren,
|
177
|
-
newDom,
|
178
|
-
oldDom
|
179
|
-
);
|
180
|
-
}
|
181
|
-
|
182
|
-
if (typeof newParentVNode.type == 'function') {
|
183
|
-
// Because the newParentVNode is Fragment-like, we need to set it's
|
184
|
-
// _nextDom property to the nextSibling of its last child DOM node.
|
185
|
-
//
|
186
|
-
// `oldDom` contains the correct value here because if the last child
|
187
|
-
// is a Fragment-like, then oldDom has already been set to that child's _nextDom.
|
188
|
-
// If the last child is a DOM VNode, then oldDom will be set to that DOM
|
189
|
-
// node's nextSibling.
|
190
|
-
newParentVNode._nextDom = oldDom;
|
191
|
-
}
|
192
|
-
} else if (
|
193
|
-
oldDom &&
|
194
|
-
oldVNode._dom == oldDom &&
|
195
|
-
oldDom.parentNode != parentDom
|
196
|
-
) {
|
197
|
-
// The above condition is to handle null placeholders. See test in placeholder.test.js:
|
198
|
-
// `efficiently replace null placeholders in parent rerenders`
|
199
|
-
oldDom = getDomSibling(oldVNode);
|
200
|
-
}
|
201
|
-
}
|
202
|
-
|
203
|
-
newParentVNode._dom = firstChildDom;
|
204
|
-
|
205
|
-
// Remove remaining oldChildren if there are any.
|
206
|
-
for (i = oldChildrenLength; i--; ) {
|
207
|
-
if (oldChildren[i] != null) {
|
208
|
-
if (
|
209
|
-
typeof newParentVNode.type == 'function' &&
|
210
|
-
oldChildren[i]._dom != null &&
|
211
|
-
oldChildren[i]._dom == newParentVNode._nextDom
|
212
|
-
) {
|
213
|
-
// If the newParentVNode.__nextDom points to a dom node that is about to
|
214
|
-
// be unmounted, then get the next sibling of that vnode and set
|
215
|
-
// _nextDom to it
|
216
|
-
newParentVNode._nextDom = getDomSibling(oldParentVNode, i + 1);
|
217
|
-
}
|
218
|
-
|
219
|
-
unmount(oldChildren[i], oldChildren[i]);
|
220
|
-
}
|
221
|
-
}
|
222
|
-
|
223
|
-
// Set refs only after unmount
|
224
|
-
if (refs) {
|
225
|
-
for (i = 0; i < refs.length; i++) {
|
226
|
-
applyRef(refs[i], refs[++i], refs[++i]);
|
227
|
-
}
|
228
|
-
}
|
229
|
-
}
|
230
|
-
|
231
|
-
function reorderChildren(childVNode, oldDom, parentDom) {
|
232
|
-
// Note: VNodes in nested suspended trees may be missing _children.
|
233
|
-
let c = childVNode._children;
|
234
|
-
let tmp = 0;
|
235
|
-
for (; c && tmp < c.length; tmp++) {
|
236
|
-
let vnode = c[tmp];
|
237
|
-
if (vnode) {
|
238
|
-
// We typically enter this code path on sCU bailout, where we copy
|
239
|
-
// oldVNode._children to newVNode._children. If that is the case, we need
|
240
|
-
// to update the old children's _parent pointer to point to the newVNode
|
241
|
-
// (childVNode here).
|
242
|
-
vnode._parent = childVNode;
|
243
|
-
|
244
|
-
if (typeof vnode.type == 'function') {
|
245
|
-
oldDom = reorderChildren(vnode, oldDom, parentDom);
|
246
|
-
} else {
|
247
|
-
oldDom = placeChild(
|
248
|
-
parentDom,
|
249
|
-
vnode,
|
250
|
-
vnode,
|
251
|
-
c,
|
252
|
-
vnode._dom,
|
253
|
-
oldDom
|
254
|
-
);
|
255
|
-
}
|
256
|
-
}
|
257
|
-
}
|
258
|
-
|
259
|
-
return oldDom;
|
260
|
-
}
|
261
|
-
|
262
|
-
/**
|
263
|
-
* Flatten and loop through the children of a virtual node
|
264
|
-
* @param {import('../index').ComponentChildren} children The unflattened
|
265
|
-
* children of a virtual node
|
266
|
-
* @returns {import('../internal').VNode[]}
|
267
|
-
*/
|
268
|
-
export function toChildArray(children, out) {
|
269
|
-
out = out || [];
|
270
|
-
if (children == null || typeof children == 'boolean') {
|
271
|
-
} else if (Array.isArray(children)) {
|
272
|
-
children.some(child => {
|
273
|
-
toChildArray(child, out);
|
274
|
-
});
|
275
|
-
} else {
|
276
|
-
out.push(children);
|
277
|
-
}
|
278
|
-
return out;
|
279
|
-
}
|
280
|
-
|
281
|
-
function placeChild(
|
282
|
-
parentDom,
|
283
|
-
childVNode,
|
284
|
-
oldVNode,
|
285
|
-
oldChildren,
|
286
|
-
newDom,
|
287
|
-
oldDom
|
288
|
-
) {
|
289
|
-
let nextDom;
|
290
|
-
if (childVNode._nextDom !== undefined) {
|
291
|
-
// Only Fragments or components that return Fragment like VNodes will
|
292
|
-
// have a non-undefined _nextDom. Continue the diff from the sibling
|
293
|
-
// of last DOM child of this child VNode
|
294
|
-
nextDom = childVNode._nextDom;
|
295
|
-
|
296
|
-
// Eagerly cleanup _nextDom. We don't need to persist the value because
|
297
|
-
// it is only used by `diffChildren` to determine where to resume the diff after
|
298
|
-
// diffing Components and Fragments. Once we store it the nextDOM local var, we
|
299
|
-
// can clean up the property
|
300
|
-
childVNode._nextDom = undefined;
|
301
|
-
} else if (
|
302
|
-
oldVNode == null ||
|
303
|
-
newDom != oldDom ||
|
304
|
-
newDom.parentNode == null
|
305
|
-
) {
|
306
|
-
outer: if (oldDom == null || oldDom.parentNode !== parentDom) {
|
307
|
-
parentDom.appendChild(newDom);
|
308
|
-
nextDom = null;
|
309
|
-
} else {
|
310
|
-
// `j<oldChildrenLength; j+=2` is an alternative to `j++<oldChildrenLength/2`
|
311
|
-
for (
|
312
|
-
let sibDom = oldDom, j = 0;
|
313
|
-
(sibDom = sibDom.nextSibling) && j < oldChildren.length;
|
314
|
-
j += 2
|
315
|
-
) {
|
316
|
-
if (sibDom == newDom) {
|
317
|
-
break outer;
|
318
|
-
}
|
319
|
-
}
|
320
|
-
parentDom.insertBefore(newDom, oldDom);
|
321
|
-
nextDom = oldDom;
|
322
|
-
}
|
323
|
-
}
|
324
|
-
|
325
|
-
// If we have pre-calculated the nextDOM node, use it. Else calculate it now
|
326
|
-
// Strictly check for `undefined` here cuz `null` is a valid value of `nextDom`.
|
327
|
-
// See more detail in create-element.js:createVNode
|
328
|
-
if (nextDom !== undefined) {
|
329
|
-
oldDom = nextDom;
|
330
|
-
} else {
|
331
|
-
oldDom = newDom.nextSibling;
|
332
|
-
}
|
333
|
-
|
334
|
-
return oldDom;
|
335
|
-
}
|
1
|
+
import { diff, unmount, applyRef } from './index';
|
2
|
+
import { createVNode, Fragment } from '../create-element';
|
3
|
+
import { EMPTY_OBJ, EMPTY_ARR } from '../constants';
|
4
|
+
import { getDomSibling } from '../component';
|
5
|
+
|
6
|
+
/**
|
7
|
+
* Diff the children of a virtual node
|
8
|
+
* @param {import('../internal').PreactElement} parentDom The DOM element whose
|
9
|
+
* children are being diffed
|
10
|
+
* @param {import('../internal').ComponentChildren[]} renderResult
|
11
|
+
* @param {import('../internal').VNode} newParentVNode The new virtual
|
12
|
+
* node whose children should be diff'ed against oldParentVNode
|
13
|
+
* @param {import('../internal').VNode} oldParentVNode The old virtual
|
14
|
+
* node whose children should be diff'ed against newParentVNode
|
15
|
+
* @param {object} globalContext The current context object - modified by getChildContext
|
16
|
+
* @param {boolean} isSvg Whether or not this DOM node is an SVG node
|
17
|
+
* @param {Array<import('../internal').PreactElement>} excessDomChildren
|
18
|
+
* @param {Array<import('../internal').Component>} commitQueue List of components
|
19
|
+
* which have callbacks to invoke in commitRoot
|
20
|
+
* @param {import('../internal').PreactElement} oldDom The current attached DOM
|
21
|
+
* element any new dom elements should be placed around. Likely `null` on first
|
22
|
+
* render (except when hydrating). Can be a sibling DOM element when diffing
|
23
|
+
* Fragments that have siblings. In most cases, it starts out as `oldChildren[0]._dom`.
|
24
|
+
* @param {boolean} isHydrating Whether or not we are in hydration
|
25
|
+
*/
|
26
|
+
export function diffChildren(
|
27
|
+
parentDom,
|
28
|
+
renderResult,
|
29
|
+
newParentVNode,
|
30
|
+
oldParentVNode,
|
31
|
+
globalContext,
|
32
|
+
isSvg,
|
33
|
+
excessDomChildren,
|
34
|
+
commitQueue,
|
35
|
+
oldDom,
|
36
|
+
isHydrating
|
37
|
+
) {
|
38
|
+
let i, j, oldVNode, childVNode, newDom, firstChildDom, refs;
|
39
|
+
|
40
|
+
// This is a compression of oldParentVNode!=null && oldParentVNode != EMPTY_OBJ && oldParentVNode._children || EMPTY_ARR
|
41
|
+
// as EMPTY_OBJ._children should be `undefined`.
|
42
|
+
let oldChildren = (oldParentVNode && oldParentVNode._children) || EMPTY_ARR;
|
43
|
+
|
44
|
+
let oldChildrenLength = oldChildren.length;
|
45
|
+
|
46
|
+
newParentVNode._children = [];
|
47
|
+
for (i = 0; i < renderResult.length; i++) {
|
48
|
+
childVNode = renderResult[i];
|
49
|
+
|
50
|
+
if (childVNode == null || typeof childVNode == 'boolean') {
|
51
|
+
childVNode = newParentVNode._children[i] = null;
|
52
|
+
}
|
53
|
+
// If this newVNode is being reused (e.g. <div>{reuse}{reuse}</div>) in the same diff,
|
54
|
+
// or we are rendering a component (e.g. setState) copy the oldVNodes so it can have
|
55
|
+
// it's own DOM & etc. pointers
|
56
|
+
else if (
|
57
|
+
typeof childVNode == 'string' ||
|
58
|
+
typeof childVNode == 'number' ||
|
59
|
+
// eslint-disable-next-line valid-typeof
|
60
|
+
typeof childVNode == 'bigint'
|
61
|
+
) {
|
62
|
+
childVNode = newParentVNode._children[i] = createVNode(
|
63
|
+
null,
|
64
|
+
childVNode,
|
65
|
+
null,
|
66
|
+
null,
|
67
|
+
childVNode
|
68
|
+
);
|
69
|
+
} else if (Array.isArray(childVNode)) {
|
70
|
+
childVNode = newParentVNode._children[i] = createVNode(
|
71
|
+
Fragment,
|
72
|
+
{ children: childVNode },
|
73
|
+
null,
|
74
|
+
null,
|
75
|
+
null
|
76
|
+
);
|
77
|
+
} else if (childVNode._depth > 0) {
|
78
|
+
// VNode is already in use, clone it. This can happen in the following
|
79
|
+
// scenario:
|
80
|
+
// const reuse = <div />
|
81
|
+
// <div>{reuse}<span />{reuse}</div>
|
82
|
+
childVNode = newParentVNode._children[i] = createVNode(
|
83
|
+
childVNode.type,
|
84
|
+
childVNode.props,
|
85
|
+
childVNode.key,
|
86
|
+
null,
|
87
|
+
childVNode._original
|
88
|
+
);
|
89
|
+
} else {
|
90
|
+
childVNode = newParentVNode._children[i] = childVNode;
|
91
|
+
}
|
92
|
+
|
93
|
+
// Terser removes the `continue` here and wraps the loop body
|
94
|
+
// in a `if (childVNode) { ... } condition
|
95
|
+
if (childVNode == null) {
|
96
|
+
continue;
|
97
|
+
}
|
98
|
+
|
99
|
+
childVNode._parent = newParentVNode;
|
100
|
+
childVNode._depth = newParentVNode._depth + 1;
|
101
|
+
|
102
|
+
// Check if we find a corresponding element in oldChildren.
|
103
|
+
// If found, delete the array item by setting to `undefined`.
|
104
|
+
// We use `undefined`, as `null` is reserved for empty placeholders
|
105
|
+
// (holes).
|
106
|
+
oldVNode = oldChildren[i];
|
107
|
+
|
108
|
+
if (
|
109
|
+
oldVNode === null ||
|
110
|
+
(oldVNode &&
|
111
|
+
childVNode.key == oldVNode.key &&
|
112
|
+
childVNode.type === oldVNode.type)
|
113
|
+
) {
|
114
|
+
oldChildren[i] = undefined;
|
115
|
+
} else {
|
116
|
+
// Either oldVNode === undefined or oldChildrenLength > 0,
|
117
|
+
// so after this loop oldVNode == null or oldVNode is a valid value.
|
118
|
+
for (j = 0; j < oldChildrenLength; j++) {
|
119
|
+
oldVNode = oldChildren[j];
|
120
|
+
// If childVNode is unkeyed, we only match similarly unkeyed nodes, otherwise we match by key.
|
121
|
+
// We always match by type (in either case).
|
122
|
+
if (
|
123
|
+
oldVNode &&
|
124
|
+
childVNode.key == oldVNode.key &&
|
125
|
+
childVNode.type === oldVNode.type
|
126
|
+
) {
|
127
|
+
oldChildren[j] = undefined;
|
128
|
+
break;
|
129
|
+
}
|
130
|
+
oldVNode = null;
|
131
|
+
}
|
132
|
+
}
|
133
|
+
|
134
|
+
oldVNode = oldVNode || EMPTY_OBJ;
|
135
|
+
|
136
|
+
// Morph the old element into the new one, but don't append it to the dom yet
|
137
|
+
diff(
|
138
|
+
parentDom,
|
139
|
+
childVNode,
|
140
|
+
oldVNode,
|
141
|
+
globalContext,
|
142
|
+
isSvg,
|
143
|
+
excessDomChildren,
|
144
|
+
commitQueue,
|
145
|
+
oldDom,
|
146
|
+
isHydrating
|
147
|
+
);
|
148
|
+
|
149
|
+
newDom = childVNode._dom;
|
150
|
+
|
151
|
+
if ((j = childVNode.ref) && oldVNode.ref != j) {
|
152
|
+
if (!refs) refs = [];
|
153
|
+
if (oldVNode.ref) refs.push(oldVNode.ref, null, childVNode);
|
154
|
+
refs.push(j, childVNode._component || newDom, childVNode);
|
155
|
+
}
|
156
|
+
|
157
|
+
if (newDom != null) {
|
158
|
+
if (firstChildDom == null) {
|
159
|
+
firstChildDom = newDom;
|
160
|
+
}
|
161
|
+
|
162
|
+
if (
|
163
|
+
typeof childVNode.type == 'function' &&
|
164
|
+
childVNode._children === oldVNode._children
|
165
|
+
) {
|
166
|
+
childVNode._nextDom = oldDom = reorderChildren(
|
167
|
+
childVNode,
|
168
|
+
oldDom,
|
169
|
+
parentDom
|
170
|
+
);
|
171
|
+
} else {
|
172
|
+
oldDom = placeChild(
|
173
|
+
parentDom,
|
174
|
+
childVNode,
|
175
|
+
oldVNode,
|
176
|
+
oldChildren,
|
177
|
+
newDom,
|
178
|
+
oldDom
|
179
|
+
);
|
180
|
+
}
|
181
|
+
|
182
|
+
if (typeof newParentVNode.type == 'function') {
|
183
|
+
// Because the newParentVNode is Fragment-like, we need to set it's
|
184
|
+
// _nextDom property to the nextSibling of its last child DOM node.
|
185
|
+
//
|
186
|
+
// `oldDom` contains the correct value here because if the last child
|
187
|
+
// is a Fragment-like, then oldDom has already been set to that child's _nextDom.
|
188
|
+
// If the last child is a DOM VNode, then oldDom will be set to that DOM
|
189
|
+
// node's nextSibling.
|
190
|
+
newParentVNode._nextDom = oldDom;
|
191
|
+
}
|
192
|
+
} else if (
|
193
|
+
oldDom &&
|
194
|
+
oldVNode._dom == oldDom &&
|
195
|
+
oldDom.parentNode != parentDom
|
196
|
+
) {
|
197
|
+
// The above condition is to handle null placeholders. See test in placeholder.test.js:
|
198
|
+
// `efficiently replace null placeholders in parent rerenders`
|
199
|
+
oldDom = getDomSibling(oldVNode);
|
200
|
+
}
|
201
|
+
}
|
202
|
+
|
203
|
+
newParentVNode._dom = firstChildDom;
|
204
|
+
|
205
|
+
// Remove remaining oldChildren if there are any.
|
206
|
+
for (i = oldChildrenLength; i--; ) {
|
207
|
+
if (oldChildren[i] != null) {
|
208
|
+
if (
|
209
|
+
typeof newParentVNode.type == 'function' &&
|
210
|
+
oldChildren[i]._dom != null &&
|
211
|
+
oldChildren[i]._dom == newParentVNode._nextDom
|
212
|
+
) {
|
213
|
+
// If the newParentVNode.__nextDom points to a dom node that is about to
|
214
|
+
// be unmounted, then get the next sibling of that vnode and set
|
215
|
+
// _nextDom to it
|
216
|
+
newParentVNode._nextDom = getDomSibling(oldParentVNode, i + 1);
|
217
|
+
}
|
218
|
+
|
219
|
+
unmount(oldChildren[i], oldChildren[i]);
|
220
|
+
}
|
221
|
+
}
|
222
|
+
|
223
|
+
// Set refs only after unmount
|
224
|
+
if (refs) {
|
225
|
+
for (i = 0; i < refs.length; i++) {
|
226
|
+
applyRef(refs[i], refs[++i], refs[++i]);
|
227
|
+
}
|
228
|
+
}
|
229
|
+
}
|
230
|
+
|
231
|
+
function reorderChildren(childVNode, oldDom, parentDom) {
|
232
|
+
// Note: VNodes in nested suspended trees may be missing _children.
|
233
|
+
let c = childVNode._children;
|
234
|
+
let tmp = 0;
|
235
|
+
for (; c && tmp < c.length; tmp++) {
|
236
|
+
let vnode = c[tmp];
|
237
|
+
if (vnode) {
|
238
|
+
// We typically enter this code path on sCU bailout, where we copy
|
239
|
+
// oldVNode._children to newVNode._children. If that is the case, we need
|
240
|
+
// to update the old children's _parent pointer to point to the newVNode
|
241
|
+
// (childVNode here).
|
242
|
+
vnode._parent = childVNode;
|
243
|
+
|
244
|
+
if (typeof vnode.type == 'function') {
|
245
|
+
oldDom = reorderChildren(vnode, oldDom, parentDom);
|
246
|
+
} else {
|
247
|
+
oldDom = placeChild(
|
248
|
+
parentDom,
|
249
|
+
vnode,
|
250
|
+
vnode,
|
251
|
+
c,
|
252
|
+
vnode._dom,
|
253
|
+
oldDom
|
254
|
+
);
|
255
|
+
}
|
256
|
+
}
|
257
|
+
}
|
258
|
+
|
259
|
+
return oldDom;
|
260
|
+
}
|
261
|
+
|
262
|
+
/**
|
263
|
+
* Flatten and loop through the children of a virtual node
|
264
|
+
* @param {import('../index').ComponentChildren} children The unflattened
|
265
|
+
* children of a virtual node
|
266
|
+
* @returns {import('../internal').VNode[]}
|
267
|
+
*/
|
268
|
+
export function toChildArray(children, out) {
|
269
|
+
out = out || [];
|
270
|
+
if (children == null || typeof children == 'boolean') {
|
271
|
+
} else if (Array.isArray(children)) {
|
272
|
+
children.some(child => {
|
273
|
+
toChildArray(child, out);
|
274
|
+
});
|
275
|
+
} else {
|
276
|
+
out.push(children);
|
277
|
+
}
|
278
|
+
return out;
|
279
|
+
}
|
280
|
+
|
281
|
+
function placeChild(
|
282
|
+
parentDom,
|
283
|
+
childVNode,
|
284
|
+
oldVNode,
|
285
|
+
oldChildren,
|
286
|
+
newDom,
|
287
|
+
oldDom
|
288
|
+
) {
|
289
|
+
let nextDom;
|
290
|
+
if (childVNode._nextDom !== undefined) {
|
291
|
+
// Only Fragments or components that return Fragment like VNodes will
|
292
|
+
// have a non-undefined _nextDom. Continue the diff from the sibling
|
293
|
+
// of last DOM child of this child VNode
|
294
|
+
nextDom = childVNode._nextDom;
|
295
|
+
|
296
|
+
// Eagerly cleanup _nextDom. We don't need to persist the value because
|
297
|
+
// it is only used by `diffChildren` to determine where to resume the diff after
|
298
|
+
// diffing Components and Fragments. Once we store it the nextDOM local var, we
|
299
|
+
// can clean up the property
|
300
|
+
childVNode._nextDom = undefined;
|
301
|
+
} else if (
|
302
|
+
oldVNode == null ||
|
303
|
+
newDom != oldDom ||
|
304
|
+
newDom.parentNode == null
|
305
|
+
) {
|
306
|
+
outer: if (oldDom == null || oldDom.parentNode !== parentDom) {
|
307
|
+
parentDom.appendChild(newDom);
|
308
|
+
nextDom = null;
|
309
|
+
} else {
|
310
|
+
// `j<oldChildrenLength; j+=2` is an alternative to `j++<oldChildrenLength/2`
|
311
|
+
for (
|
312
|
+
let sibDom = oldDom, j = 0;
|
313
|
+
(sibDom = sibDom.nextSibling) && j < oldChildren.length;
|
314
|
+
j += 2
|
315
|
+
) {
|
316
|
+
if (sibDom == newDom) {
|
317
|
+
break outer;
|
318
|
+
}
|
319
|
+
}
|
320
|
+
parentDom.insertBefore(newDom, oldDom);
|
321
|
+
nextDom = oldDom;
|
322
|
+
}
|
323
|
+
}
|
324
|
+
|
325
|
+
// If we have pre-calculated the nextDOM node, use it. Else calculate it now
|
326
|
+
// Strictly check for `undefined` here cuz `null` is a valid value of `nextDom`.
|
327
|
+
// See more detail in create-element.js:createVNode
|
328
|
+
if (nextDom !== undefined) {
|
329
|
+
oldDom = nextDom;
|
330
|
+
} else {
|
331
|
+
oldDom = newDom.nextSibling;
|
332
|
+
}
|
333
|
+
|
334
|
+
return oldDom;
|
335
|
+
}
|