-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathrenderer.js
148 lines (109 loc) Β· 3.41 KB
/
renderer.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
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-unused-vars */
/* eslint-disable no-param-reassign */
import ReactFiberReconciler from 'react-reconciler';
import * as scheduler from 'scheduler';
import propsEqual from './utils/propsEqual';
const emptyObject = {};
const appendChild = (parentInstance, child) => {
const isParentText =
parentInstance.type === 'TEXT' ||
parentInstance.type === 'LINK' ||
parentInstance.type === 'TSPAN';
const isChildTextInstance = child.type === 'TEXT_INSTANCE';
const isOrphanTextInstance = isChildTextInstance && !isParentText;
// Ignore orphan text instances.
// Caused by cases such as <>{name && <Text>{name}</Text>}</>
if (isOrphanTextInstance) {
console.warn(
`Invalid '${child.value}' string child outside <Text> component`,
);
return;
}
parentInstance.children.push(child);
};
const createRenderer = ({ onChange = () => {} }) => {
return ReactFiberReconciler({
schedulePassiveEffects: scheduler.unstable_scheduleCallback,
cancelPassiveEffects: scheduler.unstable_cancelCallback,
supportsMutation: true,
isPrimaryRenderer: false,
warnsIfNotActing: false,
appendInitialChild: appendChild,
createInstance(type, { style, children, ...props }) {
return {
type,
box: {},
style: style || {},
props: props || {},
children: [],
};
},
createTextInstance(text, rootContainerInstance) {
return { type: 'TEXT_INSTANCE', value: text };
},
finalizeInitialChildren(element, type, props) {
return false;
},
getPublicInstance(instance) {
return instance;
},
prepareForCommit() {
// Noop
},
clearContainer() {
// Noop
},
prepareUpdate(element, type, oldProps, newProps) {
return !propsEqual(oldProps, newProps);
},
resetAfterCommit: onChange,
resetTextContent(element) {
// Noop
},
getRootHostContext() {
return emptyObject;
},
getChildHostContext() {
return emptyObject;
},
shouldSetTextContent(type, props) {
return false;
},
now: Date.now,
useSyncScheduling: true,
appendChild,
appendChildToContainer(parentInstance, child) {
if (parentInstance.type === 'ROOT') {
parentInstance.document = child;
} else {
appendChild(parentInstance, child);
}
},
insertBefore(parentInstance, child, beforeChild) {
const index = parentInstance.children?.indexOf(beforeChild);
if (index === undefined) return;
if (index !== -1 && child)
parentInstance.children.splice(index, 0, child);
},
removeChild(parentInstance, child) {
const index = parentInstance.children?.indexOf(child);
if (index === undefined) return;
if (index !== -1) parentInstance.children.splice(index, 1);
},
removeChildFromContainer(parentInstance, child) {
const index = parentInstance.children?.indexOf(child);
if (index === undefined) return;
if (index !== -1) parentInstance.children.splice(index, 1);
},
commitTextUpdate(textInstance, oldText, newText) {
textInstance.value = newText;
},
commitUpdate(instance, updatePayload, type, oldProps, newProps) {
const { style, ...props } = newProps;
instance.props = props;
instance.style = style;
},
});
};
export default createRenderer;