1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | (function (global, factory) {
|
10 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
11 | typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
12 | (global = global || self, factory(global.SchedulerMock = {}));
|
13 | }(this, (function (exports) { 'use strict';
|
14 |
|
15 | var enableSchedulerDebugging = false;
|
16 | var enableProfiling = false;
|
17 |
|
18 | var currentTime = 0;
|
19 | var scheduledCallback = null;
|
20 | var scheduledTimeout = null;
|
21 | var timeoutTime = -1;
|
22 | var yieldedValues = null;
|
23 | var expectedNumberOfYields = -1;
|
24 | var didStop = false;
|
25 | var isFlushing = false;
|
26 | var needsPaint = false;
|
27 | var shouldYieldForPaint = false;
|
28 | function requestHostCallback(callback) {
|
29 | scheduledCallback = callback;
|
30 | }
|
31 | function requestHostTimeout(callback, ms) {
|
32 | scheduledTimeout = callback;
|
33 | timeoutTime = currentTime + ms;
|
34 | }
|
35 | function cancelHostTimeout() {
|
36 | scheduledTimeout = null;
|
37 | timeoutTime = -1;
|
38 | }
|
39 | function shouldYieldToHost() {
|
40 | if (expectedNumberOfYields !== -1 && yieldedValues !== null && yieldedValues.length >= expectedNumberOfYields || shouldYieldForPaint && needsPaint) {
|
41 |
|
42 | didStop = true;
|
43 | return true;
|
44 | }
|
45 |
|
46 | return false;
|
47 | }
|
48 | function getCurrentTime() {
|
49 | return currentTime;
|
50 | }
|
51 | function forceFrameRate() {
|
52 | }
|
53 |
|
54 | function unstable_flushNumberOfYields(count) {
|
55 | if (isFlushing) {
|
56 | throw new Error('Already flushing work.');
|
57 | }
|
58 |
|
59 | if (scheduledCallback !== null) {
|
60 | var cb = scheduledCallback;
|
61 | expectedNumberOfYields = count;
|
62 | isFlushing = true;
|
63 |
|
64 | try {
|
65 | var hasMoreWork = true;
|
66 |
|
67 | do {
|
68 | hasMoreWork = cb(true, currentTime);
|
69 | } while (hasMoreWork && !didStop);
|
70 |
|
71 | if (!hasMoreWork) {
|
72 | scheduledCallback = null;
|
73 | }
|
74 | } finally {
|
75 | expectedNumberOfYields = -1;
|
76 | didStop = false;
|
77 | isFlushing = false;
|
78 | }
|
79 | }
|
80 | }
|
81 | function unstable_flushUntilNextPaint() {
|
82 | if (isFlushing) {
|
83 | throw new Error('Already flushing work.');
|
84 | }
|
85 |
|
86 | if (scheduledCallback !== null) {
|
87 | var cb = scheduledCallback;
|
88 | shouldYieldForPaint = true;
|
89 | needsPaint = false;
|
90 | isFlushing = true;
|
91 |
|
92 | try {
|
93 | var hasMoreWork = true;
|
94 |
|
95 | do {
|
96 | hasMoreWork = cb(true, currentTime);
|
97 | } while (hasMoreWork && !didStop);
|
98 |
|
99 | if (!hasMoreWork) {
|
100 | scheduledCallback = null;
|
101 | }
|
102 | } finally {
|
103 | shouldYieldForPaint = false;
|
104 | didStop = false;
|
105 | isFlushing = false;
|
106 | }
|
107 | }
|
108 | }
|
109 | function unstable_flushExpired() {
|
110 | if (isFlushing) {
|
111 | throw new Error('Already flushing work.');
|
112 | }
|
113 |
|
114 | if (scheduledCallback !== null) {
|
115 | isFlushing = true;
|
116 |
|
117 | try {
|
118 | var hasMoreWork = scheduledCallback(false, currentTime);
|
119 |
|
120 | if (!hasMoreWork) {
|
121 | scheduledCallback = null;
|
122 | }
|
123 | } finally {
|
124 | isFlushing = false;
|
125 | }
|
126 | }
|
127 | }
|
128 | function unstable_flushAllWithoutAsserting() {
|
129 |
|
130 | if (isFlushing) {
|
131 | throw new Error('Already flushing work.');
|
132 | }
|
133 |
|
134 | if (scheduledCallback !== null) {
|
135 | var cb = scheduledCallback;
|
136 | isFlushing = true;
|
137 |
|
138 | try {
|
139 | var hasMoreWork = true;
|
140 |
|
141 | do {
|
142 | hasMoreWork = cb(true, currentTime);
|
143 | } while (hasMoreWork);
|
144 |
|
145 | if (!hasMoreWork) {
|
146 | scheduledCallback = null;
|
147 | }
|
148 |
|
149 | return true;
|
150 | } finally {
|
151 | isFlushing = false;
|
152 | }
|
153 | } else {
|
154 | return false;
|
155 | }
|
156 | }
|
157 | function unstable_clearYields() {
|
158 | if (yieldedValues === null) {
|
159 | return [];
|
160 | }
|
161 |
|
162 | var values = yieldedValues;
|
163 | yieldedValues = null;
|
164 | return values;
|
165 | }
|
166 | function unstable_flushAll() {
|
167 | if (yieldedValues !== null) {
|
168 | throw new Error('Log is not empty. Assert on the log of yielded values before ' + 'flushing additional work.');
|
169 | }
|
170 |
|
171 | unstable_flushAllWithoutAsserting();
|
172 |
|
173 | if (yieldedValues !== null) {
|
174 | 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([...])');
|
175 | }
|
176 | }
|
177 | function unstable_yieldValue(value) {
|
178 |
|
179 | if (console.log.name === 'disabledLog') {
|
180 |
|
181 |
|
182 | return;
|
183 | }
|
184 |
|
185 | if (yieldedValues === null) {
|
186 | yieldedValues = [value];
|
187 | } else {
|
188 | yieldedValues.push(value);
|
189 | }
|
190 | }
|
191 | function unstable_advanceTime(ms) {
|
192 |
|
193 | if (console.log.name === 'disabledLog') {
|
194 |
|
195 |
|
196 | return;
|
197 | }
|
198 |
|
199 | currentTime += ms;
|
200 |
|
201 | if (scheduledTimeout !== null && timeoutTime <= currentTime) {
|
202 | scheduledTimeout(currentTime);
|
203 | timeoutTime = -1;
|
204 | scheduledTimeout = null;
|
205 | }
|
206 | }
|
207 | function requestPaint() {
|
208 | needsPaint = true;
|
209 | }
|
210 |
|
211 | function push(heap, node) {
|
212 | var index = heap.length;
|
213 | heap.push(node);
|
214 | siftUp(heap, node, index);
|
215 | }
|
216 | function peek(heap) {
|
217 | var first = heap[0];
|
218 | return first === undefined ? null : first;
|
219 | }
|
220 | function pop(heap) {
|
221 | var first = heap[0];
|
222 |
|
223 | if (first !== undefined) {
|
224 | var last = heap.pop();
|
225 |
|
226 | if (last !== first) {
|
227 | heap[0] = last;
|
228 | siftDown(heap, last, 0);
|
229 | }
|
230 |
|
231 | return first;
|
232 | } else {
|
233 | return null;
|
234 | }
|
235 | }
|
236 |
|
237 | function siftUp(heap, node, i) {
|
238 | var index = i;
|
239 |
|
240 | while (true) {
|
241 | var parentIndex = index - 1 >>> 1;
|
242 | var parent = heap[parentIndex];
|
243 |
|
244 | if (parent !== undefined && compare(parent, node) > 0) {
|
245 |
|
246 | heap[parentIndex] = node;
|
247 | heap[index] = parent;
|
248 | index = parentIndex;
|
249 | } else {
|
250 |
|
251 | return;
|
252 | }
|
253 | }
|
254 | }
|
255 |
|
256 | function siftDown(heap, node, i) {
|
257 | var index = i;
|
258 | var length = heap.length;
|
259 |
|
260 | while (index < length) {
|
261 | var leftIndex = (index + 1) * 2 - 1;
|
262 | var left = heap[leftIndex];
|
263 | var rightIndex = leftIndex + 1;
|
264 | var right = heap[rightIndex];
|
265 |
|
266 | if (left !== undefined && compare(left, node) < 0) {
|
267 | if (right !== undefined && compare(right, left) < 0) {
|
268 | heap[index] = right;
|
269 | heap[rightIndex] = node;
|
270 | index = rightIndex;
|
271 | } else {
|
272 | heap[index] = left;
|
273 | heap[leftIndex] = node;
|
274 | index = leftIndex;
|
275 | }
|
276 | } else if (right !== undefined && compare(right, node) < 0) {
|
277 | heap[index] = right;
|
278 | heap[rightIndex] = node;
|
279 | index = rightIndex;
|
280 | } else {
|
281 |
|
282 | return;
|
283 | }
|
284 | }
|
285 | }
|
286 |
|
287 | function compare(a, b) {
|
288 |
|
289 | var diff = a.sortIndex - b.sortIndex;
|
290 | return diff !== 0 ? diff : a.id - b.id;
|
291 | }
|
292 |
|
293 |
|
294 | var ImmediatePriority = 1;
|
295 | var UserBlockingPriority = 2;
|
296 | var NormalPriority = 3;
|
297 | var LowPriority = 4;
|
298 | var IdlePriority = 5;
|
299 |
|
300 | function markTaskErrored(task, ms) {
|
301 | }
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 | var maxSigned31BitInt = 1073741823;
|
308 |
|
309 | var IMMEDIATE_PRIORITY_TIMEOUT = -1;
|
310 |
|
311 | var USER_BLOCKING_PRIORITY_TIMEOUT = 250;
|
312 | var NORMAL_PRIORITY_TIMEOUT = 5000;
|
313 | var LOW_PRIORITY_TIMEOUT = 10000;
|
314 |
|
315 | var IDLE_PRIORITY_TIMEOUT = maxSigned31BitInt;
|
316 |
|
317 | var taskQueue = [];
|
318 | var timerQueue = [];
|
319 |
|
320 | var taskIdCounter = 1;
|
321 | var currentTask = null;
|
322 | var currentPriorityLevel = NormalPriority;
|
323 |
|
324 | var isPerformingWork = false;
|
325 | var isHostCallbackScheduled = false;
|
326 | var isHostTimeoutScheduled = false;
|
327 |
|
328 | function advanceTimers(currentTime) {
|
329 |
|
330 | var timer = peek(timerQueue);
|
331 |
|
332 | while (timer !== null) {
|
333 | if (timer.callback === null) {
|
334 |
|
335 | pop(timerQueue);
|
336 | } else if (timer.startTime <= currentTime) {
|
337 |
|
338 | pop(timerQueue);
|
339 | timer.sortIndex = timer.expirationTime;
|
340 | push(taskQueue, timer);
|
341 | } else {
|
342 |
|
343 | return;
|
344 | }
|
345 |
|
346 | timer = peek(timerQueue);
|
347 | }
|
348 | }
|
349 |
|
350 | function handleTimeout(currentTime) {
|
351 | isHostTimeoutScheduled = false;
|
352 | advanceTimers(currentTime);
|
353 |
|
354 | if (!isHostCallbackScheduled) {
|
355 | if (peek(taskQueue) !== null) {
|
356 | isHostCallbackScheduled = true;
|
357 | requestHostCallback(flushWork);
|
358 | } else {
|
359 | var firstTimer = peek(timerQueue);
|
360 |
|
361 | if (firstTimer !== null) {
|
362 | requestHostTimeout(handleTimeout, firstTimer.startTime - currentTime);
|
363 | }
|
364 | }
|
365 | }
|
366 | }
|
367 |
|
368 | function flushWork(hasTimeRemaining, initialTime) {
|
369 |
|
370 |
|
371 | isHostCallbackScheduled = false;
|
372 |
|
373 | if (isHostTimeoutScheduled) {
|
374 |
|
375 | isHostTimeoutScheduled = false;
|
376 | cancelHostTimeout();
|
377 | }
|
378 |
|
379 | isPerformingWork = true;
|
380 | var previousPriorityLevel = currentPriorityLevel;
|
381 |
|
382 | try {
|
383 | if (enableProfiling) {
|
384 | try {
|
385 | return workLoop(hasTimeRemaining, initialTime);
|
386 | } catch (error) {
|
387 | if (currentTask !== null) {
|
388 | var currentTime = getCurrentTime();
|
389 | markTaskErrored(currentTask, currentTime);
|
390 | currentTask.isQueued = false;
|
391 | }
|
392 |
|
393 | throw error;
|
394 | }
|
395 | } else {
|
396 |
|
397 | return workLoop(hasTimeRemaining, initialTime);
|
398 | }
|
399 | } finally {
|
400 | currentTask = null;
|
401 | currentPriorityLevel = previousPriorityLevel;
|
402 | isPerformingWork = false;
|
403 | }
|
404 | }
|
405 |
|
406 | function workLoop(hasTimeRemaining, initialTime) {
|
407 | var currentTime = initialTime;
|
408 | advanceTimers(currentTime);
|
409 | currentTask = peek(taskQueue);
|
410 |
|
411 | while (currentTask !== null && !(enableSchedulerDebugging )) {
|
412 | if (currentTask.expirationTime > currentTime && (!hasTimeRemaining || shouldYieldToHost())) {
|
413 |
|
414 | break;
|
415 | }
|
416 |
|
417 | var callback = currentTask.callback;
|
418 |
|
419 | if (typeof callback === 'function') {
|
420 | currentTask.callback = null;
|
421 | currentPriorityLevel = currentTask.priorityLevel;
|
422 | var didUserCallbackTimeout = currentTask.expirationTime <= currentTime;
|
423 |
|
424 | var continuationCallback = callback(didUserCallbackTimeout);
|
425 | currentTime = getCurrentTime();
|
426 |
|
427 | if (typeof continuationCallback === 'function') {
|
428 | currentTask.callback = continuationCallback;
|
429 | } else {
|
430 |
|
431 | if (currentTask === peek(taskQueue)) {
|
432 | pop(taskQueue);
|
433 | }
|
434 | }
|
435 |
|
436 | advanceTimers(currentTime);
|
437 | } else {
|
438 | pop(taskQueue);
|
439 | }
|
440 |
|
441 | currentTask = peek(taskQueue);
|
442 | }
|
443 |
|
444 |
|
445 | if (currentTask !== null) {
|
446 | return true;
|
447 | } else {
|
448 | var firstTimer = peek(timerQueue);
|
449 |
|
450 | if (firstTimer !== null) {
|
451 | requestHostTimeout(handleTimeout, firstTimer.startTime - currentTime);
|
452 | }
|
453 |
|
454 | return false;
|
455 | }
|
456 | }
|
457 |
|
458 | function unstable_runWithPriority(priorityLevel, eventHandler) {
|
459 | switch (priorityLevel) {
|
460 | case ImmediatePriority:
|
461 | case UserBlockingPriority:
|
462 | case NormalPriority:
|
463 | case LowPriority:
|
464 | case IdlePriority:
|
465 | break;
|
466 |
|
467 | default:
|
468 | priorityLevel = NormalPriority;
|
469 | }
|
470 |
|
471 | var previousPriorityLevel = currentPriorityLevel;
|
472 | currentPriorityLevel = priorityLevel;
|
473 |
|
474 | try {
|
475 | return eventHandler();
|
476 | } finally {
|
477 | currentPriorityLevel = previousPriorityLevel;
|
478 | }
|
479 | }
|
480 |
|
481 | function unstable_next(eventHandler) {
|
482 | var priorityLevel;
|
483 |
|
484 | switch (currentPriorityLevel) {
|
485 | case ImmediatePriority:
|
486 | case UserBlockingPriority:
|
487 | case NormalPriority:
|
488 |
|
489 | priorityLevel = NormalPriority;
|
490 | break;
|
491 |
|
492 | default:
|
493 |
|
494 | priorityLevel = currentPriorityLevel;
|
495 | break;
|
496 | }
|
497 |
|
498 | var previousPriorityLevel = currentPriorityLevel;
|
499 | currentPriorityLevel = priorityLevel;
|
500 |
|
501 | try {
|
502 | return eventHandler();
|
503 | } finally {
|
504 | currentPriorityLevel = previousPriorityLevel;
|
505 | }
|
506 | }
|
507 |
|
508 | function unstable_wrapCallback(callback) {
|
509 | var parentPriorityLevel = currentPriorityLevel;
|
510 | return function () {
|
511 |
|
512 | var previousPriorityLevel = currentPriorityLevel;
|
513 | currentPriorityLevel = parentPriorityLevel;
|
514 |
|
515 | try {
|
516 | return callback.apply(this, arguments);
|
517 | } finally {
|
518 | currentPriorityLevel = previousPriorityLevel;
|
519 | }
|
520 | };
|
521 | }
|
522 |
|
523 | function unstable_scheduleCallback(priorityLevel, callback, options) {
|
524 | var currentTime = getCurrentTime();
|
525 | var startTime;
|
526 |
|
527 | if (typeof options === 'object' && options !== null) {
|
528 | var delay = options.delay;
|
529 |
|
530 | if (typeof delay === 'number' && delay > 0) {
|
531 | startTime = currentTime + delay;
|
532 | } else {
|
533 | startTime = currentTime;
|
534 | }
|
535 | } else {
|
536 | startTime = currentTime;
|
537 | }
|
538 |
|
539 | var timeout;
|
540 |
|
541 | switch (priorityLevel) {
|
542 | case ImmediatePriority:
|
543 | timeout = IMMEDIATE_PRIORITY_TIMEOUT;
|
544 | break;
|
545 |
|
546 | case UserBlockingPriority:
|
547 | timeout = USER_BLOCKING_PRIORITY_TIMEOUT;
|
548 | break;
|
549 |
|
550 | case IdlePriority:
|
551 | timeout = IDLE_PRIORITY_TIMEOUT;
|
552 | break;
|
553 |
|
554 | case LowPriority:
|
555 | timeout = LOW_PRIORITY_TIMEOUT;
|
556 | break;
|
557 |
|
558 | case NormalPriority:
|
559 | default:
|
560 | timeout = NORMAL_PRIORITY_TIMEOUT;
|
561 | break;
|
562 | }
|
563 |
|
564 | var expirationTime = startTime + timeout;
|
565 | var newTask = {
|
566 | id: taskIdCounter++,
|
567 | callback: callback,
|
568 | priorityLevel: priorityLevel,
|
569 | startTime: startTime,
|
570 | expirationTime: expirationTime,
|
571 | sortIndex: -1
|
572 | };
|
573 |
|
574 | if (startTime > currentTime) {
|
575 |
|
576 | newTask.sortIndex = startTime;
|
577 | push(timerQueue, newTask);
|
578 |
|
579 | if (peek(taskQueue) === null && newTask === peek(timerQueue)) {
|
580 |
|
581 | if (isHostTimeoutScheduled) {
|
582 |
|
583 | cancelHostTimeout();
|
584 | } else {
|
585 | isHostTimeoutScheduled = true;
|
586 | }
|
587 |
|
588 |
|
589 | requestHostTimeout(handleTimeout, startTime - currentTime);
|
590 | }
|
591 | } else {
|
592 | newTask.sortIndex = expirationTime;
|
593 | push(taskQueue, newTask);
|
594 |
|
595 |
|
596 |
|
597 | if (!isHostCallbackScheduled && !isPerformingWork) {
|
598 | isHostCallbackScheduled = true;
|
599 | requestHostCallback(flushWork);
|
600 | }
|
601 | }
|
602 |
|
603 | return newTask;
|
604 | }
|
605 |
|
606 | function unstable_pauseExecution() {
|
607 | }
|
608 |
|
609 | function unstable_continueExecution() {
|
610 |
|
611 | if (!isHostCallbackScheduled && !isPerformingWork) {
|
612 | isHostCallbackScheduled = true;
|
613 | requestHostCallback(flushWork);
|
614 | }
|
615 | }
|
616 |
|
617 | function unstable_getFirstCallbackNode() {
|
618 | return peek(taskQueue);
|
619 | }
|
620 |
|
621 | function unstable_cancelCallback(task) {
|
622 |
|
623 |
|
624 |
|
625 |
|
626 | task.callback = null;
|
627 | }
|
628 |
|
629 | function unstable_getCurrentPriorityLevel() {
|
630 | return currentPriorityLevel;
|
631 | }
|
632 |
|
633 | var unstable_requestPaint = requestPaint;
|
634 | var unstable_Profiling = null;
|
635 |
|
636 | exports.unstable_IdlePriority = IdlePriority;
|
637 | exports.unstable_ImmediatePriority = ImmediatePriority;
|
638 | exports.unstable_LowPriority = LowPriority;
|
639 | exports.unstable_NormalPriority = NormalPriority;
|
640 | exports.unstable_Profiling = unstable_Profiling;
|
641 | exports.unstable_UserBlockingPriority = UserBlockingPriority;
|
642 | exports.unstable_advanceTime = unstable_advanceTime;
|
643 | exports.unstable_cancelCallback = unstable_cancelCallback;
|
644 | exports.unstable_clearYields = unstable_clearYields;
|
645 | exports.unstable_continueExecution = unstable_continueExecution;
|
646 | exports.unstable_flushAll = unstable_flushAll;
|
647 | exports.unstable_flushAllWithoutAsserting = unstable_flushAllWithoutAsserting;
|
648 | exports.unstable_flushExpired = unstable_flushExpired;
|
649 | exports.unstable_flushNumberOfYields = unstable_flushNumberOfYields;
|
650 | exports.unstable_flushUntilNextPaint = unstable_flushUntilNextPaint;
|
651 | exports.unstable_forceFrameRate = forceFrameRate;
|
652 | exports.unstable_getCurrentPriorityLevel = unstable_getCurrentPriorityLevel;
|
653 | exports.unstable_getFirstCallbackNode = unstable_getFirstCallbackNode;
|
654 | exports.unstable_next = unstable_next;
|
655 | exports.unstable_now = getCurrentTime;
|
656 | exports.unstable_pauseExecution = unstable_pauseExecution;
|
657 | exports.unstable_requestPaint = unstable_requestPaint;
|
658 | exports.unstable_runWithPriority = unstable_runWithPriority;
|
659 | exports.unstable_scheduleCallback = unstable_scheduleCallback;
|
660 | exports.unstable_shouldYield = shouldYieldToHost;
|
661 | exports.unstable_wrapCallback = unstable_wrapCallback;
|
662 | exports.unstable_yieldValue = unstable_yieldValue;
|
663 |
|
664 | })));
|