1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | 'use strict';
|
13 |
|
14 | var ReactFiberBeginWork = require('./ReactFiberBeginWork');
|
15 | var ReactFiberCompleteWork = require('./ReactFiberCompleteWork');
|
16 | var ReactFiberCommitWork = require('./ReactFiberCommitWork');
|
17 |
|
18 | var _require = require('./ReactFiber'),
|
19 | cloneFiber = _require.cloneFiber;
|
20 |
|
21 | var _require2 = require('./ReactPriorityLevel'),
|
22 | NoWork = _require2.NoWork,
|
23 | LowPriority = _require2.LowPriority,
|
24 | AnimationPriority = _require2.AnimationPriority,
|
25 | SynchronousPriority = _require2.SynchronousPriority;
|
26 |
|
27 | var timeHeuristicForUnitOfWork = 1;
|
28 |
|
29 | module.exports = function (config) {
|
30 |
|
31 |
|
32 | var scheduler = void 0;
|
33 | function getScheduler() {
|
34 | return scheduler;
|
35 | }
|
36 |
|
37 | var _ReactFiberBeginWork = ReactFiberBeginWork(config, getScheduler),
|
38 | beginWork = _ReactFiberBeginWork.beginWork;
|
39 |
|
40 | var _ReactFiberCompleteWo = ReactFiberCompleteWork(config),
|
41 | completeWork = _ReactFiberCompleteWo.completeWork;
|
42 |
|
43 | var _ReactFiberCommitWork = ReactFiberCommitWork(config),
|
44 | commitWork = _ReactFiberCommitWork.commitWork;
|
45 |
|
46 | var scheduleAnimationCallback = config.scheduleAnimationCallback;
|
47 | var scheduleDeferredCallback = config.scheduleDeferredCallback;
|
48 |
|
49 |
|
50 | var defaultPriority = LowPriority;
|
51 |
|
52 |
|
53 | var nextUnitOfWork = null;
|
54 | var nextPriorityLevel = NoWork;
|
55 |
|
56 |
|
57 | var nextScheduledRoot = null;
|
58 | var lastScheduledRoot = null;
|
59 |
|
60 | function findNextUnitOfWork() {
|
61 |
|
62 | while (nextScheduledRoot && nextScheduledRoot.current.pendingWorkPriority === NoWork) {
|
63 | nextScheduledRoot.isScheduled = false;
|
64 | if (nextScheduledRoot === lastScheduledRoot) {
|
65 | nextScheduledRoot = null;
|
66 | lastScheduledRoot = null;
|
67 | nextPriorityLevel = NoWork;
|
68 | return null;
|
69 | }
|
70 | nextScheduledRoot = nextScheduledRoot.nextScheduledRoot;
|
71 | }
|
72 |
|
73 |
|
74 | var root = nextScheduledRoot;
|
75 | var highestPriorityRoot = null;
|
76 | var highestPriorityLevel = NoWork;
|
77 | while (root) {
|
78 | if (highestPriorityLevel === NoWork || highestPriorityLevel > root.current.pendingWorkPriority) {
|
79 | highestPriorityLevel = root.current.pendingWorkPriority;
|
80 | highestPriorityRoot = root;
|
81 | }
|
82 |
|
83 | root = root.nextScheduledRoot;
|
84 | }
|
85 | if (highestPriorityRoot) {
|
86 | nextPriorityLevel = highestPriorityLevel;
|
87 | return cloneFiber(highestPriorityRoot.current, highestPriorityLevel);
|
88 | }
|
89 |
|
90 | nextPriorityLevel = NoWork;
|
91 | return null;
|
92 | }
|
93 |
|
94 | function commitAllWork(finishedWork) {
|
95 |
|
96 |
|
97 | var effectfulFiber = finishedWork.firstEffect;
|
98 | while (effectfulFiber) {
|
99 | var current = effectfulFiber.alternate;
|
100 | commitWork(current, effectfulFiber);
|
101 | var next = effectfulFiber.nextEffect;
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | effectfulFiber.nextEffect = null;
|
107 | effectfulFiber = next;
|
108 | }
|
109 | }
|
110 |
|
111 | function resetWorkPriority(workInProgress) {
|
112 | var newPriority = NoWork;
|
113 |
|
114 |
|
115 |
|
116 | var child = workInProgress.progressedChild;
|
117 | while (child) {
|
118 |
|
119 | if (child.pendingWorkPriority !== NoWork && (newPriority === NoWork || newPriority > child.pendingWorkPriority)) {
|
120 | newPriority = child.pendingWorkPriority;
|
121 | }
|
122 | child = child.sibling;
|
123 | }
|
124 | workInProgress.pendingWorkPriority = newPriority;
|
125 | }
|
126 |
|
127 | function completeUnitOfWork(workInProgress) {
|
128 | while (true) {
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | var current = workInProgress.alternate;
|
134 | var next = completeWork(current, workInProgress);
|
135 |
|
136 | resetWorkPriority(workInProgress);
|
137 |
|
138 |
|
139 |
|
140 | workInProgress.pendingProps = null;
|
141 | workInProgress.updateQueue = null;
|
142 |
|
143 | var returnFiber = workInProgress['return'];
|
144 |
|
145 | if (returnFiber) {
|
146 |
|
147 |
|
148 |
|
149 | if (!returnFiber.firstEffect) {
|
150 | returnFiber.firstEffect = workInProgress.firstEffect;
|
151 | }
|
152 | if (workInProgress.lastEffect) {
|
153 | if (returnFiber.lastEffect) {
|
154 | returnFiber.lastEffect.nextEffect = workInProgress.firstEffect;
|
155 | }
|
156 | returnFiber.lastEffect = workInProgress.lastEffect;
|
157 | }
|
158 | }
|
159 |
|
160 | if (next) {
|
161 |
|
162 | return next;
|
163 | } else if (workInProgress.sibling) {
|
164 |
|
165 | return workInProgress.sibling;
|
166 | } else if (returnFiber) {
|
167 |
|
168 | workInProgress = returnFiber;
|
169 | continue;
|
170 | } else {
|
171 |
|
172 | var _root = workInProgress.stateNode;
|
173 | if (_root.current === workInProgress) {
|
174 | throw new Error('Cannot commit the same tree as before. This is probably a bug ' + 'related to the return field.');
|
175 | }
|
176 | _root.current = workInProgress;
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | commitAllWork(workInProgress);
|
182 | var nextWork = findNextUnitOfWork();
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | return nextWork;
|
191 | }
|
192 | }
|
193 | }
|
194 |
|
195 | function performUnitOfWork(workInProgress) {
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | var current = workInProgress.alternate;
|
201 | var next = beginWork(current, workInProgress, nextPriorityLevel);
|
202 |
|
203 | if (next) {
|
204 |
|
205 | return next;
|
206 | } else {
|
207 |
|
208 | return completeUnitOfWork(workInProgress);
|
209 | }
|
210 | }
|
211 |
|
212 | function performDeferredWork(deadline) {
|
213 | if (!nextUnitOfWork) {
|
214 | nextUnitOfWork = findNextUnitOfWork();
|
215 | }
|
216 | while (nextUnitOfWork) {
|
217 | if (deadline.timeRemaining() > timeHeuristicForUnitOfWork) {
|
218 | nextUnitOfWork = performUnitOfWork(nextUnitOfWork);
|
219 | if (!nextUnitOfWork) {
|
220 |
|
221 | nextUnitOfWork = findNextUnitOfWork();
|
222 | }
|
223 | } else {
|
224 | scheduleDeferredCallback(performDeferredWork);
|
225 | return;
|
226 | }
|
227 | }
|
228 | }
|
229 |
|
230 | function scheduleDeferredWork(root, priority) {
|
231 |
|
232 |
|
233 |
|
234 | if (priority <= nextPriorityLevel) {
|
235 | nextUnitOfWork = null;
|
236 | }
|
237 |
|
238 |
|
239 | if (root.current.pendingWorkPriority === NoWork || priority <= root.current.pendingWorkPriority) {
|
240 | root.current.pendingWorkPriority = priority;
|
241 | }
|
242 |
|
243 | if (root.isScheduled) {
|
244 |
|
245 | return;
|
246 | }
|
247 | root.isScheduled = true;
|
248 | if (lastScheduledRoot) {
|
249 |
|
250 | lastScheduledRoot.nextScheduledRoot = root;
|
251 | lastScheduledRoot = root;
|
252 | } else {
|
253 |
|
254 | nextScheduledRoot = root;
|
255 | lastScheduledRoot = root;
|
256 | scheduleDeferredCallback(performDeferredWork);
|
257 | }
|
258 | }
|
259 |
|
260 | function performAnimationWork() {
|
261 |
|
262 | nextUnitOfWork = findNextUnitOfWork();
|
263 | while (nextUnitOfWork && nextPriorityLevel !== NoWork) {
|
264 | nextUnitOfWork = performUnitOfWork(nextUnitOfWork);
|
265 | if (!nextUnitOfWork) {
|
266 |
|
267 | nextUnitOfWork = findNextUnitOfWork();
|
268 | }
|
269 |
|
270 | if (nextPriorityLevel > AnimationPriority) {
|
271 | scheduleDeferredCallback(performDeferredWork);
|
272 | return;
|
273 | }
|
274 | }
|
275 | }
|
276 |
|
277 | function scheduleAnimationWork(root, priorityLevel) {
|
278 |
|
279 | if (root.current.pendingWorkPriority === NoWork || priorityLevel <= root.current.pendingWorkPriority) {
|
280 | root.current.pendingWorkPriority = priorityLevel;
|
281 | }
|
282 |
|
283 | if (root.isScheduled) {
|
284 |
|
285 | return;
|
286 | }
|
287 | root.isScheduled = true;
|
288 | if (lastScheduledRoot) {
|
289 |
|
290 | lastScheduledRoot.nextScheduledRoot = root;
|
291 | lastScheduledRoot = root;
|
292 | } else {
|
293 |
|
294 | nextScheduledRoot = root;
|
295 | lastScheduledRoot = root;
|
296 | scheduleAnimationCallback(performAnimationWork);
|
297 | }
|
298 | }
|
299 |
|
300 | function scheduleWork(root) {
|
301 | if (defaultPriority === SynchronousPriority) {
|
302 | throw new Error('Not implemented yet');
|
303 | }
|
304 |
|
305 | if (defaultPriority === NoWork) {
|
306 | return;
|
307 | }
|
308 | if (defaultPriority > AnimationPriority) {
|
309 | scheduleDeferredWork(root, defaultPriority);
|
310 | return;
|
311 | }
|
312 | scheduleAnimationWork(root, defaultPriority);
|
313 | }
|
314 |
|
315 | function performWithPriority(priorityLevel, fn) {
|
316 | var previousDefaultPriority = defaultPriority;
|
317 | defaultPriority = priorityLevel;
|
318 | try {
|
319 | fn();
|
320 | } finally {
|
321 | defaultPriority = previousDefaultPriority;
|
322 | }
|
323 | }
|
324 |
|
325 | scheduler = {
|
326 | scheduleWork: scheduleWork,
|
327 | scheduleDeferredWork: scheduleDeferredWork,
|
328 | performWithPriority: performWithPriority
|
329 | };
|
330 | return scheduler;
|
331 | }; |
\ | No newline at end of file |