|
14 | 14 |
|
15 | 15 | import type { ReactCoroutine } from 'ReactCoroutine'; |
16 | 16 | import type { Fiber } from 'ReactFiber'; |
| 17 | +import type { FiberRoot } from 'ReactFiberRoot'; |
17 | 18 | import type { HostConfig } from 'ReactFiberReconciler'; |
| 19 | +import type { Scheduler } from 'ReactFiberScheduler'; |
18 | 20 | import type { PriorityLevel } from 'ReactPriorityLevel'; |
| 21 | +import type { UpdateQueue } from 'ReactFiberUpdateQueue'; |
19 | 22 |
|
20 | 23 | var { |
21 | 24 | reconcileChildFibers, |
22 | 25 | reconcileChildFibersInPlace, |
23 | 26 | cloneChildFibers, |
24 | 27 | } = require('ReactChildFiber'); |
| 28 | +var { LowPriority } = require('ReactPriorityLevel'); |
25 | 29 | var ReactTypeOfWork = require('ReactTypeOfWork'); |
26 | 30 | var { |
27 | 31 | IndeterminateComponent, |
|
37 | 41 | NoWork, |
38 | 42 | OffscreenPriority, |
39 | 43 | } = require('ReactPriorityLevel'); |
| 44 | +var { |
| 45 | + createUpdateQueue, |
| 46 | + addToQueue, |
| 47 | + addCallbackToQueue, |
| 48 | + mergeUpdateQueue, |
| 49 | +} = require('ReactFiberUpdateQueue'); |
| 50 | +var ReactInstanceMap = require('ReactInstanceMap'); |
40 | 51 |
|
41 | | -module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>) { |
| 52 | +module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>, getScheduler : () => Scheduler) { |
42 | 53 |
|
43 | 54 | function markChildAsProgressed(current, workInProgress, priorityLevel) { |
44 | 55 | // We now have clones. Let's store them as the currently progressed work. |
@@ -105,25 +116,116 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>) { |
105 | 116 | return workInProgress.child; |
106 | 117 | } |
107 | 118 |
|
| 119 | + function scheduleUpdate(fiber: Fiber, updateQueue: UpdateQueue, priorityLevel : PriorityLevel): void { |
| 120 | + const { scheduleLowPriWork } = getScheduler(); |
| 121 | + fiber.updateQueue = updateQueue; |
| 122 | + // Schedule update on the alternate as well, since we don't know which tree |
| 123 | + // is current. |
| 124 | + if (fiber.alternate) { |
| 125 | + fiber.alternate.updateQueue = updateQueue; |
| 126 | + } |
| 127 | + while (true) { |
| 128 | + if (fiber.pendingWorkPriority === NoWork || |
| 129 | + fiber.pendingWorkPriority >= priorityLevel) { |
| 130 | + fiber.pendingWorkPriority = priorityLevel; |
| 131 | + } |
| 132 | + if (fiber.alternate) { |
| 133 | + if (fiber.alternate.pendingWorkPriority === NoWork || |
| 134 | + fiber.alternate.pendingWorkPriority >= priorityLevel) { |
| 135 | + fiber.alternate.pendingWorkPriority = priorityLevel; |
| 136 | + } |
| 137 | + } |
| 138 | + // Duck type root |
| 139 | + if (fiber.stateNode && fiber.stateNode.containerInfo) { |
| 140 | + const root : FiberRoot = (fiber.stateNode : any); |
| 141 | + scheduleLowPriWork(root, priorityLevel); |
| 142 | + return; |
| 143 | + } |
| 144 | + if (!fiber.return) { |
| 145 | + throw new Error('No root!'); |
| 146 | + } |
| 147 | + fiber = fiber.return; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // Class component state updater |
| 152 | + const updater = { |
| 153 | + enqueueSetState(instance, partialState) { |
| 154 | + const fiber = ReactInstanceMap.get(instance); |
| 155 | + const updateQueue = fiber.updateQueue ? |
| 156 | + addToQueue(fiber.updateQueue, partialState) : |
| 157 | + createUpdateQueue(partialState); |
| 158 | + scheduleUpdate(fiber, updateQueue, LowPriority); |
| 159 | + }, |
| 160 | + enqueueReplaceState(instance, state) { |
| 161 | + const fiber = ReactInstanceMap.get(instance); |
| 162 | + const updateQueue = createUpdateQueue(state); |
| 163 | + updateQueue.isReplace = true; |
| 164 | + scheduleUpdate(fiber, updateQueue, LowPriority); |
| 165 | + }, |
| 166 | + enqueueForceUpdate(instance) { |
| 167 | + const fiber = ReactInstanceMap.get(instance); |
| 168 | + const updateQueue = fiber.updateQueue || createUpdateQueue(null); |
| 169 | + updateQueue.isForced = true; |
| 170 | + scheduleUpdate(fiber, updateQueue, LowPriority); |
| 171 | + }, |
| 172 | + enqueueCallback(instance, callback) { |
| 173 | + const fiber = ReactInstanceMap.get(instance); |
| 174 | + let updateQueue = fiber.updateQueue ? |
| 175 | + fiber.updateQueue : |
| 176 | + createUpdateQueue(null); |
| 177 | + addCallbackToQueue(updateQueue, callback); |
| 178 | + fiber.updateQueue = updateQueue; |
| 179 | + if (fiber.alternate) { |
| 180 | + fiber.alternate.updateQueue = updateQueue; |
| 181 | + } |
| 182 | + }, |
| 183 | + }; |
| 184 | + |
108 | 185 | function updateClassComponent(current : ?Fiber, workInProgress : Fiber) { |
| 186 | + // A class component update is the result of either new props or new state. |
| 187 | + // Account for the possibly of missing pending props by falling back to the |
| 188 | + // memoized props. |
109 | 189 | var props = workInProgress.pendingProps; |
| 190 | + if (!props && current) { |
| 191 | + props = current.memoizedProps; |
| 192 | + } |
| 193 | + // Compute the state using the memoized state and the update queue. |
| 194 | + var updateQueue = workInProgress.updateQueue; |
| 195 | + var previousState = current ? current.memoizedState : null; |
| 196 | + var state = updateQueue ? |
| 197 | + mergeUpdateQueue(updateQueue, previousState, props) : |
| 198 | + previousState; |
| 199 | + |
110 | 200 | var instance = workInProgress.stateNode; |
111 | 201 | if (!instance) { |
112 | 202 | var ctor = workInProgress.type; |
113 | 203 | workInProgress.stateNode = instance = new ctor(props); |
114 | | - } else if (typeof instance.shouldComponentUpdate === 'function') { |
| 204 | + state = instance.state || null; |
| 205 | + // The initial state must be added to the update queue in case |
| 206 | + // setState is called before the initial render. |
| 207 | + if (state !== null) { |
| 208 | + workInProgress.updateQueue = createUpdateQueue(state); |
| 209 | + } |
| 210 | + // The instance needs access to the fiber so that it can schedule updates |
| 211 | + ReactInstanceMap.set(instance, workInProgress); |
| 212 | + instance.updater = updater; |
| 213 | + } else if (typeof instance.shouldComponentUpdate === 'function' && |
| 214 | + !(updateQueue && updateQueue.isForced)) { |
115 | 215 | if (workInProgress.memoizedProps !== null) { |
116 | 216 | // Reset the props, in case this is a ping-pong case rather than a |
117 | 217 | // completed update case. For the completed update case, the instance |
118 | 218 | // props will already be the memoizedProps. |
119 | 219 | instance.props = workInProgress.memoizedProps; |
120 | | - if (!instance.shouldComponentUpdate(props)) { |
| 220 | + instance.state = workInProgress.memoizedState; |
| 221 | + if (!instance.shouldComponentUpdate(props, state)) { |
121 | 222 | return bailoutOnAlreadyFinishedWork(current, workInProgress); |
122 | 223 | } |
123 | 224 | } |
124 | 225 | } |
125 | 226 |
|
126 | 227 | instance.props = props; |
| 228 | + instance.state = state; |
127 | 229 | var nextChildren = instance.render(); |
128 | 230 | reconcileChildren(current, workInProgress, nextChildren); |
129 | 231 |
|
@@ -251,10 +353,11 @@ module.exports = function<T, P, I, C>(config : HostConfig<T, P, I, C>) { |
251 | 353 | workInProgress.child = workInProgress.progressedChild; |
252 | 354 | } |
253 | 355 |
|
254 | | - if (workInProgress.pendingProps === null || ( |
| 356 | + if ((workInProgress.pendingProps === null || ( |
255 | 357 | workInProgress.memoizedProps !== null && |
256 | 358 | workInProgress.pendingProps === workInProgress.memoizedProps |
257 | | - )) { |
| 359 | + )) && |
| 360 | + workInProgress.updateQueue === null) { |
258 | 361 | return bailoutOnAlreadyFinishedWork(current, workInProgress); |
259 | 362 | } |
260 | 363 |
|
|
0 commit comments