source: imaps-frontend/node_modules/scheduler/umd/scheduler-unstable_mock.development.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 18.3 KB
Line 
1/**
2 * @license React
3 * scheduler-unstable_mock.development.js
4 *
5 * Copyright (c) Facebook, Inc. and its affiliates.
6 *
7 * This source code is licensed under the MIT license found in the
8 * LICENSE file in the root directory of this source tree.
9 */
10(function (global, factory) {
11 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
12 typeof define === 'function' && define.amd ? define(['exports'], factory) :
13 (global = global || self, factory(global.SchedulerMock = {}));
14}(this, (function (exports) { 'use strict';
15
16 var enableSchedulerDebugging = false;
17 var enableProfiling = false;
18
19 function push(heap, node) {
20 var index = heap.length;
21 heap.push(node);
22 siftUp(heap, node, index);
23 }
24 function peek(heap) {
25 return heap.length === 0 ? null : heap[0];
26 }
27 function pop(heap) {
28 if (heap.length === 0) {
29 return null;
30 }
31
32 var first = heap[0];
33 var last = heap.pop();
34
35 if (last !== first) {
36 heap[0] = last;
37 siftDown(heap, last, 0);
38 }
39
40 return first;
41 }
42
43 function siftUp(heap, node, i) {
44 var index = i;
45
46 while (index > 0) {
47 var parentIndex = index - 1 >>> 1;
48 var parent = heap[parentIndex];
49
50 if (compare(parent, node) > 0) {
51 // The parent is larger. Swap positions.
52 heap[parentIndex] = node;
53 heap[index] = parent;
54 index = parentIndex;
55 } else {
56 // The parent is smaller. Exit.
57 return;
58 }
59 }
60 }
61
62 function siftDown(heap, node, i) {
63 var index = i;
64 var length = heap.length;
65 var halfLength = length >>> 1;
66
67 while (index < halfLength) {
68 var leftIndex = (index + 1) * 2 - 1;
69 var left = heap[leftIndex];
70 var rightIndex = leftIndex + 1;
71 var right = heap[rightIndex]; // If the left or right node is smaller, swap with the smaller of those.
72
73 if (compare(left, node) < 0) {
74 if (rightIndex < length && compare(right, left) < 0) {
75 heap[index] = right;
76 heap[rightIndex] = node;
77 index = rightIndex;
78 } else {
79 heap[index] = left;
80 heap[leftIndex] = node;
81 index = leftIndex;
82 }
83 } else if (rightIndex < length && compare(right, node) < 0) {
84 heap[index] = right;
85 heap[rightIndex] = node;
86 index = rightIndex;
87 } else {
88 // Neither child is smaller. Exit.
89 return;
90 }
91 }
92 }
93
94 function compare(a, b) {
95 // Compare sort index first, then task id.
96 var diff = a.sortIndex - b.sortIndex;
97 return diff !== 0 ? diff : a.id - b.id;
98 }
99
100 // TODO: Use symbols?
101 var ImmediatePriority = 1;
102 var UserBlockingPriority = 2;
103 var NormalPriority = 3;
104 var LowPriority = 4;
105 var IdlePriority = 5;
106
107 function markTaskErrored(task, ms) {
108 }
109
110 /* eslint-disable no-var */
111 // Math.pow(2, 30) - 1
112 // 0b111111111111111111111111111111
113
114 var maxSigned31BitInt = 1073741823; // Times out immediately
115
116 var IMMEDIATE_PRIORITY_TIMEOUT = -1; // Eventually times out
117
118 var USER_BLOCKING_PRIORITY_TIMEOUT = 250;
119 var NORMAL_PRIORITY_TIMEOUT = 5000;
120 var LOW_PRIORITY_TIMEOUT = 10000; // Never times out
121
122 var IDLE_PRIORITY_TIMEOUT = maxSigned31BitInt; // Tasks are stored on a min heap
123
124 var taskQueue = [];
125 var timerQueue = []; // Incrementing id counter. Used to maintain insertion order.
126
127 var taskIdCounter = 1; // Pausing the scheduler is useful for debugging.
128 var currentTask = null;
129 var currentPriorityLevel = NormalPriority; // This is set while performing work, to prevent re-entrance.
130
131 var isPerformingWork = false;
132 var isHostCallbackScheduled = false;
133 var isHostTimeoutScheduled = false;
134 var currentMockTime = 0;
135 var scheduledCallback = null;
136 var scheduledTimeout = null;
137 var timeoutTime = -1;
138 var yieldedValues = null;
139 var expectedNumberOfYields = -1;
140 var didStop = false;
141 var isFlushing = false;
142 var needsPaint = false;
143 var shouldYieldForPaint = false;
144 var disableYieldValue = false;
145
146 function setDisableYieldValue(newValue) {
147 disableYieldValue = newValue;
148 }
149
150 function advanceTimers(currentTime) {
151 // Check for tasks that are no longer delayed and add them to the queue.
152 var timer = peek(timerQueue);
153
154 while (timer !== null) {
155 if (timer.callback === null) {
156 // Timer was cancelled.
157 pop(timerQueue);
158 } else if (timer.startTime <= currentTime) {
159 // Timer fired. Transfer to the task queue.
160 pop(timerQueue);
161 timer.sortIndex = timer.expirationTime;
162 push(taskQueue, timer);
163 } else {
164 // Remaining timers are pending.
165 return;
166 }
167
168 timer = peek(timerQueue);
169 }
170 }
171
172 function handleTimeout(currentTime) {
173 isHostTimeoutScheduled = false;
174 advanceTimers(currentTime);
175
176 if (!isHostCallbackScheduled) {
177 if (peek(taskQueue) !== null) {
178 isHostCallbackScheduled = true;
179 requestHostCallback(flushWork);
180 } else {
181 var firstTimer = peek(timerQueue);
182
183 if (firstTimer !== null) {
184 requestHostTimeout(handleTimeout, firstTimer.startTime - currentTime);
185 }
186 }
187 }
188 }
189
190 function flushWork(hasTimeRemaining, initialTime) {
191
192
193 isHostCallbackScheduled = false;
194
195 if (isHostTimeoutScheduled) {
196 // We scheduled a timeout but it's no longer needed. Cancel it.
197 isHostTimeoutScheduled = false;
198 cancelHostTimeout();
199 }
200
201 isPerformingWork = true;
202 var previousPriorityLevel = currentPriorityLevel;
203
204 try {
205 if (enableProfiling) {
206 try {
207 return workLoop(hasTimeRemaining, initialTime);
208 } catch (error) {
209 if (currentTask !== null) {
210 var currentTime = getCurrentTime();
211 markTaskErrored(currentTask, currentTime);
212 currentTask.isQueued = false;
213 }
214
215 throw error;
216 }
217 } else {
218 // No catch in prod code path.
219 return workLoop(hasTimeRemaining, initialTime);
220 }
221 } finally {
222 currentTask = null;
223 currentPriorityLevel = previousPriorityLevel;
224 isPerformingWork = false;
225 }
226 }
227
228 function workLoop(hasTimeRemaining, initialTime) {
229 var currentTime = initialTime;
230 advanceTimers(currentTime);
231 currentTask = peek(taskQueue);
232
233 while (currentTask !== null && !(enableSchedulerDebugging )) {
234 if (currentTask.expirationTime > currentTime && (!hasTimeRemaining || shouldYieldToHost())) {
235 // This currentTask hasn't expired, and we've reached the deadline.
236 break;
237 }
238
239 var callback = currentTask.callback;
240
241 if (typeof callback === 'function') {
242 currentTask.callback = null;
243 currentPriorityLevel = currentTask.priorityLevel;
244 var didUserCallbackTimeout = currentTask.expirationTime <= currentTime;
245
246 var continuationCallback = callback(didUserCallbackTimeout);
247 currentTime = getCurrentTime();
248
249 if (typeof continuationCallback === 'function') {
250 currentTask.callback = continuationCallback;
251 } else {
252
253 if (currentTask === peek(taskQueue)) {
254 pop(taskQueue);
255 }
256 }
257
258 advanceTimers(currentTime);
259 } else {
260 pop(taskQueue);
261 }
262
263 currentTask = peek(taskQueue);
264 } // Return whether there's additional work
265
266
267 if (currentTask !== null) {
268 return true;
269 } else {
270 var firstTimer = peek(timerQueue);
271
272 if (firstTimer !== null) {
273 requestHostTimeout(handleTimeout, firstTimer.startTime - currentTime);
274 }
275
276 return false;
277 }
278 }
279
280 function unstable_runWithPriority(priorityLevel, eventHandler) {
281 switch (priorityLevel) {
282 case ImmediatePriority:
283 case UserBlockingPriority:
284 case NormalPriority:
285 case LowPriority:
286 case IdlePriority:
287 break;
288
289 default:
290 priorityLevel = NormalPriority;
291 }
292
293 var previousPriorityLevel = currentPriorityLevel;
294 currentPriorityLevel = priorityLevel;
295
296 try {
297 return eventHandler();
298 } finally {
299 currentPriorityLevel = previousPriorityLevel;
300 }
301 }
302
303 function unstable_next(eventHandler) {
304 var priorityLevel;
305
306 switch (currentPriorityLevel) {
307 case ImmediatePriority:
308 case UserBlockingPriority:
309 case NormalPriority:
310 // Shift down to normal priority
311 priorityLevel = NormalPriority;
312 break;
313
314 default:
315 // Anything lower than normal priority should remain at the current level.
316 priorityLevel = currentPriorityLevel;
317 break;
318 }
319
320 var previousPriorityLevel = currentPriorityLevel;
321 currentPriorityLevel = priorityLevel;
322
323 try {
324 return eventHandler();
325 } finally {
326 currentPriorityLevel = previousPriorityLevel;
327 }
328 }
329
330 function unstable_wrapCallback(callback) {
331 var parentPriorityLevel = currentPriorityLevel;
332 return function () {
333 // This is a fork of runWithPriority, inlined for performance.
334 var previousPriorityLevel = currentPriorityLevel;
335 currentPriorityLevel = parentPriorityLevel;
336
337 try {
338 return callback.apply(this, arguments);
339 } finally {
340 currentPriorityLevel = previousPriorityLevel;
341 }
342 };
343 }
344
345 function unstable_scheduleCallback(priorityLevel, callback, options) {
346 var currentTime = getCurrentTime();
347 var startTime;
348
349 if (typeof options === 'object' && options !== null) {
350 var delay = options.delay;
351
352 if (typeof delay === 'number' && delay > 0) {
353 startTime = currentTime + delay;
354 } else {
355 startTime = currentTime;
356 }
357 } else {
358 startTime = currentTime;
359 }
360
361 var timeout;
362
363 switch (priorityLevel) {
364 case ImmediatePriority:
365 timeout = IMMEDIATE_PRIORITY_TIMEOUT;
366 break;
367
368 case UserBlockingPriority:
369 timeout = USER_BLOCKING_PRIORITY_TIMEOUT;
370 break;
371
372 case IdlePriority:
373 timeout = IDLE_PRIORITY_TIMEOUT;
374 break;
375
376 case LowPriority:
377 timeout = LOW_PRIORITY_TIMEOUT;
378 break;
379
380 case NormalPriority:
381 default:
382 timeout = NORMAL_PRIORITY_TIMEOUT;
383 break;
384 }
385
386 var expirationTime = startTime + timeout;
387 var newTask = {
388 id: taskIdCounter++,
389 callback: callback,
390 priorityLevel: priorityLevel,
391 startTime: startTime,
392 expirationTime: expirationTime,
393 sortIndex: -1
394 };
395
396 if (startTime > currentTime) {
397 // This is a delayed task.
398 newTask.sortIndex = startTime;
399 push(timerQueue, newTask);
400
401 if (peek(taskQueue) === null && newTask === peek(timerQueue)) {
402 // All tasks are delayed, and this is the task with the earliest delay.
403 if (isHostTimeoutScheduled) {
404 // Cancel an existing timeout.
405 cancelHostTimeout();
406 } else {
407 isHostTimeoutScheduled = true;
408 } // Schedule a timeout.
409
410
411 requestHostTimeout(handleTimeout, startTime - currentTime);
412 }
413 } else {
414 newTask.sortIndex = expirationTime;
415 push(taskQueue, newTask);
416 // wait until the next time we yield.
417
418
419 if (!isHostCallbackScheduled && !isPerformingWork) {
420 isHostCallbackScheduled = true;
421 requestHostCallback(flushWork);
422 }
423 }
424
425 return newTask;
426 }
427
428 function unstable_pauseExecution() {
429 }
430
431 function unstable_continueExecution() {
432
433 if (!isHostCallbackScheduled && !isPerformingWork) {
434 isHostCallbackScheduled = true;
435 requestHostCallback(flushWork);
436 }
437 }
438
439 function unstable_getFirstCallbackNode() {
440 return peek(taskQueue);
441 }
442
443 function unstable_cancelCallback(task) {
444 // remove from the queue because you can't remove arbitrary nodes from an
445 // array based heap, only the first one.)
446
447
448 task.callback = null;
449 }
450
451 function unstable_getCurrentPriorityLevel() {
452 return currentPriorityLevel;
453 }
454
455 function requestHostCallback(callback) {
456 scheduledCallback = callback;
457 }
458
459 function requestHostTimeout(callback, ms) {
460 scheduledTimeout = callback;
461 timeoutTime = currentMockTime + ms;
462 }
463
464 function cancelHostTimeout() {
465 scheduledTimeout = null;
466 timeoutTime = -1;
467 }
468
469 function shouldYieldToHost() {
470 if (expectedNumberOfYields === 0 && yieldedValues === null || expectedNumberOfYields !== -1 && yieldedValues !== null && yieldedValues.length >= expectedNumberOfYields || shouldYieldForPaint && needsPaint) {
471 // We yielded at least as many values as expected. Stop flushing.
472 didStop = true;
473 return true;
474 }
475
476 return false;
477 }
478
479 function getCurrentTime() {
480 return currentMockTime;
481 }
482
483 function forceFrameRate() {// No-op
484 }
485
486 function reset() {
487 if (isFlushing) {
488 throw new Error('Cannot reset while already flushing work.');
489 }
490
491 currentMockTime = 0;
492 scheduledCallback = null;
493 scheduledTimeout = null;
494 timeoutTime = -1;
495 yieldedValues = null;
496 expectedNumberOfYields = -1;
497 didStop = false;
498 isFlushing = false;
499 needsPaint = false;
500 } // Should only be used via an assertion helper that inspects the yielded values.
501
502
503 function unstable_flushNumberOfYields(count) {
504 if (isFlushing) {
505 throw new Error('Already flushing work.');
506 }
507
508 if (scheduledCallback !== null) {
509 var cb = scheduledCallback;
510 expectedNumberOfYields = count;
511 isFlushing = true;
512
513 try {
514 var hasMoreWork = true;
515
516 do {
517 hasMoreWork = cb(true, currentMockTime);
518 } while (hasMoreWork && !didStop);
519
520 if (!hasMoreWork) {
521 scheduledCallback = null;
522 }
523 } finally {
524 expectedNumberOfYields = -1;
525 didStop = false;
526 isFlushing = false;
527 }
528 }
529 }
530
531 function unstable_flushUntilNextPaint() {
532 if (isFlushing) {
533 throw new Error('Already flushing work.');
534 }
535
536 if (scheduledCallback !== null) {
537 var cb = scheduledCallback;
538 shouldYieldForPaint = true;
539 needsPaint = false;
540 isFlushing = true;
541
542 try {
543 var hasMoreWork = true;
544
545 do {
546 hasMoreWork = cb(true, currentMockTime);
547 } while (hasMoreWork && !didStop);
548
549 if (!hasMoreWork) {
550 scheduledCallback = null;
551 }
552 } finally {
553 shouldYieldForPaint = false;
554 didStop = false;
555 isFlushing = false;
556 }
557 }
558 }
559
560 function unstable_flushExpired() {
561 if (isFlushing) {
562 throw new Error('Already flushing work.');
563 }
564
565 if (scheduledCallback !== null) {
566 isFlushing = true;
567
568 try {
569 var hasMoreWork = scheduledCallback(false, currentMockTime);
570
571 if (!hasMoreWork) {
572 scheduledCallback = null;
573 }
574 } finally {
575 isFlushing = false;
576 }
577 }
578 }
579
580 function unstable_flushAllWithoutAsserting() {
581 // Returns false if no work was flushed.
582 if (isFlushing) {
583 throw new Error('Already flushing work.');
584 }
585
586 if (scheduledCallback !== null) {
587 var cb = scheduledCallback;
588 isFlushing = true;
589
590 try {
591 var hasMoreWork = true;
592
593 do {
594 hasMoreWork = cb(true, currentMockTime);
595 } while (hasMoreWork);
596
597 if (!hasMoreWork) {
598 scheduledCallback = null;
599 }
600
601 return true;
602 } finally {
603 isFlushing = false;
604 }
605 } else {
606 return false;
607 }
608 }
609
610 function unstable_clearYields() {
611 if (yieldedValues === null) {
612 return [];
613 }
614
615 var values = yieldedValues;
616 yieldedValues = null;
617 return values;
618 }
619
620 function unstable_flushAll() {
621 if (yieldedValues !== null) {
622 throw new Error('Log is not empty. Assert on the log of yielded values before ' + 'flushing additional work.');
623 }
624
625 unstable_flushAllWithoutAsserting();
626
627 if (yieldedValues !== null) {
628 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([...])');
629 }
630 }
631
632 function unstable_yieldValue(value) {
633 // eslint-disable-next-line react-internal/no-production-logging
634 if (console.log.name === 'disabledLog' || disableYieldValue) {
635 // If console.log has been patched, we assume we're in render
636 // replaying and we ignore any values yielding in the second pass.
637 return;
638 }
639
640 if (yieldedValues === null) {
641 yieldedValues = [value];
642 } else {
643 yieldedValues.push(value);
644 }
645 }
646
647 function unstable_advanceTime(ms) {
648 // eslint-disable-next-line react-internal/no-production-logging
649 if (console.log.name === 'disabledLog' || disableYieldValue) {
650 // If console.log has been patched, we assume we're in render
651 // replaying and we ignore any time advancing in the second pass.
652 return;
653 }
654
655 currentMockTime += ms;
656
657 if (scheduledTimeout !== null && timeoutTime <= currentMockTime) {
658 scheduledTimeout(currentMockTime);
659 timeoutTime = -1;
660 scheduledTimeout = null;
661 }
662 }
663
664 function requestPaint() {
665 needsPaint = true;
666 }
667 var unstable_Profiling = null;
668
669 exports.reset = reset;
670 exports.unstable_IdlePriority = IdlePriority;
671 exports.unstable_ImmediatePriority = ImmediatePriority;
672 exports.unstable_LowPriority = LowPriority;
673 exports.unstable_NormalPriority = NormalPriority;
674 exports.unstable_Profiling = unstable_Profiling;
675 exports.unstable_UserBlockingPriority = UserBlockingPriority;
676 exports.unstable_advanceTime = unstable_advanceTime;
677 exports.unstable_cancelCallback = unstable_cancelCallback;
678 exports.unstable_clearYields = unstable_clearYields;
679 exports.unstable_continueExecution = unstable_continueExecution;
680 exports.unstable_flushAll = unstable_flushAll;
681 exports.unstable_flushAllWithoutAsserting = unstable_flushAllWithoutAsserting;
682 exports.unstable_flushExpired = unstable_flushExpired;
683 exports.unstable_flushNumberOfYields = unstable_flushNumberOfYields;
684 exports.unstable_flushUntilNextPaint = unstable_flushUntilNextPaint;
685 exports.unstable_forceFrameRate = forceFrameRate;
686 exports.unstable_getCurrentPriorityLevel = unstable_getCurrentPriorityLevel;
687 exports.unstable_getFirstCallbackNode = unstable_getFirstCallbackNode;
688 exports.unstable_next = unstable_next;
689 exports.unstable_now = getCurrentTime;
690 exports.unstable_pauseExecution = unstable_pauseExecution;
691 exports.unstable_requestPaint = requestPaint;
692 exports.unstable_runWithPriority = unstable_runWithPriority;
693 exports.unstable_scheduleCallback = unstable_scheduleCallback;
694 exports.unstable_setDisableYieldValue = setDisableYieldValue;
695 exports.unstable_shouldYield = shouldYieldToHost;
696 exports.unstable_wrapCallback = unstable_wrapCallback;
697 exports.unstable_yieldValue = unstable_yieldValue;
698
699})));
Note: See TracBrowser for help on using the repository browser.