/
fiber.ts
156 lines (135 loc) · 3.54 KB
/
fiber.ts
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
import { Props, Key, Ref, ReactElementType } from 'shared/ReactTypes';
import {
ContextProvider,
Fragment,
FunctionComponent,
HostComponent,
WorkTag
} from './workTags';
import { Flags, NoFlags } from './fiberFlags';
import { Container } from 'hostConfig';
import { Lane, Lanes, NoLane, NoLanes } from './fiberLanes';
import { Effect } from './fiberHooks';
import { CallbackNode } from 'scheduler';
import { REACT_PROVIDER_TYPE } from 'shared/ReactSymbols';
export class FiberNode {
type: any;
tag: WorkTag;
pendingProps: Props;
key: Key;
stateNode: any;
ref: Ref;
return: FiberNode | null;
sibling: FiberNode | null;
child: FiberNode | null;
index: number;
memoizedProps: Props | null;
memoizedState: any;
alternate: FiberNode | null;
flags: Flags;
subtreeFlags: Flags;
updateQueue: unknown;
deletions: FiberNode[] | null;
constructor(tag: WorkTag, pendingProps: Props, key: Key) {
// 实例
this.tag = tag;
this.key = key || null;
// HostComponent <div> div DOM
this.stateNode = null;
// FunctionComponent () => {}
this.type = null;
// 构成树状结构
this.return = null;
this.sibling = null;
this.child = null;
this.index = 0;
this.ref = null;
// 作为工作单元
this.pendingProps = pendingProps;
this.memoizedProps = null;
this.memoizedState = null;
this.updateQueue = null;
this.alternate = null;
// 副作用
this.flags = NoFlags;
this.subtreeFlags = NoFlags;
this.deletions = null;
}
}
export interface PendingPassiveEffects {
unmount: Effect[];
update: Effect[];
}
export class FiberRootNode {
container: Container;
current: FiberNode;
finishedWork: FiberNode | null;
pendingLanes: Lanes;
finishedLane: Lane;
pendingPassiveEffects: PendingPassiveEffects;
callbackNode: CallbackNode | null;
callbackPriority: Lane;
constructor(container: Container, hostRootFiber: FiberNode) {
this.container = container;
this.current = hostRootFiber;
hostRootFiber.stateNode = this;
this.finishedWork = null;
this.pendingLanes = NoLanes;
this.finishedLane = NoLane;
this.callbackNode = null;
this.callbackPriority = NoLane;
this.pendingPassiveEffects = {
unmount: [],
update: []
};
}
}
export const createWorkInProgress = (
current: FiberNode,
pendingProps: Props
): FiberNode => {
let wip = current.alternate;
if (wip === null) {
// mount
wip = new FiberNode(current.tag, pendingProps, current.key);
wip.stateNode = current.stateNode;
wip.alternate = current;
current.alternate = wip;
} else {
// update
wip.pendingProps = pendingProps;
wip.flags = NoFlags;
wip.subtreeFlags = NoFlags;
wip.deletions = null;
}
wip.type = current.type;
wip.updateQueue = current.updateQueue;
wip.child = current.child;
wip.memoizedProps = current.memoizedProps;
wip.memoizedState = current.memoizedState;
wip.ref = current.ref;
return wip;
};
export function createFiberFromElement(element: ReactElementType): FiberNode {
const { type, key, props, ref } = element;
let fiberTag: WorkTag = FunctionComponent;
if (typeof type === 'string') {
// <div/> type: 'div'
fiberTag = HostComponent;
} else if (
typeof type === 'object' &&
type.$$typeof === REACT_PROVIDER_TYPE
) {
fiberTag = ContextProvider;
} else if (typeof type !== 'function' && __DEV__) {
console.warn('为定义的type类型', element);
}
const fiber = new FiberNode(fiberTag, props, key);
fiber.type = type;
fiber.ref = ref;
return fiber;
}
export function createFiberFromFragment(elements: any[], key: Key): FiberNode {
const fiber = new FiberNode(Fragment, elements, key);
return fiber;
}