/
patch.js
320 lines (282 loc) · 8.94 KB
/
patch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { patchChildren, insertComponentDom } from './children';
import { setProperty } from './props';
import options from '../options';
import {
RESET_MODE,
TYPE_TEXT,
TYPE_ELEMENT,
MODE_SUSPENDED,
MODE_ERRORED,
TYPE_ROOT,
TYPE_CLASS,
MODE_SVG,
UNDEFINED,
MODE_HYDRATE,
MODE_PENDING_ERROR,
MODE_RERENDERING_ERROR,
SKIP_CHILDREN,
DIRTY_BIT,
FORCE_UPDATE
} from '../constants';
import { getDomSibling, getParentContext } from '../tree';
import { mountChildren } from './mount';
import { Fragment } from '../create-element';
import { ENABLE_CLASSES } from '../component';
import { commitQueue } from './commit';
/**
* Diff two virtual nodes and apply proper changes to the DOM
* @param {import('../internal').Internal} internal The Internal node to patch
* @param {import('../internal').VNode | string} vnode The new virtual node
* @param {import('../internal').PreactElement} parentDom The element into which this subtree is rendered
*/
export function patch(internal, vnode, parentDom) {
let flags = internal.flags;
if (flags & TYPE_TEXT) {
if (vnode !== internal.props) {
// @ts-ignore We know that newVNode is string/number/bigint, and internal._dom is Text
internal._dom.data = vnode;
internal.props = vnode;
}
return;
}
// When passing through createElement it assigns the object
// constructor as undefined. This to prevent JSON-injection.
if (vnode.constructor !== UNDEFINED) return;
if (options._diff) options._diff(internal, vnode);
// Root nodes render their children into a specific parent DOM element.
// They can occur anywhere in the tree, can be nested, and currently allow reparenting during patches.
// @TODO: Decide if we actually want to support silent reparenting during patch - is it worth the bytes?
let prevParentDom = parentDom;
if (flags & TYPE_ROOT) {
parentDom = vnode.props._parentDom;
if (internal.props._parentDom !== vnode.props._parentDom) {
let nextSibling =
parentDom == prevParentDom ? getDomSibling(internal) : null;
insertComponentDom(internal, nextSibling, parentDom);
}
}
if (flags & TYPE_ELEMENT) {
if (vnode._vnodeId !== internal._vnodeId) {
// @ts-ignore dom is a PreactElement here
patchElement(internal, vnode);
internal.props = vnode.props;
}
} else {
if (flags & MODE_PENDING_ERROR) {
// Toggle the MODE_PENDING_ERROR and MODE_RERENDERING_ERROR flags. In
// actuality, this should turn off the MODE_PENDING_ERROR flag and turn on
// the MODE_RERENDERING_ERROR flag.
internal.flags ^= MODE_PENDING_ERROR | MODE_RERENDERING_ERROR;
}
try {
let renderResult;
if (internal._vnodeId === vnode._vnodeId) {
internal.flags |= SKIP_CHILDREN;
} else {
renderResult = patchComponent(internal, vnode);
}
if (internal.flags & SKIP_CHILDREN) {
internal._component.props = internal.props = vnode.props;
if (vnode._vnodeId !== internal._vnodeId) {
internal.flags &= ~DIRTY_BIT;
}
} else if (internal._children == null) {
let siblingDom =
(internal.flags & (MODE_HYDRATE | MODE_SUSPENDED)) ===
(MODE_HYDRATE | MODE_SUSPENDED)
? internal._dom
: internal.flags & MODE_HYDRATE
? null
: getDomSibling(internal);
mountChildren(internal, renderResult, parentDom, siblingDom);
} else {
patchChildren(internal, renderResult, parentDom);
}
if (internal._commitCallbacks.length) {
commitQueue.push(internal);
}
} catch (e) {
// @TODO: assign a new VNode ID here? Or NaN?
// newVNode._vnodeId = 0;
internal.flags |= e.then ? MODE_SUSPENDED : MODE_ERRORED;
options._catchError(e, internal);
}
}
if (options.diffed) options.diffed(internal);
// We successfully rendered this VNode, unset any stored hydration/bailout state:
internal.flags &= RESET_MODE;
// Once we have successfully rendered the new VNode, copy it's ID over
internal._vnodeId = vnode._vnodeId;
internal._prevRef = internal.ref;
internal.ref = vnode.ref;
}
/**
* Update an internal and its associated DOM element based on a new VNode
* @param {import('../internal').Internal} internal
* @param {import('../internal').VNode} vnode A VNode with props to compare and apply
*/
function patchElement(internal, vnode) {
let dom = /** @type {import('../internal').PreactElement} */ (internal._dom),
oldProps = internal.props,
newProps = vnode.props,
isSvg = internal.flags & MODE_SVG,
i,
value,
tmp,
newHtml,
oldHtml,
newChildren;
for (i in oldProps) {
value = oldProps[i];
if (i === 'children') {
} else if (i === 'dangerouslySetInnerHTML') {
oldHtml = value;
} else if (!(i in newProps)) {
setProperty(dom, i, null, value, isSvg);
}
}
for (i in newProps) {
value = newProps[i];
if (i === 'children') {
newChildren = value;
} else if (i === 'dangerouslySetInnerHTML') {
newHtml = value;
} else if (
value !== (tmp = oldProps[i]) ||
((i === 'checked' || i === 'value') && value != null && value !== dom[i])
) {
setProperty(dom, i, value, tmp, isSvg);
}
}
// If the new vnode didn't have dangerouslySetInnerHTML, diff its children
if (newHtml) {
value = newHtml.__html;
// Avoid re-applying the same '__html' if it did not changed between re-render
if (!oldHtml || (value !== oldHtml.__html && value !== dom.innerHTML)) {
dom.innerHTML = value;
}
internal._children = null;
} else {
if (oldHtml) dom.innerHTML = '';
patchChildren(
internal,
newChildren && Array.isArray(newChildren) ? newChildren : [newChildren],
dom
);
}
}
/**
* @param {import('../internal').Internal} internal The component's backing Internal node
* @param {import('../internal').VNode} newVNode The new virtual node
* @returns {import('../internal').ComponentChildren} the component's children
*/
function patchComponent(internal, newVNode) {
let type = /** @type {import('../internal').ComponentType} */ (internal.type);
let snapshot,
c = internal._component,
newProps = newVNode.props;
// Necessary for createContext api. Setting this property will pass
// the context value as `this.context` just for this component.
let context = getParentContext(internal);
let tmp = newVNode.type.contextType;
let provider = tmp && context[tmp._id];
let componentContext = tmp
? provider
? provider.props.value
: tmp._defaultValue
: context;
if (c._nextState == null) {
c._nextState = c.state;
}
let oldProps = c.props;
let oldState = c.state;
if (ENABLE_CLASSES) {
if (type.getDerivedStateFromProps != null) {
if (c._nextState == c.state) {
c._nextState = Object.assign({}, c._nextState);
}
Object.assign(
c._nextState,
type.getDerivedStateFromProps(newProps, c._nextState)
);
}
if (
type.getDerivedStateFromProps == null &&
newProps !== oldProps &&
c.componentWillReceiveProps != null
) {
c.componentWillReceiveProps(newProps, componentContext);
}
}
if (
!(internal.flags & FORCE_UPDATE) &&
c.shouldComponentUpdate != null &&
c.shouldComponentUpdate(newProps, c._nextState, componentContext) === false
) {
c.props = newProps;
c.state = c._nextState;
internal.flags |= SKIP_CHILDREN;
for (let i = 0; i < internal._stateCallbacks.length; i++) {
internal._commitCallbacks.push(internal._stateCallbacks[i]);
}
internal._stateCallbacks = [];
return;
}
if (ENABLE_CLASSES && c.componentWillUpdate != null) {
c.componentWillUpdate(newProps, c._nextState, componentContext);
}
c.context = componentContext;
internal.props = c.props = newProps;
c.state = c._nextState;
let renderHook = options._render;
if (renderHook) renderHook(internal);
let counter = 0,
renderResult;
while (counter++ < 25) {
internal.flags &= ~DIRTY_BIT;
if (renderHook) renderHook(internal);
if (ENABLE_CLASSES && internal.flags & TYPE_CLASS) {
renderResult = c.render(c.props, c.state, c.context);
for (let i = 0; i < internal._stateCallbacks.length; i++) {
internal._commitCallbacks.push(internal._stateCallbacks[i]);
}
internal._stateCallbacks = [];
// note: disable repeat render invocation for class components
break;
} else {
renderResult = type.call(c, c.props, c.context);
}
if (!(internal.flags & DIRTY_BIT)) {
break;
}
}
// Handle setState called in render, see #2553
c.state = c._nextState;
if (c.getChildContext != null) {
internal._context = Object.assign({}, context, c.getChildContext());
}
if (ENABLE_CLASSES) {
if (c.getSnapshotBeforeUpdate != null) {
snapshot = c.getSnapshotBeforeUpdate(oldProps, oldState);
}
// Only schedule componentDidUpdate if the component successfully rendered
if (c.componentDidUpdate != null) {
internal._commitCallbacks.push(() => {
c.componentDidUpdate(oldProps, oldState, snapshot);
});
}
}
if (renderResult == null) {
renderResult = [];
} else if (typeof renderResult === 'object') {
if (renderResult.type === Fragment && renderResult.key == null) {
renderResult = renderResult.props.children;
}
if (!Array.isArray(renderResult)) {
renderResult = [renderResult];
}
} else {
renderResult = [renderResult];
}
return renderResult;
}