-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathhostConfig.ts
More file actions
66 lines (56 loc) · 1.75 KB
/
hostConfig.ts
File metadata and controls
66 lines (56 loc) · 1.75 KB
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 { DOMElement, updateFiberProps } from './SyntheticEvent';
import { FiberNode } from 'react-reconciler/src/fiber';
import { HostText } from 'react-reconciler/src/workTags';
export type Container = Element;
export type Instance = DOMElement;
export type TextInstance = Text;
export const createInstance = (type: string, props: any): Instance => {
const element = document.createElement(type) as unknown;
updateFiberProps(element as DOMElement, props);
return element as DOMElement;
};
export const createTextInstance = (content: string) => {
return document.createTextNode(content);
};
export const appendInitialChild = (parent: Instance, child: Instance) => {
parent.appendChild(child);
};
export const appendChildToContainer = (
child: Instance,
container: Container
) => {
container.appendChild(child);
};
export const insertChildToContainer = (
child: Instance,
container: Container,
before: Instance
) => {
container.insertBefore(child, before);
};
export const removeChild = (child: Instance, container: Container) => {
container.removeChild(child);
};
export function commitUpdate(finishedWork: FiberNode) {
if (__LOG__) {
console.log('更新DOM、文本节点内容', finishedWork);
}
switch (finishedWork.tag) {
case HostText:
const newContent = finishedWork.pendingProps.content;
return commitTextUpdate(finishedWork.stateNode, newContent);
}
console.error('commitUpdate未支持的类型', finishedWork);
}
export const commitTextUpdate = (
textIntance: TextInstance,
content: string
) => {
textIntance.nodeValue = content;
};
export const scheduleMicrotask =
typeof queueMicrotask === 'function'
? queueMicrotask
: typeof Promise === 'function'
? (callback: () => void) => Promise.resolve(null).then(callback)
: setTimeout;