@@ -18,6 +18,7 @@ import type {Fiber} from 'react-reconciler/src/ReactFiber';
18
18
import type { UpdateQueue } from 'react-reconciler/src/ReactUpdateQueue' ;
19
19
import type { ReactNodeList } from 'shared/ReactTypes' ;
20
20
21
+ import * as Scheduler from 'scheduler/unstable_mock' ;
21
22
import { createPortal } from 'shared/ReactPortal' ;
22
23
import expect from 'expect' ;
23
24
import { REACT_FRAGMENT_TYPE , REACT_ELEMENT_TYPE } from 'shared/ReactSymbols' ;
@@ -51,9 +52,6 @@ if (__DEV__) {
51
52
}
52
53
53
54
function createReactNoop ( reconciler : Function , useMutation : boolean ) {
54
- let scheduledCallback = null ;
55
- let scheduledCallbackTimeout = - 1 ;
56
- let scheduledPassiveCallback = null ;
57
55
let instanceCounter = 0 ;
58
56
let hostDiffCounter = 0 ;
59
57
let hostUpdateCounter = 0 ;
@@ -218,8 +216,6 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
218
216
) ;
219
217
}
220
218
221
- let elapsedTimeInMs = 0 ;
222
-
223
219
const sharedHostConfig = {
224
220
getRootHostContext ( ) {
225
221
return NO_CONTEXT ;
@@ -308,66 +304,23 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
308
304
return inst ;
309
305
} ,
310
306
311
- scheduleDeferredCallback ( callback , options ) {
312
- if ( scheduledCallback ) {
313
- throw new Error (
314
- 'Scheduling a callback twice is excessive. Instead, keep track of ' +
315
- 'whether the callback has already been scheduled.' ,
316
- ) ;
317
- }
318
- scheduledCallback = callback ;
319
- if (
320
- typeof options === 'object' &&
321
- options !== null &&
322
- typeof options . timeout === 'number'
323
- ) {
324
- const newTimeout = options . timeout ;
325
- if (
326
- scheduledCallbackTimeout === - 1 ||
327
- scheduledCallbackTimeout > newTimeout
328
- ) {
329
- scheduledCallbackTimeout = elapsedTimeInMs + newTimeout ;
330
- }
331
- }
332
- return 0 ;
333
- } ,
334
-
335
- cancelDeferredCallback ( ) {
336
- if ( scheduledCallback === null ) {
337
- throw new Error ( 'No callback is scheduled.' ) ;
338
- }
339
- scheduledCallback = null ;
340
- scheduledCallbackTimeout = - 1 ;
341
- } ,
307
+ scheduleDeferredCallback : Scheduler . unstable_scheduleCallback ,
308
+ cancelDeferredCallback : Scheduler . unstable_cancelCallback ,
342
309
343
- shouldYield ,
310
+ shouldYield : Scheduler . unstable_shouldYield ,
344
311
345
312
scheduleTimeout : setTimeout ,
346
313
cancelTimeout : clearTimeout ,
347
314
noTimeout : - 1 ,
348
- schedulePassiveEffects ( callback ) {
349
- if ( scheduledCallback ) {
350
- throw new Error (
351
- 'Scheduling a callback twice is excessive. Instead, keep track of ' +
352
- 'whether the callback has already been scheduled.' ,
353
- ) ;
354
- }
355
- scheduledPassiveCallback = callback ;
356
- } ,
357
- cancelPassiveEffects ( ) {
358
- if ( scheduledPassiveCallback === null ) {
359
- throw new Error ( 'No passive effects callback is scheduled.' ) ;
360
- }
361
- scheduledPassiveCallback = null ;
362
- } ,
315
+
316
+ schedulePassiveEffects : Scheduler . unstable_scheduleCallback ,
317
+ cancelPassiveEffects : Scheduler . unstable_cancelCallback ,
363
318
364
319
prepareForCommit ( ) : void { } ,
365
320
366
321
resetAfterCommit ( ) : void { } ,
367
322
368
- now ( ) : number {
369
- return elapsedTimeInMs ;
370
- } ,
323
+ now : Scheduler . unstable_now ,
371
324
372
325
isPrimaryRenderer : true ,
373
326
supportsHydration : false ,
@@ -534,71 +487,6 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
534
487
const roots = new Map ( ) ;
535
488
const DEFAULT_ROOT_ID = '< default > ';
536
489
537
- let yieldedValues : Array < mixed > = [ ] ;
538
- let didStop : boolean = false ;
539
- let expectedNumberOfYields : number = - 1 ;
540
-
541
- function shouldYield ( ) {
542
- if (
543
- expectedNumberOfYields !== - 1 &&
544
- yieldedValues . length >= expectedNumberOfYields &&
545
- ( scheduledCallbackTimeout === - 1 ||
546
- elapsedTimeInMs < scheduledCallbackTimeout )
547
- ) {
548
- // We yielded at least as many values as expected. Stop rendering.
549
- didStop = true ;
550
- return true ;
551
- }
552
- // Keep rendering.
553
- return false ;
554
- }
555
-
556
- function flushAll ( ) : Array < mixed > {
557
- yieldedValues = [ ] ;
558
- while ( scheduledCallback !== null ) {
559
- const cb = scheduledCallback ;
560
- scheduledCallback = null ;
561
- const didTimeout =
562
- scheduledCallbackTimeout !== - 1 &&
563
- scheduledCallbackTimeout < elapsedTimeInMs ;
564
- cb ( didTimeout ) ;
565
- }
566
- const values = yieldedValues ;
567
- yieldedValues = [ ] ;
568
- return values ;
569
- }
570
-
571
- function flushNumberOfYields ( count : number ) : Array < mixed > {
572
- expectedNumberOfYields = count ;
573
- didStop = false ;
574
- yieldedValues = [ ] ;
575
- try {
576
- while ( scheduledCallback !== null && ! didStop ) {
577
- const cb = scheduledCallback ;
578
- scheduledCallback = null ;
579
- const didTimeout =
580
- scheduledCallbackTimeout !== - 1 &&
581
- scheduledCallbackTimeout < elapsedTimeInMs ;
582
- cb ( didTimeout ) ;
583
- }
584
- return yieldedValues ;
585
- } finally {
586
- expectedNumberOfYields = - 1 ;
587
- didStop = false ;
588
- yieldedValues = [ ] ;
589
- }
590
- }
591
-
592
- function yieldValue ( value : mixed ) : void {
593
- yieldedValues . push ( value ) ;
594
- }
595
-
596
- function clearYields ( ) : Array < mixed > {
597
- const values = yieldedValues ;
598
- yieldedValues = [ ] ;
599
- return values ;
600
- }
601
-
602
490
function childToJSX ( child , text ) {
603
491
if ( text !== null ) {
604
492
return text ;
@@ -653,6 +541,8 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
653
541
}
654
542
655
543
const ReactNoop = {
544
+ _Scheduler : Scheduler ,
545
+
656
546
getChildren ( rootID : string = DEFAULT_ROOT_ID ) {
657
547
const container = rootContainers . get ( rootID ) ;
658
548
if ( container ) {
@@ -763,14 +653,9 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
763
653
return NoopRenderer . findHostInstance ( component ) ;
764
654
} ,
765
655
766
- // TODO: Should only be used via a Jest plugin (like we do with the
767
- // test renderer).
768
- unstable_flushWithoutYielding: flushAll ,
769
- unstable_flushNumberOfYields : flushNumberOfYields ,
770
- unstable_clearYields : clearYields ,
771
-
772
656
flushNextYield ( ) : Array < mixed > {
773
- return flushNumberOfYields ( 1 ) ;
657
+ Scheduler. unstable_flushNumberOfYields ( 1 ) ;
658
+ return Scheduler . unstable_clearYields ( ) ;
774
659
} ,
775
660
776
661
flushWithHostCounters (
@@ -788,7 +673,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
788
673
hostUpdateCounter = 0 ;
789
674
hostCloneCounter = 0 ;
790
675
try {
791
- flushAll ( ) ;
676
+ Scheduler . flushAll ( ) ;
792
677
return useMutation
793
678
? {
794
679
hostDiffCounter ,
@@ -805,24 +690,13 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
805
690
}
806
691
} ,
807
692
808
- expire ( ms : number ) : Array < mixed > {
809
- ReactNoop. advanceTime ( ms ) ;
810
- return ReactNoop . flushExpired ( ) ;
811
- } ,
812
-
813
- advanceTime ( ms : number ) : void {
814
- elapsedTimeInMs + = ms ;
815
- } ,
693
+ expire : Scheduler . advanceTime ,
816
694
817
695
flushExpired ( ) : Array < mixed > {
818
- return flushNumberOfYields ( 0 ) ;
696
+ return Scheduler . unstable_flushExpired ( ) ;
819
697
} ,
820
698
821
- yield : yieldValue ,
822
-
823
- hasScheduledCallback ( ) {
824
- return ! ! scheduledCallback ;
825
- } ,
699
+ yield : Scheduler . yieldValue ,
826
700
827
701
batchedUpdates : NoopRenderer . batchedUpdates ,
828
702
@@ -870,9 +744,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
870
744
} ,
871
745
872
746
flushSync ( fn : ( ) => mixed ) {
873
- yieldedValues = [ ] ;
874
747
NoopRenderer . flushSync ( fn ) ;
875
- return yieldedValues ;
876
748
} ,
877
749
878
750
flushPassiveEffects ( ) {
@@ -997,12 +869,13 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
997
869
_next : null ,
998
870
} ;
999
871
root . firstBatch = batch ;
1000
- const actual = flushAll ( ) ;
872
+ Scheduler . unstable_flushWithoutYielding ( ) ;
873
+ const actual = Scheduler . unstable_clearYields ( ) ;
1001
874
expect ( actual ) . toEqual ( expectedFlush ) ;
1002
875
return ( expectedCommit : Array < mixed > ) => {
1003
876
batch. _defer = false ;
1004
877
NoopRenderer . flushRoot ( root , expiration ) ;
1005
- expect ( yieldedValues ) . toEqual ( expectedCommit ) ;
878
+ expect ( Scheduler . unstable_clearYields ( ) ) . toEqual ( expectedCommit ) ;
1006
879
} ;
1007
880
} ,
1008
881
0 commit comments