1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | 'use strict';
|
11 |
|
12 | if (process.env.NODE_ENV !== "production") {
|
13 | (function() {
|
14 | 'use strict';
|
15 |
|
16 | var enableSchedulerDebugging = false;
|
17 | var enableProfiling = true;
|
18 |
|
19 | var currentTime = 0;
|
20 | var scheduledCallback = null;
|
21 | var scheduledTimeout = null;
|
22 | var timeoutTime = -1;
|
23 | var yieldedValues = null;
|
24 | var expectedNumberOfYields = -1;
|
25 | var didStop = false;
|
26 | var isFlushing = false;
|
27 | var needsPaint = false;
|
28 | var shouldYieldForPaint = false;
|
29 | function requestHostCallback(callback) {
|
30 | scheduledCallback = callback;
|
31 | }
|
32 | function requestHostTimeout(callback, ms) {
|
33 | scheduledTimeout = callback;
|
34 | timeoutTime = currentTime + ms;
|
35 | }
|
36 | function cancelHostTimeout() {
|
37 | scheduledTimeout = null;
|
38 | timeoutTime = -1;
|
39 | }
|
40 | function shouldYieldToHost() {
|
41 | if (expectedNumberOfYields !== -1 && yieldedValues !== null && yieldedValues.length >= expectedNumberOfYields || shouldYieldForPaint && needsPaint) {
|
42 |
|
43 | didStop = true;
|
44 | return true;
|
45 | }
|
46 |
|
47 | return false;
|
48 | }
|
49 | function getCurrentTime() {
|
50 | return currentTime;
|
51 | }
|
52 | function forceFrameRate() {
|
53 | }
|
54 |
|
55 | function unstable_flushNumberOfYields(count) {
|
56 | if (isFlushing) {
|
57 | throw new Error('Already flushing work.');
|
58 | }
|
59 |
|
60 | if (scheduledCallback !== null) {
|
61 | var cb = scheduledCallback;
|
62 | expectedNumberOfYields = count;
|
63 | isFlushing = true;
|
64 |
|
65 | try {
|
66 | var hasMoreWork = true;
|
67 |
|
68 | do {
|
69 | hasMoreWork = cb(true, currentTime);
|
70 | } while (hasMoreWork && !didStop);
|
71 |
|
72 | if (!hasMoreWork) {
|
73 | scheduledCallback = null;
|
74 | }
|
75 | } finally {
|
76 | expectedNumberOfYields = -1;
|
77 | didStop = false;
|
78 | isFlushing = false;
|
79 | }
|
80 | }
|
81 | }
|
82 | function unstable_flushUntilNextPaint() {
|
83 | if (isFlushing) {
|
84 | throw new Error('Already flushing work.');
|
85 | }
|
86 |
|
87 | if (scheduledCallback !== null) {
|
88 | var cb = scheduledCallback;
|
89 | shouldYieldForPaint = true;
|
90 | needsPaint = false;
|
91 | isFlushing = true;
|
92 |
|
93 | try {
|
94 | var hasMoreWork = true;
|
95 |
|
96 | do {
|
97 | hasMoreWork = cb(true, currentTime);
|
98 | } while (hasMoreWork && !didStop);
|
99 |
|
100 | if (!hasMoreWork) {
|
101 | scheduledCallback = null;
|
102 | }
|
103 | } finally {
|
104 | shouldYieldForPaint = false;
|
105 | didStop = false;
|
106 | isFlushing = false;
|
107 | }
|
108 | }
|
109 | }
|
110 | function unstable_flushExpired() {
|
111 | if (isFlushing) {
|
112 | throw new Error('Already flushing work.');
|
113 | }
|
114 |
|
115 | if (scheduledCallback !== null) {
|
116 | isFlushing = true;
|
117 |
|
118 | try {
|
119 | var hasMoreWork = scheduledCallback(false, currentTime);
|
120 |
|
121 | if (!hasMoreWork) {
|
122 | scheduledCallback = null;
|
123 | }
|
124 | } finally {
|
125 | isFlushing = false;
|
126 | }
|
127 | }
|
128 | }
|
129 | function unstable_flushAllWithoutAsserting() {
|
130 |
|
131 | if (isFlushing) {
|
132 | throw new Error('Already flushing work.');
|
133 | }
|
134 |
|
135 | if (scheduledCallback !== null) {
|
136 | var cb = scheduledCallback;
|
137 | isFlushing = true;
|
138 |
|
139 | try {
|
140 | var hasMoreWork = true;
|
141 |
|
142 | do {
|
143 | hasMoreWork = cb(true, currentTime);
|
144 | } while (hasMoreWork);
|
145 |
|
146 | if (!hasMoreWork) {
|
147 | scheduledCallback = null;
|
148 | }
|
149 |
|
150 | return true;
|
151 | } finally {
|
152 | isFlushing = false;
|
153 | }
|
154 | } else {
|
155 | return false;
|
156 | }
|
157 | }
|
158 | function unstable_clearYields() {
|
159 | if (yieldedValues === null) {
|
160 | return [];
|
161 | }
|
162 |
|
163 | var values = yieldedValues;
|
164 | yieldedValues = null;
|
165 | return values;
|
166 | }
|
167 | function unstable_flushAll() {
|
168 | if (yieldedValues !== null) {
|
169 | throw new Error('Log is not empty. Assert on the log of yielded values before ' + 'flushing additional work.');
|
170 | }
|
171 |
|
172 | unstable_flushAllWithoutAsserting();
|
173 |
|
174 | if (yieldedValues !== null) {
|
175 | throw new Error('While flushing work, something yielded a value. Use an ' + 'assertion helper to assert on the log of yielded values, e.g. ' + 'expect(Scheduler).toFlushAndYield([...])');
|
176 | }
|
177 | }
|
178 | function unstable_yieldValue(value) {
|
179 |
|
180 | if (console.log.name === 'disabledLog') {
|
181 |
|
182 |
|
183 | return;
|
184 | }
|
185 |
|
186 | if (yieldedValues === null) {
|
187 | yieldedValues = [value];
|
188 | } else {
|
189 | yieldedValues.push(value);
|
190 | }
|
191 | }
|
192 | function unstable_advanceTime(ms) {
|
193 |
|
194 | if (console.log.name === 'disabledLog') {
|
195 |
|
196 |
|
197 | return;
|
198 | }
|
199 |
|
200 | currentTime += ms;
|
201 |
|
202 | if (scheduledTimeout !== null && timeoutTime <= currentTime) {
|
203 | scheduledTimeout(currentTime);
|
204 | timeoutTime = -1;
|
205 | scheduledTimeout = null;
|
206 | }
|
207 | }
|
208 | function requestPaint() {
|
209 | needsPaint = true;
|
210 | }
|
211 |
|
212 | function push(heap, node) {
|
213 | var index = heap.length;
|
214 | heap.push(node);
|
215 | siftUp(heap, node, index);
|
216 | }
|
217 | function peek(heap) {
|
218 | var first = heap[0];
|
219 | return first === undefined ? null : first;
|
220 | }
|
221 | function pop(heap) {
|
222 | var first = heap[0];
|
223 |
|
224 | if (first !== undefined) {
|
225 | var last = heap.pop();
|
226 |
|
227 | if (last !== first) {
|
228 | heap[0] = last;
|
229 | siftDown(heap, last, 0);
|
230 | }
|
231 |
|
232 | return first;
|
233 | } else {
|
234 | return null;
|
235 | }
|
236 | }
|
237 |
|
238 | function siftUp(heap, node, i) {
|
239 | var index = i;
|
240 |
|
241 | while (true) {
|
242 | var parentIndex = index - 1 >>> 1;
|
243 | var parent = heap[parentIndex];
|
244 |
|
245 | if (parent !== undefined && compare(parent, node) > 0) {
|
246 |
|
247 | heap[parentIndex] = node;
|
248 | heap[index] = parent;
|
249 | index = parentIndex;
|
250 | } else {
|
251 |
|
252 | return;
|
253 | }
|
254 | }
|
255 | }
|
256 |
|
257 | function siftDown(heap, node, i) {
|
258 | var index = i;
|
259 | var length = heap.length;
|
260 |
|
261 | while (index < length) {
|
262 | var leftIndex = (index + 1) * 2 - 1;
|
263 | var left = heap[leftIndex];
|
264 | var rightIndex = leftIndex + 1;
|
265 | var right = heap[rightIndex];
|
266 |
|
267 | if (left !== undefined && compare(left, node) < 0) {
|
268 | if (right !== undefined && compare(right, left) < 0) {
|
269 | heap[index] = right;
|
270 | heap[rightIndex] = node;
|
271 | index = rightIndex;
|
272 | } else {
|
273 | heap[index] = left;
|
274 | heap[leftIndex] = node;
|
275 | index = leftIndex;
|
276 | }
|
277 | } else if (right !== undefined && compare(right, node) < 0) {
|
278 | heap[index] = right;
|
279 | heap[rightIndex] = node;
|
280 | index = rightIndex;
|
281 | } else {
|
282 |
|
283 | return;
|
284 | }
|
285 | }
|
286 | }
|
287 |
|
288 | function compare(a, b) {
|
289 |
|
290 | var diff = a.sortIndex - b.sortIndex;
|
291 | return diff !== 0 ? diff : a.id - b.id;
|
292 | }
|
293 |
|
294 |
|
295 | var NoPriority = 0;
|
296 | var ImmediatePriority = 1;
|
297 | var UserBlockingPriority = 2;
|
298 | var NormalPriority = 3;
|
299 | var LowPriority = 4;
|
300 | var IdlePriority = 5;
|
301 |
|
302 | var runIdCounter = 0;
|
303 | var mainThreadIdCounter = 0;
|
304 | var profilingStateSize = 4;
|
305 | var sharedProfilingBuffer =
|
306 | typeof SharedArrayBuffer === 'function' ? new SharedArrayBuffer(profilingStateSize * Int32Array.BYTES_PER_ELEMENT) :
|
307 | typeof ArrayBuffer === 'function' ? new ArrayBuffer(profilingStateSize * Int32Array.BYTES_PER_ELEMENT) : null
|
308 | ;
|
309 | var profilingState = sharedProfilingBuffer !== null ? new Int32Array(sharedProfilingBuffer) : [];
|
310 |
|
311 | var PRIORITY = 0;
|
312 | var CURRENT_TASK_ID = 1;
|
313 | var CURRENT_RUN_ID = 2;
|
314 | var QUEUE_SIZE = 3;
|
315 |
|
316 | {
|
317 | profilingState[PRIORITY] = NoPriority;
|
318 |
|
319 |
|
320 | profilingState[QUEUE_SIZE] = 0;
|
321 | profilingState[CURRENT_TASK_ID] = 0;
|
322 | }
|
323 |
|
324 |
|
325 | var INITIAL_EVENT_LOG_SIZE = 131072;
|
326 | var MAX_EVENT_LOG_SIZE = 524288;
|
327 |
|
328 | var eventLogSize = 0;
|
329 | var eventLogBuffer = null;
|
330 | var eventLog = null;
|
331 | var eventLogIndex = 0;
|
332 | var TaskStartEvent = 1;
|
333 | var TaskCompleteEvent = 2;
|
334 | var TaskErrorEvent = 3;
|
335 | var TaskCancelEvent = 4;
|
336 | var TaskRunEvent = 5;
|
337 | var TaskYieldEvent = 6;
|
338 | var SchedulerSuspendEvent = 7;
|
339 | var SchedulerResumeEvent = 8;
|
340 |
|
341 | function logEvent(entries) {
|
342 | if (eventLog !== null) {
|
343 | var offset = eventLogIndex;
|
344 | eventLogIndex += entries.length;
|
345 |
|
346 | if (eventLogIndex + 1 > eventLogSize) {
|
347 | eventLogSize *= 2;
|
348 |
|
349 | if (eventLogSize > MAX_EVENT_LOG_SIZE) {
|
350 |
|
351 | console['error']("Scheduler Profiling: Event log exceeded maximum size. Don't " + 'forget to call `stopLoggingProfilingEvents()`.');
|
352 | stopLoggingProfilingEvents();
|
353 | return;
|
354 | }
|
355 |
|
356 | var newEventLog = new Int32Array(eventLogSize * 4);
|
357 | newEventLog.set(eventLog);
|
358 | eventLogBuffer = newEventLog.buffer;
|
359 | eventLog = newEventLog;
|
360 | }
|
361 |
|
362 | eventLog.set(entries, offset);
|
363 | }
|
364 | }
|
365 |
|
366 | function startLoggingProfilingEvents() {
|
367 | eventLogSize = INITIAL_EVENT_LOG_SIZE;
|
368 | eventLogBuffer = new ArrayBuffer(eventLogSize * 4);
|
369 | eventLog = new Int32Array(eventLogBuffer);
|
370 | eventLogIndex = 0;
|
371 | }
|
372 | function stopLoggingProfilingEvents() {
|
373 | var buffer = eventLogBuffer;
|
374 | eventLogSize = 0;
|
375 | eventLogBuffer = null;
|
376 | eventLog = null;
|
377 | eventLogIndex = 0;
|
378 | return buffer;
|
379 | }
|
380 | function markTaskStart(task, ms) {
|
381 | {
|
382 | profilingState[QUEUE_SIZE]++;
|
383 |
|
384 | if (eventLog !== null) {
|
385 |
|
386 |
|
387 |
|
388 | logEvent([TaskStartEvent, ms * 1000, task.id, task.priorityLevel]);
|
389 | }
|
390 | }
|
391 | }
|
392 | function markTaskCompleted(task, ms) {
|
393 | {
|
394 | profilingState[PRIORITY] = NoPriority;
|
395 | profilingState[CURRENT_TASK_ID] = 0;
|
396 | profilingState[QUEUE_SIZE]--;
|
397 |
|
398 | if (eventLog !== null) {
|
399 | logEvent([TaskCompleteEvent, ms * 1000, task.id]);
|
400 | }
|
401 | }
|
402 | }
|
403 | function markTaskCanceled(task, ms) {
|
404 | {
|
405 | profilingState[QUEUE_SIZE]--;
|
406 |
|
407 | if (eventLog !== null) {
|
408 | logEvent([TaskCancelEvent, ms * 1000, task.id]);
|
409 | }
|
410 | }
|
411 | }
|
412 | function markTaskErrored(task, ms) {
|
413 | {
|
414 | profilingState[PRIORITY] = NoPriority;
|
415 | profilingState[CURRENT_TASK_ID] = 0;
|
416 | profilingState[QUEUE_SIZE]--;
|
417 |
|
418 | if (eventLog !== null) {
|
419 | logEvent([TaskErrorEvent, ms * 1000, task.id]);
|
420 | }
|
421 | }
|
422 | }
|
423 | function markTaskRun(task, ms) {
|
424 | {
|
425 | runIdCounter++;
|
426 | profilingState[PRIORITY] = task.priorityLevel;
|
427 | profilingState[CURRENT_TASK_ID] = task.id;
|
428 | profilingState[CURRENT_RUN_ID] = runIdCounter;
|
429 |
|
430 | if (eventLog !== null) {
|
431 | logEvent([TaskRunEvent, ms * 1000, task.id, runIdCounter]);
|
432 | }
|
433 | }
|
434 | }
|
435 | function markTaskYield(task, ms) {
|
436 | {
|
437 | profilingState[PRIORITY] = NoPriority;
|
438 | profilingState[CURRENT_TASK_ID] = 0;
|
439 | profilingState[CURRENT_RUN_ID] = 0;
|
440 |
|
441 | if (eventLog !== null) {
|
442 | logEvent([TaskYieldEvent, ms * 1000, task.id, runIdCounter]);
|
443 | }
|
444 | }
|
445 | }
|
446 | function markSchedulerSuspended(ms) {
|
447 | {
|
448 | mainThreadIdCounter++;
|
449 |
|
450 | if (eventLog !== null) {
|
451 | logEvent([SchedulerSuspendEvent, ms * 1000, mainThreadIdCounter]);
|
452 | }
|
453 | }
|
454 | }
|
455 | function markSchedulerUnsuspended(ms) {
|
456 | {
|
457 | if (eventLog !== null) {
|
458 | logEvent([SchedulerResumeEvent, ms * 1000, mainThreadIdCounter]);
|
459 | }
|
460 | }
|
461 | }
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 | var maxSigned31BitInt = 1073741823;
|
468 |
|
469 | var IMMEDIATE_PRIORITY_TIMEOUT = -1;
|
470 |
|
471 | var USER_BLOCKING_PRIORITY_TIMEOUT = 250;
|
472 | var NORMAL_PRIORITY_TIMEOUT = 5000;
|
473 | var LOW_PRIORITY_TIMEOUT = 10000;
|
474 |
|
475 | var IDLE_PRIORITY_TIMEOUT = maxSigned31BitInt;
|
476 |
|
477 | var taskQueue = [];
|
478 | var timerQueue = [];
|
479 |
|
480 | var taskIdCounter = 1;
|
481 | var currentTask = null;
|
482 | var currentPriorityLevel = NormalPriority;
|
483 |
|
484 | var isPerformingWork = false;
|
485 | var isHostCallbackScheduled = false;
|
486 | var isHostTimeoutScheduled = false;
|
487 |
|
488 | function advanceTimers(currentTime) {
|
489 |
|
490 | var timer = peek(timerQueue);
|
491 |
|
492 | while (timer !== null) {
|
493 | if (timer.callback === null) {
|
494 |
|
495 | pop(timerQueue);
|
496 | } else if (timer.startTime <= currentTime) {
|
497 |
|
498 | pop(timerQueue);
|
499 | timer.sortIndex = timer.expirationTime;
|
500 | push(taskQueue, timer);
|
501 |
|
502 | {
|
503 | markTaskStart(timer, currentTime);
|
504 | timer.isQueued = true;
|
505 | }
|
506 | } else {
|
507 |
|
508 | return;
|
509 | }
|
510 |
|
511 | timer = peek(timerQueue);
|
512 | }
|
513 | }
|
514 |
|
515 | function handleTimeout(currentTime) {
|
516 | isHostTimeoutScheduled = false;
|
517 | advanceTimers(currentTime);
|
518 |
|
519 | if (!isHostCallbackScheduled) {
|
520 | if (peek(taskQueue) !== null) {
|
521 | isHostCallbackScheduled = true;
|
522 | requestHostCallback(flushWork);
|
523 | } else {
|
524 | var firstTimer = peek(timerQueue);
|
525 |
|
526 | if (firstTimer !== null) {
|
527 | requestHostTimeout(handleTimeout, firstTimer.startTime - currentTime);
|
528 | }
|
529 | }
|
530 | }
|
531 | }
|
532 |
|
533 | function flushWork(hasTimeRemaining, initialTime) {
|
534 | {
|
535 | markSchedulerUnsuspended(initialTime);
|
536 | }
|
537 |
|
538 |
|
539 | isHostCallbackScheduled = false;
|
540 |
|
541 | if (isHostTimeoutScheduled) {
|
542 |
|
543 | isHostTimeoutScheduled = false;
|
544 | cancelHostTimeout();
|
545 | }
|
546 |
|
547 | isPerformingWork = true;
|
548 | var previousPriorityLevel = currentPriorityLevel;
|
549 |
|
550 | try {
|
551 | if (enableProfiling) {
|
552 | try {
|
553 | return workLoop(hasTimeRemaining, initialTime);
|
554 | } catch (error) {
|
555 | if (currentTask !== null) {
|
556 | var currentTime = getCurrentTime();
|
557 | markTaskErrored(currentTask, currentTime);
|
558 | currentTask.isQueued = false;
|
559 | }
|
560 |
|
561 | throw error;
|
562 | }
|
563 | } else {
|
564 |
|
565 | return workLoop(hasTimeRemaining, initialTime);
|
566 | }
|
567 | } finally {
|
568 | currentTask = null;
|
569 | currentPriorityLevel = previousPriorityLevel;
|
570 | isPerformingWork = false;
|
571 |
|
572 | {
|
573 | var _currentTime = getCurrentTime();
|
574 |
|
575 | markSchedulerSuspended(_currentTime);
|
576 | }
|
577 | }
|
578 | }
|
579 |
|
580 | function workLoop(hasTimeRemaining, initialTime) {
|
581 | var currentTime = initialTime;
|
582 | advanceTimers(currentTime);
|
583 | currentTask = peek(taskQueue);
|
584 |
|
585 | while (currentTask !== null && !(enableSchedulerDebugging )) {
|
586 | if (currentTask.expirationTime > currentTime && (!hasTimeRemaining || shouldYieldToHost())) {
|
587 |
|
588 | break;
|
589 | }
|
590 |
|
591 | var callback = currentTask.callback;
|
592 |
|
593 | if (typeof callback === 'function') {
|
594 | currentTask.callback = null;
|
595 | currentPriorityLevel = currentTask.priorityLevel;
|
596 | var didUserCallbackTimeout = currentTask.expirationTime <= currentTime;
|
597 | markTaskRun(currentTask, currentTime);
|
598 | var continuationCallback = callback(didUserCallbackTimeout);
|
599 | currentTime = getCurrentTime();
|
600 |
|
601 | if (typeof continuationCallback === 'function') {
|
602 | currentTask.callback = continuationCallback;
|
603 | markTaskYield(currentTask, currentTime);
|
604 | } else {
|
605 | {
|
606 | markTaskCompleted(currentTask, currentTime);
|
607 | currentTask.isQueued = false;
|
608 | }
|
609 |
|
610 | if (currentTask === peek(taskQueue)) {
|
611 | pop(taskQueue);
|
612 | }
|
613 | }
|
614 |
|
615 | advanceTimers(currentTime);
|
616 | } else {
|
617 | pop(taskQueue);
|
618 | }
|
619 |
|
620 | currentTask = peek(taskQueue);
|
621 | }
|
622 |
|
623 |
|
624 | if (currentTask !== null) {
|
625 | return true;
|
626 | } else {
|
627 | var firstTimer = peek(timerQueue);
|
628 |
|
629 | if (firstTimer !== null) {
|
630 | requestHostTimeout(handleTimeout, firstTimer.startTime - currentTime);
|
631 | }
|
632 |
|
633 | return false;
|
634 | }
|
635 | }
|
636 |
|
637 | function unstable_runWithPriority(priorityLevel, eventHandler) {
|
638 | switch (priorityLevel) {
|
639 | case ImmediatePriority:
|
640 | case UserBlockingPriority:
|
641 | case NormalPriority:
|
642 | case LowPriority:
|
643 | case IdlePriority:
|
644 | break;
|
645 |
|
646 | default:
|
647 | priorityLevel = NormalPriority;
|
648 | }
|
649 |
|
650 | var previousPriorityLevel = currentPriorityLevel;
|
651 | currentPriorityLevel = priorityLevel;
|
652 |
|
653 | try {
|
654 | return eventHandler();
|
655 | } finally {
|
656 | currentPriorityLevel = previousPriorityLevel;
|
657 | }
|
658 | }
|
659 |
|
660 | function unstable_next(eventHandler) {
|
661 | var priorityLevel;
|
662 |
|
663 | switch (currentPriorityLevel) {
|
664 | case ImmediatePriority:
|
665 | case UserBlockingPriority:
|
666 | case NormalPriority:
|
667 |
|
668 | priorityLevel = NormalPriority;
|
669 | break;
|
670 |
|
671 | default:
|
672 |
|
673 | priorityLevel = currentPriorityLevel;
|
674 | break;
|
675 | }
|
676 |
|
677 | var previousPriorityLevel = currentPriorityLevel;
|
678 | currentPriorityLevel = priorityLevel;
|
679 |
|
680 | try {
|
681 | return eventHandler();
|
682 | } finally {
|
683 | currentPriorityLevel = previousPriorityLevel;
|
684 | }
|
685 | }
|
686 |
|
687 | function unstable_wrapCallback(callback) {
|
688 | var parentPriorityLevel = currentPriorityLevel;
|
689 | return function () {
|
690 |
|
691 | var previousPriorityLevel = currentPriorityLevel;
|
692 | currentPriorityLevel = parentPriorityLevel;
|
693 |
|
694 | try {
|
695 | return callback.apply(this, arguments);
|
696 | } finally {
|
697 | currentPriorityLevel = previousPriorityLevel;
|
698 | }
|
699 | };
|
700 | }
|
701 |
|
702 | function unstable_scheduleCallback(priorityLevel, callback, options) {
|
703 | var currentTime = getCurrentTime();
|
704 | var startTime;
|
705 |
|
706 | if (typeof options === 'object' && options !== null) {
|
707 | var delay = options.delay;
|
708 |
|
709 | if (typeof delay === 'number' && delay > 0) {
|
710 | startTime = currentTime + delay;
|
711 | } else {
|
712 | startTime = currentTime;
|
713 | }
|
714 | } else {
|
715 | startTime = currentTime;
|
716 | }
|
717 |
|
718 | var timeout;
|
719 |
|
720 | switch (priorityLevel) {
|
721 | case ImmediatePriority:
|
722 | timeout = IMMEDIATE_PRIORITY_TIMEOUT;
|
723 | break;
|
724 |
|
725 | case UserBlockingPriority:
|
726 | timeout = USER_BLOCKING_PRIORITY_TIMEOUT;
|
727 | break;
|
728 |
|
729 | case IdlePriority:
|
730 | timeout = IDLE_PRIORITY_TIMEOUT;
|
731 | break;
|
732 |
|
733 | case LowPriority:
|
734 | timeout = LOW_PRIORITY_TIMEOUT;
|
735 | break;
|
736 |
|
737 | case NormalPriority:
|
738 | default:
|
739 | timeout = NORMAL_PRIORITY_TIMEOUT;
|
740 | break;
|
741 | }
|
742 |
|
743 | var expirationTime = startTime + timeout;
|
744 | var newTask = {
|
745 | id: taskIdCounter++,
|
746 | callback: callback,
|
747 | priorityLevel: priorityLevel,
|
748 | startTime: startTime,
|
749 | expirationTime: expirationTime,
|
750 | sortIndex: -1
|
751 | };
|
752 |
|
753 | {
|
754 | newTask.isQueued = false;
|
755 | }
|
756 |
|
757 | if (startTime > currentTime) {
|
758 |
|
759 | newTask.sortIndex = startTime;
|
760 | push(timerQueue, newTask);
|
761 |
|
762 | if (peek(taskQueue) === null && newTask === peek(timerQueue)) {
|
763 |
|
764 | if (isHostTimeoutScheduled) {
|
765 |
|
766 | cancelHostTimeout();
|
767 | } else {
|
768 | isHostTimeoutScheduled = true;
|
769 | }
|
770 |
|
771 |
|
772 | requestHostTimeout(handleTimeout, startTime - currentTime);
|
773 | }
|
774 | } else {
|
775 | newTask.sortIndex = expirationTime;
|
776 | push(taskQueue, newTask);
|
777 |
|
778 | {
|
779 | markTaskStart(newTask, currentTime);
|
780 | newTask.isQueued = true;
|
781 | }
|
782 |
|
783 |
|
784 |
|
785 | if (!isHostCallbackScheduled && !isPerformingWork) {
|
786 | isHostCallbackScheduled = true;
|
787 | requestHostCallback(flushWork);
|
788 | }
|
789 | }
|
790 |
|
791 | return newTask;
|
792 | }
|
793 |
|
794 | function unstable_pauseExecution() {
|
795 | }
|
796 |
|
797 | function unstable_continueExecution() {
|
798 |
|
799 | if (!isHostCallbackScheduled && !isPerformingWork) {
|
800 | isHostCallbackScheduled = true;
|
801 | requestHostCallback(flushWork);
|
802 | }
|
803 | }
|
804 |
|
805 | function unstable_getFirstCallbackNode() {
|
806 | return peek(taskQueue);
|
807 | }
|
808 |
|
809 | function unstable_cancelCallback(task) {
|
810 | {
|
811 | if (task.isQueued) {
|
812 | var currentTime = getCurrentTime();
|
813 | markTaskCanceled(task, currentTime);
|
814 | task.isQueued = false;
|
815 | }
|
816 | }
|
817 |
|
818 |
|
819 |
|
820 |
|
821 | task.callback = null;
|
822 | }
|
823 |
|
824 | function unstable_getCurrentPriorityLevel() {
|
825 | return currentPriorityLevel;
|
826 | }
|
827 |
|
828 | var unstable_requestPaint = requestPaint;
|
829 | var unstable_Profiling = {
|
830 | startLoggingProfilingEvents: startLoggingProfilingEvents,
|
831 | stopLoggingProfilingEvents: stopLoggingProfilingEvents,
|
832 | sharedProfilingBuffer: sharedProfilingBuffer
|
833 | } ;
|
834 |
|
835 | exports.unstable_IdlePriority = IdlePriority;
|
836 | exports.unstable_ImmediatePriority = ImmediatePriority;
|
837 | exports.unstable_LowPriority = LowPriority;
|
838 | exports.unstable_NormalPriority = NormalPriority;
|
839 | exports.unstable_Profiling = unstable_Profiling;
|
840 | exports.unstable_UserBlockingPriority = UserBlockingPriority;
|
841 | exports.unstable_advanceTime = unstable_advanceTime;
|
842 | exports.unstable_cancelCallback = unstable_cancelCallback;
|
843 | exports.unstable_clearYields = unstable_clearYields;
|
844 | exports.unstable_continueExecution = unstable_continueExecution;
|
845 | exports.unstable_flushAll = unstable_flushAll;
|
846 | exports.unstable_flushAllWithoutAsserting = unstable_flushAllWithoutAsserting;
|
847 | exports.unstable_flushExpired = unstable_flushExpired;
|
848 | exports.unstable_flushNumberOfYields = unstable_flushNumberOfYields;
|
849 | exports.unstable_flushUntilNextPaint = unstable_flushUntilNextPaint;
|
850 | exports.unstable_forceFrameRate = forceFrameRate;
|
851 | exports.unstable_getCurrentPriorityLevel = unstable_getCurrentPriorityLevel;
|
852 | exports.unstable_getFirstCallbackNode = unstable_getFirstCallbackNode;
|
853 | exports.unstable_next = unstable_next;
|
854 | exports.unstable_now = getCurrentTime;
|
855 | exports.unstable_pauseExecution = unstable_pauseExecution;
|
856 | exports.unstable_requestPaint = unstable_requestPaint;
|
857 | exports.unstable_runWithPriority = unstable_runWithPriority;
|
858 | exports.unstable_scheduleCallback = unstable_scheduleCallback;
|
859 | exports.unstable_shouldYield = shouldYieldToHost;
|
860 | exports.unstable_wrapCallback = unstable_wrapCallback;
|
861 | exports.unstable_yieldValue = unstable_yieldValue;
|
862 | })();
|
863 | }
|