-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathcreate-root.js
66 lines (55 loc) · 1.63 KB
/
create-root.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
import {
MODE_HYDRATE,
MODE_MUTATIVE_HYDRATE,
MODE_SVG,
UNDEFINED
} from './constants';
import { commitRoot } from './diff/commit';
import { createElement, Fragment } from './create-element';
import options from './options';
import { mount } from './diff/mount';
import { patch } from './diff/patch';
import { createInternal } from './tree';
/**
*
* @param {import('./internal').PreactElement} parentDom The DOM element to
* @returns {import('./internal').Root}
*/
export function createRoot(parentDom) {
let rootInternal,
firstChild,
flags = 0;
function render(vnode) {
if (options._root) options._root(vnode, parentDom);
vnode = createElement(Fragment, { _parentDom: parentDom }, [vnode]);
firstChild =
/** @type {import('./internal').PreactElement} */ (parentDom.firstChild);
if (rootInternal) {
patch(rootInternal, vnode, parentDom);
} else {
rootInternal = createInternal(vnode);
// Store the VDOM tree root on the DOM element in a (minified) property:
parentDom._child = rootInternal;
// Calling createRoot().render() on an Element with existing children triggers mutative hydrate mode:
if (firstChild) {
flags = flags || MODE_MUTATIVE_HYDRATE;
}
// If the parent of this tree is within an inline SVG, the tree should start off in SVG mode:
if (parentDom.ownerSVGElement !== UNDEFINED) {
flags |= MODE_SVG;
}
rootInternal.flags |= flags;
rootInternal._context = {};
mount(rootInternal, vnode, parentDom, firstChild);
}
// Flush all queued effects
commitRoot(rootInternal);
}
return {
hydrate(vnode) {
flags |= MODE_HYDRATE;
render(vnode);
},
render
};
}