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