|
21 | 21 | createUpdateQueue, |
22 | 22 | addToQueue, |
23 | 23 | addCallbackToQueue, |
| 24 | + mergeUpdateQueue, |
24 | 25 | } = require('ReactFiberUpdateQueue'); |
25 | 26 | var ReactInstanceMap = require('ReactInstanceMap'); |
26 | 27 |
|
@@ -70,20 +71,157 @@ module.exports = function(scheduleUpdate : (fiber: Fiber, priorityLevel : Priori |
70 | 71 | }, |
71 | 72 | }; |
72 | 73 |
|
73 | | - function mount(workInProgress : Fiber, instance : any) { |
| 74 | + function adoptClassInstance(workInProgress : Fiber, instance : any) : void { |
| 75 | + instance.updater = updater; |
| 76 | + workInProgress.stateNode = instance; |
| 77 | + // The instance needs access to the fiber so that it can schedule updates |
| 78 | + ReactInstanceMap.set(instance, workInProgress); |
| 79 | + } |
| 80 | + |
| 81 | + function constructClassInstance(workInProgress : Fiber) : any { |
| 82 | + const ctor = workInProgress.type; |
| 83 | + const props = workInProgress.pendingProps; |
| 84 | + const instance = new ctor(props); |
| 85 | + adoptClassInstance(workInProgress, instance); |
| 86 | + return instance; |
| 87 | + } |
| 88 | + |
| 89 | + // Invokes the mount life-cycles on a previously never rendered instance. |
| 90 | + function mountClassInstance(workInProgress : Fiber) : void { |
| 91 | + const instance = workInProgress.stateNode; |
| 92 | + |
74 | 93 | const state = instance.state || null; |
75 | 94 | // The initial state must be added to the update queue in case |
76 | 95 | // setState is called before the initial render. |
77 | 96 | if (state !== null) { |
78 | 97 | workInProgress.updateQueue = createUpdateQueue(state); |
79 | 98 | } |
80 | | - // The instance needs access to the fiber so that it can schedule updates |
81 | | - ReactInstanceMap.set(instance, workInProgress); |
82 | | - instance.updater = updater; |
| 99 | + |
| 100 | + // A class component update is the result of either new props or new state. |
| 101 | + // Account for the possibly of missing pending props by falling back to the |
| 102 | + // memoized props. |
| 103 | + let props = workInProgress.pendingProps; |
| 104 | + if (!props) { |
| 105 | + throw new Error('There must be pending props for an initial mount.'); |
| 106 | + } |
| 107 | + |
| 108 | + instance.props = props; |
| 109 | + instance.state = state; |
| 110 | + |
| 111 | + if (typeof instance.componentWillMount === 'function') { |
| 112 | + instance.componentWillMount(); |
| 113 | + // If we had additional state updates during this life-cycle, let's |
| 114 | + // process them now. |
| 115 | + const updateQueue = workInProgress.updateQueue; |
| 116 | + if (updateQueue) { |
| 117 | + instance.state = mergeUpdateQueue(updateQueue, state, props); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Called on a preexisting class instance. Returns false if a resumed render |
| 123 | + // could be reused. |
| 124 | + function resumeMountClassInstance(workInProgress : Fiber) : boolean { |
| 125 | + const instance = workInProgress.stateNode; |
| 126 | + const newState = workInProgress.memoizedState; |
| 127 | + let newProps = workInProgress.pendingProps; |
| 128 | + if (!newProps) { |
| 129 | + // If there isn't any new props, then we'll reuse the memoized props. |
| 130 | + // This could be from already completed work. |
| 131 | + newProps = workInProgress.memoizedProps; |
| 132 | + if (!newProps) { |
| 133 | + throw new Error('There should always be pending or memoized props.'); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // TODO: Should we deal with a setState that happened after the last |
| 138 | + // componentWillMount and before this componentWillMount? Probably |
| 139 | + // unsupported anyway. |
| 140 | + |
| 141 | + const updateQueue = workInProgress.updateQueue; |
| 142 | + |
| 143 | + // If this completed, we might be able to just reuse this instance. |
| 144 | + if (typeof instance.shouldComponentUpdate === 'function' && |
| 145 | + !(updateQueue && updateQueue.isForced) && |
| 146 | + workInProgress.memoizedProps !== null && |
| 147 | + !instance.shouldComponentUpdate(newProps, newState)) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + // If we didn't bail out we need to construct a new instance. We don't |
| 152 | + // want to reuse one that failed to fully mount. |
| 153 | + const newInstance = constructClassInstance(workInProgress); |
| 154 | + if (typeof newInstance.componentWillMount === 'function') { |
| 155 | + newInstance.componentWillMount(); |
| 156 | + // If we had additional state updates during this life-cycle, let's |
| 157 | + // process them now. |
| 158 | + const newUpdateQueue = workInProgress.updateQueue; |
| 159 | + if (newUpdateQueue) { |
| 160 | + instance.state = mergeUpdateQueue(newUpdateQueue, newState, newProps); |
| 161 | + } |
| 162 | + } |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + // Invokes the update life-cycles and returns false if it shouldn't rerender. |
| 167 | + function updateClassInstance(current : Fiber, workInProgress : Fiber) : boolean { |
| 168 | + const instance = workInProgress.stateNode; |
| 169 | + |
| 170 | + const oldProps = current.memoizedProps; |
| 171 | + let newProps = workInProgress.pendingProps; |
| 172 | + if (!newProps) { |
| 173 | + // If there aren't any new props, then we'll reuse the memoized props. |
| 174 | + // This could be from already completed work. |
| 175 | + newProps = workInProgress.memoizedProps; |
| 176 | + if (!newProps) { |
| 177 | + throw new Error('There should always be pending or memoized props.'); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + // Note: During these life-cycles, instance.props/instance.state are what |
| 182 | + // ever the previously attempted to render - not the "current". However, |
| 183 | + // during componentDidUpdate we pass the "current" props. |
| 184 | + |
| 185 | + if (oldProps !== newProps) { |
| 186 | + if (typeof instance.componentWillReceiveProps === 'function') { |
| 187 | + instance.componentWillReceiveProps(newProps); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + // Compute the next state using the memoized state and the update queue. |
| 192 | + const updateQueue = workInProgress.updateQueue; |
| 193 | + const previousState = workInProgress.memoizedState; |
| 194 | + // TODO: Previous state can be null. |
| 195 | + let newState; |
| 196 | + if (updateQueue) { |
| 197 | + newState = mergeUpdateQueue(updateQueue, previousState, newProps); |
| 198 | + } else { |
| 199 | + newState = previousState; |
| 200 | + } |
| 201 | + |
| 202 | + if (typeof instance.shouldComponentUpdate === 'function' && |
| 203 | + !(updateQueue && updateQueue.isForced) && |
| 204 | + workInProgress.memoizedProps !== null && |
| 205 | + !instance.shouldComponentUpdate(newProps, newState)) { |
| 206 | + // TODO: Should this get the new props/state updated regardless? |
| 207 | + return false; |
| 208 | + } |
| 209 | + |
| 210 | + if (typeof instance.componentWillUpdate === 'function') { |
| 211 | + instance.componentWillUpdate(newProps, newState); |
| 212 | + } |
| 213 | + |
| 214 | + instance.props = newProps; |
| 215 | + instance.state = newState; |
| 216 | + return true; |
83 | 217 | } |
84 | 218 |
|
85 | 219 | return { |
86 | | - mount, |
| 220 | + adoptClassInstance, |
| 221 | + constructClassInstance, |
| 222 | + mountClassInstance, |
| 223 | + resumeMountClassInstance, |
| 224 | + updateClassInstance, |
87 | 225 | }; |
88 | 226 |
|
89 | 227 | }; |
0 commit comments