@simpreact/simpreact 0.0.0-alpha.dd6f145 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/context.d.ts +18 -0
- package/core/context.js +18 -0
- package/core/createElement.d.ts +24 -15
- package/core/createElement.js +93 -27
- package/core/fragment.js +1 -2
- package/core/hostAdapter.d.ts +19 -8
- package/core/hostAdapter.js +4 -1
- package/core/index.d.ts +68 -2
- package/core/index.js +6 -2
- package/core/internal.d.ts +7 -3
- package/core/internal.js +7 -3
- package/core/lifecycleEventBus.d.ts +26 -0
- package/core/lifecycleEventBus.js +2 -0
- package/core/mounting.d.ts +11 -7
- package/core/mounting.js +104 -38
- package/core/patching.d.ts +8 -5
- package/core/patching.js +157 -161
- package/core/portal.d.ts +2 -0
- package/core/portal.js +9 -0
- package/core/ref.d.ts +17 -0
- package/core/ref.js +27 -0
- package/core/rerender.d.ts +11 -0
- package/core/rerender.js +61 -3
- package/core/unmounting.d.ts +4 -6
- package/core/unmounting.js +33 -39
- package/dom/attach-element-to-dom.d.ts +4 -0
- package/dom/attach-element-to-dom.js +9 -0
- package/dom/domAdapter.d.ts +3 -2
- package/dom/domAdapter.js +42 -115
- package/dom/events.d.ts +19 -0
- package/dom/events.js +129 -0
- package/dom/index.d.ts +1733 -1
- package/dom/index.js +2 -0
- package/dom/namespace.d.ts +2 -0
- package/dom/namespace.js +1 -0
- package/dom/props/controlled/index.d.ts +7 -0
- package/dom/props/controlled/index.js +51 -0
- package/dom/props/controlled/input.d.ts +7 -0
- package/dom/props/controlled/input.js +80 -0
- package/dom/props/controlled/select.d.ts +6 -0
- package/dom/props/controlled/select.js +76 -0
- package/dom/props/controlled/textarea.d.ts +6 -0
- package/dom/props/controlled/textarea.js +61 -0
- package/dom/props/dangerInnerHTML.d.ts +7 -0
- package/dom/props/dangerInnerHTML.js +24 -0
- package/dom/props/index.d.ts +1 -0
- package/dom/props/index.js +1 -0
- package/dom/props/props.d.ts +5 -0
- package/dom/props/props.js +197 -0
- package/dom/props/style.d.ts +1 -0
- package/dom/props/style.js +32 -0
- package/dom/render.d.ts +3 -3
- package/dom/render.js +31 -17
- package/hooks/index.d.ts +23 -10
- package/hooks/index.js +126 -32
- package/jsx-runtime/index.d.ts +247 -4
- package/jsx-runtime/index.js +3 -6
- package/package.json +22 -15
- package/shared/EventBus.js +1 -3
- package/shared/index.d.ts +19 -4
- package/shared/index.js +5 -4
- package/shared/lang.d.ts +3 -4
- package/shared/lang.js +3 -4
- package/shared/utils.d.ts +3 -3
- package/shared/utils.js +3 -7
- package/core/global.d.ts +0 -21
- package/core/global.js +0 -5
- package/core/jsx-runtime.d.ts +0 -2
- package/core/jsx-runtime.js +0 -2
- package/shared/types.d.ts +0 -8
- package/shared/types.js +0 -1
package/core/mounting.js
CHANGED
|
@@ -1,66 +1,132 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { normalizeRoot } from './createElement';
|
|
4
|
-
|
|
1
|
+
import { emptyMap, emptyObject } from '../shared';
|
|
2
|
+
import { hostAdapter } from './hostAdapter';
|
|
3
|
+
import { createTextElement, normalizeRoot } from './createElement';
|
|
4
|
+
import { applyRef } from './ref';
|
|
5
|
+
import { lifecycleEventBus } from './lifecycleEventBus';
|
|
6
|
+
export function mount(element, parentReference, nextReference, contextMap, hostNamespace) {
|
|
5
7
|
if (element.flag === 'TEXT') {
|
|
6
8
|
mountTextElement(element, parentReference, nextReference);
|
|
7
9
|
}
|
|
8
10
|
else if (element.flag === 'HOST') {
|
|
9
|
-
mountHostElement(element, parentReference, nextReference);
|
|
11
|
+
mountHostElement(element, parentReference, nextReference, contextMap, hostNamespace);
|
|
10
12
|
}
|
|
11
13
|
else if (element.flag === 'FC') {
|
|
12
|
-
mountFunctionalElement(element, parentReference, nextReference);
|
|
14
|
+
mountFunctionalElement(element, parentReference, nextReference, contextMap, hostNamespace);
|
|
13
15
|
}
|
|
14
16
|
else if (element.flag === 'FRAGMENT') {
|
|
15
|
-
mountFragment(element, parentReference, nextReference);
|
|
17
|
+
mountFragment(element, parentReference, nextReference, contextMap, hostNamespace);
|
|
18
|
+
}
|
|
19
|
+
else if (element.flag === 'PROVIDER') {
|
|
20
|
+
mountProvider(element, parentReference, nextReference, contextMap, hostNamespace);
|
|
21
|
+
}
|
|
22
|
+
else if (element.flag === 'PORTAL') {
|
|
23
|
+
mountPortal(element, parentReference, nextReference, contextMap);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
mountConsumer(element, parentReference, nextReference, contextMap, hostNamespace);
|
|
16
27
|
}
|
|
17
28
|
}
|
|
18
29
|
export function mountTextElement(element, parentReference, nextReference) {
|
|
19
|
-
const reference = (element.reference
|
|
20
|
-
if (parentReference
|
|
21
|
-
|
|
30
|
+
const reference = (element.reference = hostAdapter.createTextReference(element.children));
|
|
31
|
+
if (parentReference) {
|
|
32
|
+
hostAdapter.insertOrAppend(parentReference, reference, nextReference);
|
|
22
33
|
}
|
|
23
34
|
}
|
|
24
|
-
export function mountHostElement(element, parentReference, nextReference) {
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
const hostReference = (element.reference =
|
|
35
|
+
export function mountHostElement(element, parentReference, nextReference, contextMap, hostNamespace) {
|
|
36
|
+
const hostNamespaces = hostAdapter.getHostNamespaces(element, hostNamespace);
|
|
37
|
+
hostNamespace = hostNamespaces?.self;
|
|
38
|
+
const hostReference = (element.reference = hostAdapter.createReference(element.type, hostNamespace));
|
|
39
|
+
hostAdapter.attachElementToReference(element, hostReference);
|
|
40
|
+
if (parentReference) {
|
|
41
|
+
hostAdapter.insertOrAppend(parentReference, hostReference, nextReference);
|
|
42
|
+
}
|
|
28
43
|
// HOST element always has Maybe<Many<SimpElement>> children due to normalization process.
|
|
29
44
|
const children = element.children;
|
|
30
|
-
if (className != null && className !== '') {
|
|
31
|
-
GLOBAL.hostAdapter.setClassname(hostReference, className);
|
|
32
|
-
}
|
|
33
45
|
if (Array.isArray(children)) {
|
|
34
|
-
mountArrayChildren(children, hostReference, null);
|
|
46
|
+
mountArrayChildren(children, hostReference, null, contextMap, element, hostNamespaces?.children);
|
|
47
|
+
}
|
|
48
|
+
else if (children) {
|
|
49
|
+
children.parent = element;
|
|
50
|
+
mount(children, hostReference, null, contextMap, hostNamespaces?.children);
|
|
35
51
|
}
|
|
36
|
-
|
|
37
|
-
|
|
52
|
+
if (element.props) {
|
|
53
|
+
hostAdapter.mountProps(hostReference, element, hostNamespace);
|
|
38
54
|
}
|
|
39
|
-
if (
|
|
40
|
-
|
|
55
|
+
if (element.className) {
|
|
56
|
+
hostAdapter.setClassname(hostReference, element.className, hostNamespace);
|
|
41
57
|
}
|
|
42
|
-
|
|
58
|
+
applyRef(element);
|
|
43
59
|
}
|
|
44
|
-
export function mountFunctionalElement(element, parentReference, nextReference) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
60
|
+
export function mountFunctionalElement(element, parentReference, nextReference, contextMap, hostNamespace) {
|
|
61
|
+
if (contextMap) {
|
|
62
|
+
element.contextMap = contextMap;
|
|
63
|
+
}
|
|
64
|
+
if (element.unmounted) {
|
|
65
|
+
element.unmounted = false;
|
|
66
|
+
}
|
|
67
|
+
element.store = { latestElement: element };
|
|
68
|
+
if (hostNamespace) {
|
|
69
|
+
element.store.hostNamespace = hostNamespace;
|
|
70
|
+
}
|
|
71
|
+
// FC element always has Maybe<SimpElement> children due to normalization process.
|
|
72
|
+
let children;
|
|
73
|
+
try {
|
|
74
|
+
lifecycleEventBus.publish({ type: 'beforeRender', element, phase: 'mounting' });
|
|
75
|
+
children = normalizeRoot(element.type(element.props || emptyObject), false);
|
|
76
|
+
lifecycleEventBus.publish({ type: 'afterRender', phase: 'mounting' });
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
lifecycleEventBus.publish({ type: 'errored', element, error, phase: 'mounting' });
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (children) {
|
|
83
|
+
children.parent = element;
|
|
84
|
+
mount((element.children = children), parentReference, nextReference, contextMap, hostNamespace);
|
|
85
|
+
}
|
|
86
|
+
lifecycleEventBus.publish({ type: 'mounted', element });
|
|
51
87
|
}
|
|
52
|
-
export function mountFragment(element, parentReference, nextReference) {
|
|
88
|
+
export function mountFragment(element, parentReference, nextReference, contextMap, hostNamespace) {
|
|
53
89
|
// FRAGMENT element always has Maybe<Many<SimpElement>> children due to normalization process.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
mountArrayChildren(children, parentReference, nextReference);
|
|
90
|
+
if (Array.isArray(element.children)) {
|
|
91
|
+
mountArrayChildren(element.children, parentReference, nextReference, contextMap, element, hostNamespace);
|
|
57
92
|
}
|
|
58
|
-
else if (children
|
|
59
|
-
|
|
93
|
+
else if (element.children) {
|
|
94
|
+
element.children.parent = element;
|
|
95
|
+
mount(element.children, parentReference, nextReference, contextMap, hostNamespace);
|
|
60
96
|
}
|
|
61
97
|
}
|
|
62
|
-
export function mountArrayChildren(children, reference, nextReference) {
|
|
98
|
+
export function mountArrayChildren(children, reference, nextReference, contextMap, parentElement, hostNamespace) {
|
|
63
99
|
for (const child of children) {
|
|
64
|
-
|
|
100
|
+
child.parent = parentElement;
|
|
101
|
+
mount(child, reference, nextReference, contextMap, hostNamespace);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export function mountProvider(element, parentReference, nextReference, contextMap, hostNamespace) {
|
|
105
|
+
contextMap = new Map(contextMap);
|
|
106
|
+
contextMap.set(element.type.context, element.props.value);
|
|
107
|
+
// PROVIDER element always has Maybe<Many<SimpElement>> children due to normalization process.
|
|
108
|
+
if (Array.isArray(element.children)) {
|
|
109
|
+
mountArrayChildren(element.children, parentReference, nextReference, contextMap, element, hostNamespace);
|
|
110
|
+
}
|
|
111
|
+
else if (element.children) {
|
|
112
|
+
element.children.parent = element;
|
|
113
|
+
mount(element.children, parentReference, nextReference, contextMap, hostNamespace);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
export function mountConsumer(element, parentReference, nextReference, contextMap, hostNamespace) {
|
|
117
|
+
const children = normalizeRoot(element.type(element.props || emptyObject, contextMap || emptyMap), false);
|
|
118
|
+
if (!children) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
children.parent = element;
|
|
122
|
+
mount((element.children = children), parentReference, nextReference, contextMap, hostNamespace);
|
|
123
|
+
}
|
|
124
|
+
export function mountPortal(element, parentReference, nextReference, contextMap) {
|
|
125
|
+
if (element.children) {
|
|
126
|
+
element.children.parent = element;
|
|
127
|
+
mount(element.children, element.ref, null, contextMap, hostAdapter.getHostNamespaces(element.children, undefined)?.self);
|
|
65
128
|
}
|
|
129
|
+
const placeHolderElement = createTextElement('');
|
|
130
|
+
mountTextElement(placeHolderElement, parentReference, nextReference);
|
|
131
|
+
element.reference = placeHolderElement.reference;
|
|
66
132
|
}
|
package/core/patching.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import type { Maybe, Nullable } from '../shared';
|
|
1
2
|
import type { SimpElement } from './createElement';
|
|
2
|
-
import type { Nullable } from '../shared';
|
|
3
3
|
import type { HostReference } from './hostAdapter';
|
|
4
|
-
|
|
5
|
-
export declare function
|
|
6
|
-
export declare function
|
|
7
|
-
export declare function
|
|
4
|
+
import type { SimpContextMap } from './context';
|
|
5
|
+
export declare function patch(prevElement: SimpElement, nextElement: SimpElement, parentReference: HostReference, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
|
|
6
|
+
export declare function patchPortal(prevElement: SimpElement, nextElement: SimpElement, contextMap: Nullable<SimpContextMap>): void;
|
|
7
|
+
export declare function updateFunctionalComponent(element: SimpElement, parentReference: HostReference, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
|
|
8
|
+
export declare function patchKeyedChildren(prevChildren: SimpElement[], nextChildren: SimpElement[], parentReference: HostReference, nextReference: Nullable<HostReference>, contextMap: Nullable<SimpContextMap>, hostNamespace: Maybe<string>): void;
|
|
9
|
+
export declare function findParentReferenceFromElement(element: SimpElement): Nullable<HostReference>;
|
|
10
|
+
export declare function findHostReferenceFromElement(element: SimpElement): Nullable<HostReference>;
|
package/core/patching.js
CHANGED
|
@@ -1,200 +1,171 @@
|
|
|
1
|
+
import { emptyMap, emptyObject } from '../shared';
|
|
1
2
|
import { normalizeRoot } from './createElement';
|
|
2
|
-
import {
|
|
3
|
-
import { clearElementHostReference, remove,
|
|
3
|
+
import { hostAdapter } from './hostAdapter';
|
|
4
|
+
import { clearElementHostReference, remove, unmount } from './unmounting';
|
|
4
5
|
import { mount, mountArrayChildren } from './mounting';
|
|
5
|
-
import {
|
|
6
|
-
|
|
6
|
+
import { applyRef } from './ref';
|
|
7
|
+
import { lifecycleEventBus } from './lifecycleEventBus';
|
|
8
|
+
export function patch(prevElement, nextElement, parentReference, nextReference, contextMap, hostNamespace) {
|
|
7
9
|
if (prevElement.type !== nextElement.type || prevElement.key !== nextElement.key) {
|
|
8
|
-
replaceWithNewElement(prevElement, nextElement, parentReference);
|
|
10
|
+
replaceWithNewElement(prevElement, nextElement, parentReference, contextMap, hostNamespace);
|
|
9
11
|
}
|
|
10
12
|
else if (nextElement.flag === 'HOST') {
|
|
11
|
-
|
|
13
|
+
patchHostElement(prevElement, nextElement, contextMap, hostNamespace);
|
|
12
14
|
}
|
|
13
15
|
else if (nextElement.flag === 'FC') {
|
|
14
|
-
|
|
15
|
-
nextElement.store = prevElement.store;
|
|
16
|
-
}
|
|
17
|
-
patchFunctionalComponent(prevElement, nextElement, parentReference, nextReference);
|
|
16
|
+
patchFunctionalComponent(prevElement, nextElement, parentReference, nextReference, contextMap, hostNamespace);
|
|
18
17
|
}
|
|
19
18
|
else if (nextElement.flag === 'TEXT') {
|
|
20
|
-
|
|
19
|
+
patchTextElement(prevElement, nextElement);
|
|
20
|
+
}
|
|
21
|
+
else if (nextElement.flag === 'FRAGMENT') {
|
|
22
|
+
patchFragment(prevElement, nextElement, parentReference, contextMap, hostNamespace);
|
|
23
|
+
}
|
|
24
|
+
else if (nextElement.flag === 'PROVIDER') {
|
|
25
|
+
patchProvider(prevElement, nextElement, parentReference, contextMap, hostNamespace);
|
|
26
|
+
}
|
|
27
|
+
else if (nextElement.flag === 'PORTAL') {
|
|
28
|
+
patchPortal(prevElement, nextElement, contextMap);
|
|
21
29
|
}
|
|
22
30
|
else {
|
|
23
|
-
|
|
31
|
+
patchConsumer(prevElement, nextElement, parentReference, nextReference, contextMap, hostNamespace);
|
|
24
32
|
}
|
|
25
33
|
}
|
|
26
|
-
function replaceWithNewElement(prevElement, nextElement, parentReference) {
|
|
34
|
+
function replaceWithNewElement(prevElement, nextElement, parentReference, contextMap, hostNamespace) {
|
|
27
35
|
unmount(prevElement);
|
|
36
|
+
nextElement.parent = prevElement.parent;
|
|
28
37
|
if (nextElement.flag === 'HOST' && prevElement.flag === 'HOST') {
|
|
29
|
-
mount(nextElement, null, null);
|
|
30
|
-
|
|
38
|
+
mount(nextElement, null, null, contextMap, hostNamespace);
|
|
39
|
+
hostAdapter.replaceChild(parentReference, nextElement.reference, prevElement.reference);
|
|
31
40
|
}
|
|
32
41
|
else {
|
|
33
|
-
mount(nextElement, parentReference, findHostReferenceFromElement(prevElement,
|
|
42
|
+
mount(nextElement, parentReference, findHostReferenceFromElement(prevElement), contextMap, hostNamespace);
|
|
34
43
|
clearElementHostReference(prevElement, parentReference);
|
|
35
44
|
}
|
|
36
45
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
while (temp != null) {
|
|
41
|
-
flag = temp.flag;
|
|
42
|
-
if (flag === 'HOST' || flag === 'TEXT') {
|
|
43
|
-
return temp.reference;
|
|
44
|
-
}
|
|
45
|
-
temp = findChildElement(temp, startEdge);
|
|
46
|
+
function patchHostElement(prevElement, nextElement, contextMap, hostNamespace) {
|
|
47
|
+
if (prevElement.ref) {
|
|
48
|
+
nextElement.ref = prevElement.ref;
|
|
46
49
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
const hostNamespaces = hostAdapter.getHostNamespaces(nextElement, hostNamespace);
|
|
51
|
+
hostNamespace = hostNamespaces?.self;
|
|
52
|
+
nextElement.reference = prevElement.reference;
|
|
53
|
+
hostAdapter.attachElementToReference(nextElement, nextElement.reference);
|
|
54
|
+
patchChildren(prevElement.children, nextElement.children, nextElement.reference, null, nextElement, contextMap, hostNamespaces?.children);
|
|
55
|
+
hostAdapter.patchProps(nextElement.reference, prevElement, nextElement, hostNamespace);
|
|
56
|
+
if (prevElement.className !== nextElement.className) {
|
|
57
|
+
hostAdapter.setClassname(nextElement.reference, nextElement.className, hostNamespace);
|
|
53
58
|
}
|
|
54
|
-
|
|
59
|
+
applyRef(nextElement);
|
|
55
60
|
}
|
|
56
|
-
function
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
for (const propName in nextProps) {
|
|
61
|
-
const prevValue = prevProps[propName];
|
|
62
|
-
const nextValue = nextProps[propName];
|
|
63
|
-
if (prevValue !== nextValue) {
|
|
64
|
-
GLOBAL.hostAdapter.patchProp(hostReference, propName, prevValue, nextValue);
|
|
65
|
-
}
|
|
61
|
+
function patchFunctionalComponent(prevElement, nextElement, parentReference, nextReference, contextMap, hostNamespace) {
|
|
62
|
+
(nextElement.store = prevElement.store ||= {}).latestElement = nextElement;
|
|
63
|
+
if (hostNamespace) {
|
|
64
|
+
nextElement.store.hostNamespace = hostNamespace;
|
|
66
65
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
66
|
+
if (contextMap) {
|
|
67
|
+
nextElement.contextMap = contextMap;
|
|
68
|
+
}
|
|
69
|
+
let nextChildren;
|
|
70
|
+
try {
|
|
71
|
+
lifecycleEventBus.publish({ type: 'beforeRender', element: nextElement, phase: 'updating' });
|
|
72
|
+
nextChildren = normalizeRoot(nextElement.type(nextElement.props || emptyObject), false);
|
|
73
|
+
lifecycleEventBus.publish({ type: 'afterRender', phase: 'updating' });
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
lifecycleEventBus.publish({ type: 'errored', element: nextElement, error, phase: 'updating' });
|
|
77
|
+
return;
|
|
71
78
|
}
|
|
72
79
|
const prevChildren = prevElement.children;
|
|
80
|
+
if (nextChildren) {
|
|
81
|
+
nextElement.children = nextChildren;
|
|
82
|
+
}
|
|
83
|
+
patchChildren(prevChildren, nextChildren, parentReference, nextReference, nextElement, contextMap, hostNamespace);
|
|
84
|
+
lifecycleEventBus.publish({ type: 'updated', element: nextElement });
|
|
85
|
+
}
|
|
86
|
+
function patchTextElement(prevElement, nextElement) {
|
|
87
|
+
nextElement.reference = prevElement.reference;
|
|
88
|
+
if (nextElement.children !== prevElement.children) {
|
|
89
|
+
hostAdapter.setTextContent(nextElement.reference, nextElement.children);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function patchFragment(prevElement, nextElement, parentReference, contextMap, hostNamespace) {
|
|
93
|
+
let nextReference = null;
|
|
94
|
+
if (Array.isArray(prevElement.children) && !Array.isArray(nextElement.children) && nextElement.children) {
|
|
95
|
+
nextReference = hostAdapter.findNextSiblingReference(findHostReferenceFromElement(prevElement.children[prevElement.children.length - 1]));
|
|
96
|
+
}
|
|
97
|
+
patchChildren(prevElement.children, nextElement.children, parentReference, nextReference, nextElement, contextMap, hostNamespace);
|
|
98
|
+
}
|
|
99
|
+
function patchProvider(prevElement, nextElement, parentReference, contextMap, hostNamespace) {
|
|
100
|
+
let nextReference = null;
|
|
101
|
+
if (Array.isArray(prevElement.children) && !Array.isArray(nextElement.children) && nextElement.children) {
|
|
102
|
+
nextReference = hostAdapter.findNextSiblingReference(findHostReferenceFromElement(prevElement.children[prevElement.children.length - 1]));
|
|
103
|
+
}
|
|
104
|
+
contextMap = new Map(contextMap);
|
|
105
|
+
contextMap.set(nextElement.type.context, nextElement.props.value);
|
|
106
|
+
patchChildren(prevElement.children, nextElement.children, parentReference, nextReference, nextElement, contextMap, hostNamespace);
|
|
107
|
+
}
|
|
108
|
+
function patchConsumer(prevElement, nextElement, parentReference, nextReference, contextMap, hostNamespace) {
|
|
109
|
+
const children = normalizeRoot(nextElement.type(nextElement.props || emptyObject, contextMap || emptyMap), false);
|
|
110
|
+
if (children) {
|
|
111
|
+
nextElement.children = children;
|
|
112
|
+
}
|
|
113
|
+
patchChildren(prevElement.children, nextElement.children, parentReference, nextReference, nextElement, contextMap, hostNamespace);
|
|
114
|
+
}
|
|
115
|
+
export function patchPortal(prevElement, nextElement, contextMap) {
|
|
116
|
+
const prevContainer = prevElement.ref;
|
|
117
|
+
const nextContainer = nextElement.ref;
|
|
73
118
|
const nextChildren = nextElement.children;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
119
|
+
patchChildren(prevElement.children, nextChildren, prevContainer, null, nextElement, contextMap, hostAdapter.getHostNamespaces(nextChildren, undefined)?.self);
|
|
120
|
+
nextElement.reference = prevElement.reference;
|
|
121
|
+
if (prevContainer !== nextContainer && nextChildren != null) {
|
|
122
|
+
hostAdapter.removeChild(prevContainer, nextChildren.reference);
|
|
123
|
+
hostAdapter.appendChild(nextContainer, nextChildren.reference);
|
|
77
124
|
}
|
|
78
|
-
patchChildren(prevChildren, nextChildren, hostReference, null, prevElement);
|
|
79
125
|
}
|
|
80
|
-
function
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
else if (nextChildrenLength === 0) {
|
|
91
|
-
removeAllChildren(parentReference, parentElement, prevChildren);
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
patchKeyedChildren(prevChildren, nextChildren, parentReference, nextReference);
|
|
126
|
+
export function updateFunctionalComponent(element, parentReference, nextReference, contextMap, hostNamespace) {
|
|
127
|
+
patchFunctionalComponent(element, element, parentReference, nextReference, contextMap, hostNamespace);
|
|
128
|
+
}
|
|
129
|
+
function patchChildren(prevChildren, nextChildren, parentReference, nextReference, nextElement, contextMap, hostNamespace) {
|
|
130
|
+
if (Array.isArray(prevChildren)) {
|
|
131
|
+
if (Array.isArray(nextChildren)) {
|
|
132
|
+
for (const child of nextChildren) {
|
|
133
|
+
child.parent = nextElement;
|
|
95
134
|
}
|
|
135
|
+
patchKeyedChildren(prevChildren, nextChildren, parentReference, nextReference, contextMap, hostNamespace);
|
|
96
136
|
}
|
|
97
|
-
else if (
|
|
98
|
-
|
|
99
|
-
GLOBAL.hostAdapter.setTextContent(parentReference, (nextChildren || ''));
|
|
137
|
+
else if (nextChildren) {
|
|
138
|
+
patchKeyedChildren(prevChildren, [nextChildren], parentReference, nextReference, contextMap, hostNamespace);
|
|
100
139
|
}
|
|
101
140
|
else {
|
|
102
|
-
|
|
103
|
-
|
|
141
|
+
unmount(prevChildren);
|
|
142
|
+
hostAdapter.clearNode(parentReference);
|
|
104
143
|
}
|
|
105
144
|
}
|
|
106
|
-
else if (
|
|
107
|
-
if (isArray(nextChildren)) {
|
|
108
|
-
|
|
109
|
-
mountArrayChildren(nextChildren, parentReference, nextReference);
|
|
145
|
+
else if (prevChildren) {
|
|
146
|
+
if (Array.isArray(nextChildren)) {
|
|
147
|
+
patchKeyedChildren([prevChildren], nextChildren, parentReference, nextReference, contextMap, hostNamespace);
|
|
110
148
|
}
|
|
111
|
-
else if (
|
|
112
|
-
|
|
149
|
+
else if (nextChildren) {
|
|
150
|
+
nextChildren.parent = nextElement;
|
|
151
|
+
patch(prevChildren, nextChildren, parentReference, nextReference, contextMap, hostNamespace);
|
|
113
152
|
}
|
|
114
153
|
else {
|
|
115
|
-
|
|
116
|
-
|
|
154
|
+
unmount(prevChildren);
|
|
155
|
+
hostAdapter.clearNode(parentReference);
|
|
117
156
|
}
|
|
118
157
|
}
|
|
119
158
|
else {
|
|
120
|
-
if (isArray(nextChildren)) {
|
|
121
|
-
|
|
159
|
+
if (Array.isArray(nextChildren)) {
|
|
160
|
+
mountArrayChildren(nextChildren, parentReference, nextReference, contextMap, nextElement, hostNamespace);
|
|
122
161
|
}
|
|
123
|
-
else if (
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
patch(prevChildren, nextChildren, parentReference, nextReference);
|
|
162
|
+
else if (nextChildren) {
|
|
163
|
+
nextChildren.parent = nextElement;
|
|
164
|
+
mount(nextChildren, parentReference, nextReference, contextMap, hostNamespace);
|
|
129
165
|
}
|
|
130
166
|
}
|
|
131
167
|
}
|
|
132
|
-
function
|
|
133
|
-
unmount(prevChildren);
|
|
134
|
-
mountArrayChildren(nextChildren, parentReference, findHostReferenceFromElement(prevChildren, true));
|
|
135
|
-
clearElementHostReference(prevChildren, parentReference);
|
|
136
|
-
}
|
|
137
|
-
function patchSingleTextChild(prevChildren, nextChildren, parentReference) {
|
|
138
|
-
if (prevChildren !== nextChildren) {
|
|
139
|
-
GLOBAL.hostAdapter.setTextContent(parentReference, nextChildren);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
// export function patchNonKeyedChildren(
|
|
143
|
-
// prevChildren: SimpElement[],
|
|
144
|
-
// nextChildren: SimpElement[],
|
|
145
|
-
// parentReference: HostReference,
|
|
146
|
-
// prevChildrenLength: number,
|
|
147
|
-
// nextChildrenLength: number,
|
|
148
|
-
// nextReference: Nullable<HostReference>
|
|
149
|
-
// ): void {
|
|
150
|
-
// const commonLength = prevChildrenLength > nextChildrenLength ? nextChildrenLength : prevChildrenLength;
|
|
151
|
-
// let i = 0;
|
|
152
|
-
// let prevChild;
|
|
153
|
-
// let nextChild;
|
|
154
|
-
//
|
|
155
|
-
// for (; i < commonLength; ++i) {
|
|
156
|
-
// nextChild = nextChildren[i];
|
|
157
|
-
// prevChild = prevChildren[i];
|
|
158
|
-
//
|
|
159
|
-
// patch(prevChild as SimpElement, nextChild as SimpElement, parentReference, nextReference);
|
|
160
|
-
// prevChildren[i] = nextChild!;
|
|
161
|
-
// }
|
|
162
|
-
// if (prevChildrenLength < nextChildrenLength) {
|
|
163
|
-
// for (i = commonLength; i < nextChildrenLength; ++i) {
|
|
164
|
-
// nextChild = nextChildren[i];
|
|
165
|
-
// mount(nextChild as SimpElement, parentReference, nextReference);
|
|
166
|
-
// }
|
|
167
|
-
// } else if (prevChildrenLength > nextChildrenLength) {
|
|
168
|
-
// for (i = commonLength; i < prevChildrenLength; ++i) {
|
|
169
|
-
// remove(prevChildren[i] as SimpElement, parentReference);
|
|
170
|
-
// }
|
|
171
|
-
// }
|
|
172
|
-
// }
|
|
173
|
-
function patchFunctionalComponent(prevElement, nextElement, parentReference, nextReference) {
|
|
174
|
-
const prevChildren = prevElement.children;
|
|
175
|
-
const type = nextElement.type;
|
|
176
|
-
GLOBAL.eventBus.publish({ type: 'beforeRender', element: nextElement });
|
|
177
|
-
const nextChildren = normalizeRoot(type(nextElement.props || EMPTY_OBJECT));
|
|
178
|
-
GLOBAL.eventBus.publish({ type: 'afterRender' });
|
|
179
|
-
patch(prevChildren, nextChildren, parentReference, nextReference);
|
|
180
|
-
GLOBAL.eventBus.publish({ type: 'mounted', element: nextElement });
|
|
181
|
-
nextElement.children = nextChildren;
|
|
182
|
-
}
|
|
183
|
-
function patchText(prevElement, nextElement) {
|
|
184
|
-
const nextText = nextElement.children;
|
|
185
|
-
const reference = (nextElement.reference = prevElement.reference);
|
|
186
|
-
if (nextText !== prevElement.children) {
|
|
187
|
-
GLOBAL.hostAdapter.setTextContent(reference, nextText);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
function patchFragment(prevElement, nextElement, parentReference) {
|
|
191
|
-
patchChildren(prevElement.children, nextElement.children, parentReference, null, prevElement);
|
|
192
|
-
}
|
|
193
|
-
export function updateFunctionalComponent(element, parentReference, nextReference) {
|
|
194
|
-
patch(element, element, parentReference, nextReference);
|
|
195
|
-
}
|
|
196
|
-
export function patchKeyedChildren(prevChildren, nextChildren, parentReference, nextReference) {
|
|
197
|
-
var _a, _b, _c;
|
|
168
|
+
export function patchKeyedChildren(prevChildren, nextChildren, parentReference, nextReference, contextMap, hostNamespace) {
|
|
198
169
|
let prevStart = 0;
|
|
199
170
|
let nextStart = 0;
|
|
200
171
|
let prevEnd = prevChildren.length - 1;
|
|
@@ -203,21 +174,21 @@ export function patchKeyedChildren(prevChildren, nextChildren, parentReference,
|
|
|
203
174
|
while (prevStart <= prevEnd &&
|
|
204
175
|
nextStart <= nextEnd &&
|
|
205
176
|
prevChildren[prevStart].key === nextChildren[nextStart].key) {
|
|
206
|
-
patch(prevChildren[prevStart], nextChildren[nextStart], parentReference, null);
|
|
177
|
+
patch(prevChildren[prevStart], nextChildren[nextStart], parentReference, null, contextMap, hostNamespace);
|
|
207
178
|
prevStart++;
|
|
208
179
|
nextStart++;
|
|
209
180
|
}
|
|
210
181
|
// Step 2: Sync from end
|
|
211
182
|
while (prevStart <= prevEnd && nextStart <= nextEnd && prevChildren[prevEnd].key === nextChildren[nextEnd].key) {
|
|
212
|
-
patch(prevChildren[prevEnd], nextChildren[nextEnd], parentReference, null);
|
|
183
|
+
patch(prevChildren[prevEnd], nextChildren[nextEnd], parentReference, null, contextMap, hostNamespace);
|
|
213
184
|
prevEnd--;
|
|
214
185
|
nextEnd--;
|
|
215
186
|
}
|
|
216
187
|
// Step 3: Mount new nodes if prev list is exhausted
|
|
217
188
|
if (prevStart > prevEnd) {
|
|
218
|
-
const before =
|
|
189
|
+
const before = nextChildren[nextEnd + 1]?.reference || nextReference;
|
|
219
190
|
for (let i = nextStart; i <= nextEnd; i++) {
|
|
220
|
-
mount(nextChildren[i], parentReference, before);
|
|
191
|
+
mount(nextChildren[i], parentReference, before, contextMap, hostNamespace);
|
|
221
192
|
}
|
|
222
193
|
// Step 4: Remove prev nodes if next list is exhausted
|
|
223
194
|
}
|
|
@@ -232,8 +203,9 @@ export function patchKeyedChildren(prevChildren, nextChildren, parentReference,
|
|
|
232
203
|
const keyToPrevIndexMap = new Map();
|
|
233
204
|
for (let i = prevStart; i <= prevEnd; i++) {
|
|
234
205
|
const key = prevChildren[i].key;
|
|
235
|
-
if (key != null)
|
|
206
|
+
if (key != null) {
|
|
236
207
|
keyToPrevIndexMap.set(key, i);
|
|
208
|
+
}
|
|
237
209
|
}
|
|
238
210
|
// Track reused indices and move plan
|
|
239
211
|
const toMove = new Array(nextEnd - nextStart + 1);
|
|
@@ -244,12 +216,12 @@ export function patchKeyedChildren(prevChildren, nextChildren, parentReference,
|
|
|
244
216
|
const prevIndex = keyToPrevIndexMap.get(nextChild.key);
|
|
245
217
|
if (prevIndex != null) {
|
|
246
218
|
const prevElement = prevChildren[prevIndex];
|
|
247
|
-
patch(prevElement, nextChild, parentReference, null);
|
|
219
|
+
patch(prevElement, nextChild, parentReference, null, contextMap, hostNamespace);
|
|
248
220
|
toMove[i - nextStart] = prevIndex;
|
|
249
221
|
usedIndices.add(prevIndex);
|
|
250
222
|
}
|
|
251
223
|
else {
|
|
252
|
-
mount(nextChild, parentReference,
|
|
224
|
+
mount(nextChild, parentReference, nextChildren[i + 1]?.reference || nextReference, contextMap, hostNamespace);
|
|
253
225
|
toMove[i - nextStart] = -1;
|
|
254
226
|
}
|
|
255
227
|
}
|
|
@@ -262,10 +234,34 @@ export function patchKeyedChildren(prevChildren, nextChildren, parentReference,
|
|
|
262
234
|
// Insert in correct order
|
|
263
235
|
for (let i = nextEnd; i >= nextStart; i--) {
|
|
264
236
|
const currentChild = nextChildren[i];
|
|
265
|
-
const reference =
|
|
237
|
+
const reference = nextChildren[i + 1]?.reference || nextReference;
|
|
266
238
|
if (toMove[i - nextStart] !== -1) {
|
|
267
|
-
|
|
239
|
+
hostAdapter.insertBefore(parentReference, currentChild.reference, reference);
|
|
268
240
|
}
|
|
269
241
|
}
|
|
270
242
|
}
|
|
271
243
|
}
|
|
244
|
+
export function findParentReferenceFromElement(element) {
|
|
245
|
+
let flag;
|
|
246
|
+
let temp = element;
|
|
247
|
+
while (temp != null) {
|
|
248
|
+
flag = temp.flag;
|
|
249
|
+
if (flag === 'HOST') {
|
|
250
|
+
return temp.reference;
|
|
251
|
+
}
|
|
252
|
+
temp = temp.parent;
|
|
253
|
+
}
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
export function findHostReferenceFromElement(element) {
|
|
257
|
+
let flag;
|
|
258
|
+
let temp = element;
|
|
259
|
+
while (temp != null) {
|
|
260
|
+
flag = temp.flag;
|
|
261
|
+
if (flag === 'HOST' || flag === 'TEXT' || flag === 'PORTAL') {
|
|
262
|
+
return temp.reference;
|
|
263
|
+
}
|
|
264
|
+
temp = (Array.isArray(temp.children) ? temp.children[0] : temp.children);
|
|
265
|
+
}
|
|
266
|
+
return null;
|
|
267
|
+
}
|
package/core/portal.d.ts
ADDED
package/core/portal.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { normalizeRoot } from './createElement';
|
|
2
|
+
export function createPortal(children, container) {
|
|
3
|
+
const element = { flag: 'PORTAL', parent: null };
|
|
4
|
+
if ((children = normalizeRoot(children, false))) {
|
|
5
|
+
element.children = children;
|
|
6
|
+
}
|
|
7
|
+
element.ref = container;
|
|
8
|
+
return element;
|
|
9
|
+
}
|
package/core/ref.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { SimpElement } from './createElement';
|
|
2
|
+
interface RefSimpElement extends SimpElement {
|
|
3
|
+
ref?: {
|
|
4
|
+
value: NonNullable<Ref<unknown>>;
|
|
5
|
+
cleanup?: () => void;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
export interface RefObject<T> {
|
|
9
|
+
current: T;
|
|
10
|
+
}
|
|
11
|
+
export type RefCallback<T> = {
|
|
12
|
+
bivarianceHack(instance: T | null): (() => void) | void;
|
|
13
|
+
}['bivarianceHack'];
|
|
14
|
+
export type Ref<T> = RefCallback<T> | RefObject<T> | null;
|
|
15
|
+
export declare function unmountRef(element: RefSimpElement): void;
|
|
16
|
+
export declare function applyRef(element: RefSimpElement): void;
|
|
17
|
+
export {};
|