@@ -105,6 +105,20 @@ <h2>Tests:</h2>
105
105
< div > < b > Actual:</ b > </ div >
106
106
< div id ="test-8 "> </ div >
107
107
</ li >
108
+ < li >
109
+ < p > Can force a specific framerate</ p >
110
+ < p > < b > IMPORTANT:</ b > This test may be flaky if other tests have been run in this js instance. To get a clean test refresh the page before running test 9</ p >
111
+ < button onClick ="runTestNine() "> Run Test 9</ button >
112
+ < div > < b > Expected:</ b > </ div >
113
+ < div id ="test-9-expected ">
114
+ </ div >
115
+ < div > -------------------------------------------------</ div >
116
+ < div > If you see the same above and below it's correct.
117
+ < div > -------------------------------------------------</ div >
118
+ < div > < b > Actual:</ b > </ div >
119
+ < div id ="test-9 "> </ div >
120
+ </ div >
121
+ </ li >
108
122
</ ol >
109
123
< script src ="../../build/node_modules/react/umd/react.development.js "> </ script >
110
124
< script src ="../../build/node_modules/scheduler/umd/scheduler.development.js "> </ script >
@@ -117,6 +131,9 @@ <h2>Tests:</h2>
117
131
unstable_getFirstCallbackNode : getFirstCallbackNode ,
118
132
unstable_pauseExecution : pauseExecution ,
119
133
unstable_continueExecution : continueExecution ,
134
+ unstable_forceFrameRate : forceFrameRate ,
135
+ unstable_shouldYield : shouldYield ,
136
+ unstable_NormalPriority : NormalPriority ,
120
137
} = Scheduler ;
121
138
function displayTestResult ( testNumber ) {
122
139
const expectationNode = document . getElementById ( 'test-' + testNumber + '-expected' ) ;
@@ -188,7 +205,7 @@ <h2>Tests:</h2>
188
205
[
189
206
'scheduled Cb1' ,
190
207
'frame 1 started' ,
191
- 'cb1 called with argument of {"didTimeout": false} ' ,
208
+ 'cb1 called with argument of false' ,
192
209
'frame 2 started' ,
193
210
'frame 3 started... we stop counting now.' ,
194
211
] ,
@@ -197,8 +214,8 @@ <h2>Tests:</h2>
197
214
'scheduled CbA' ,
198
215
'scheduled CbB' ,
199
216
'frame 1 started' ,
200
- 'cbA called with argument of {"didTimeout": false} ' ,
201
- 'cbB called with argument of {"didTimeout": false} ' ,
217
+ 'cbA called with argument of false' ,
218
+ 'cbB called with argument of false' ,
202
219
'frame 2 started' ,
203
220
'frame 3 started... we stop counting now.' ,
204
221
] ,
@@ -208,9 +225,9 @@ <h2>Tests:</h2>
208
225
'scheduled CbB' ,
209
226
'frame 1 started' ,
210
227
'scheduled CbA again' ,
211
- 'cbA0 called with argument of {"didTimeout": false} ' ,
212
- 'cbB called with argument of {"didTimeout": false} ' ,
213
- 'cbA1 called with argument of {"didTimeout": false} ' ,
228
+ 'cbA0 called with argument of false' ,
229
+ 'cbB called with argument of false' ,
230
+ 'cbA1 called with argument of false' ,
214
231
'frame 2 started' ,
215
232
'frame 3 started... we stop counting now.' ,
216
233
] ,
@@ -222,11 +239,11 @@ <h2>Tests:</h2>
222
239
'scheduled cbD' ,
223
240
'frame 1 started' ,
224
241
'cbC called with argument of {"didTimeout":true}' ,
225
- 'cbA called with argument of {"didTimeout": false} ' ,
242
+ 'cbA called with argument of false' ,
226
243
'cbA running and taking some time' ,
227
244
'frame 2 started' ,
228
- 'cbB called with argument of {"didTimeout": false} ' ,
229
- 'cbD called with argument of {"didTimeout": false} ' ,
245
+ 'cbB called with argument of false' ,
246
+ 'cbD called with argument of false' ,
230
247
'frame 3 started... we stop counting now.' ,
231
248
] ,
232
249
// test 5
@@ -243,6 +260,13 @@ <h2>Tests:</h2>
243
260
'Finishing...' ,
244
261
'Done!' ,
245
262
] ,
263
+ // test 9
264
+ [
265
+ 'Forcing new frame times...' ,
266
+ 'Using new frame time!' ,
267
+ 'Using new frame time!' ,
268
+ 'Finished!' ,
269
+ ] ,
246
270
] ;
247
271
function runTestOne ( ) {
248
272
// Test 1
@@ -253,7 +277,7 @@ <h2>Tests:</h2>
253
277
const cb1 = ( x ) => {
254
278
updateTestResult ( 1 , 'cb1 called with argument of ' + JSON . stringify ( x ) ) ;
255
279
}
256
- scheduleCallback ( cb1 ) ;
280
+ scheduleCallback ( NormalPriority , cb1 ) ;
257
281
updateTestResult ( 1 , 'scheduled Cb1' ) ;
258
282
logWhenFramesStart ( 1 , ( ) => {
259
283
displayTestResult ( 1 ) ;
@@ -271,9 +295,9 @@ <h2>Tests:</h2>
271
295
const cbB = ( x ) => {
272
296
updateTestResult ( 2 , 'cbB called with argument of ' + JSON . stringify ( x ) ) ;
273
297
}
274
- scheduleCallback ( cbA ) ;
298
+ scheduleCallback ( NormalPriority , cbA ) ;
275
299
updateTestResult ( 2 , 'scheduled CbA' ) ;
276
- scheduleCallback ( cbB ) ;
300
+ scheduleCallback ( NormalPriority , cbB ) ;
277
301
updateTestResult ( 2 , 'scheduled CbB' ) ;
278
302
logWhenFramesStart ( 2 , ( ) => {
279
303
displayTestResult ( 2 ) ;
@@ -288,7 +312,7 @@ <h2>Tests:</h2>
288
312
let callbackAIterations = 0 ;
289
313
const cbA = ( x ) => {
290
314
if ( callbackAIterations < 1 ) {
291
- scheduleCallback ( cbA ) ;
315
+ scheduleCallback ( NormalPriority , cbA ) ;
292
316
updateTestResult ( 3 , 'scheduled CbA again' ) ;
293
317
}
294
318
updateTestResult ( 3 , 'cbA' + callbackAIterations + ' called with argument of ' + JSON . stringify ( x ) ) ;
@@ -297,9 +321,9 @@ <h2>Tests:</h2>
297
321
const cbB = ( x ) => {
298
322
updateTestResult ( 3 , 'cbB called with argument of ' + JSON . stringify ( x ) ) ;
299
323
}
300
- scheduleCallback ( cbA ) ;
324
+ scheduleCallback ( NormalPriority , cbA ) ;
301
325
updateTestResult ( 3 , 'scheduled CbA' ) ;
302
- scheduleCallback ( cbB ) ;
326
+ scheduleCallback ( NormalPriority , cbB ) ;
303
327
updateTestResult ( 3 , 'scheduled CbB' ) ;
304
328
logWhenFramesStart ( 3 , ( ) => {
305
329
displayTestResult ( 3 ) ;
@@ -333,13 +357,13 @@ <h2>Tests:</h2>
333
357
const cbD = ( x ) => {
334
358
updateTestResult ( 4 , 'cbD called with argument of ' + JSON . stringify ( x ) ) ;
335
359
}
336
- scheduleCallback ( cbA ) ; // won't time out
360
+ scheduleCallback ( NormalPriority , cbA ) ; // won't time out
337
361
updateTestResult ( 4 , 'scheduled cbA' ) ;
338
- scheduleCallback ( cbB , { timeout : 100 } ) ; // times out later
362
+ scheduleCallback ( NormalPriority , cbB , { timeout : 100 } ) ; // times out later
339
363
updateTestResult ( 4 , 'scheduled cbB' ) ;
340
- scheduleCallback ( cbC , { timeout : 1 } ) ; // will time out fast
364
+ scheduleCallback ( NormalPriority , cbC , { timeout : 1 } ) ; // will time out fast
341
365
updateTestResult ( 4 , 'scheduled cbC' ) ;
342
- scheduleCallback ( cbD ) ; // won't time out
366
+ scheduleCallback ( NormalPriority , cbD ) ; // won't time out
343
367
updateTestResult ( 4 , 'scheduled cbD' ) ;
344
368
345
369
// should have run in order of C, A, B, D
@@ -418,15 +442,15 @@ <h2>Tests:</h2>
418
442
} ) ;
419
443
} ) ;
420
444
} ) ;
421
- scheduleCallback ( cbA ) ;
445
+ scheduleCallback ( NormalPriority , cbA ) ;
422
446
console . log ( 'scheduled cbA' ) ;
423
- scheduleCallback ( cbB ) ; // will throw error
447
+ scheduleCallback ( NormalPriority , cbB ) ; // will throw error
424
448
console . log ( 'scheduled cbB' ) ;
425
- scheduleCallback ( cbC ) ;
449
+ scheduleCallback ( NormalPriority , cbC ) ;
426
450
console . log ( 'scheduled cbC' ) ;
427
- scheduleCallback ( cbD ) ; // will throw error
451
+ scheduleCallback ( NormalPriority , cbD ) ; // will throw error
428
452
console . log ( 'scheduled cbD' ) ;
429
- scheduleCallback ( cbE ) ;
453
+ scheduleCallback ( NormalPriority , cbE ) ;
430
454
console . log ( 'scheduled cbE' ) ;
431
455
} ;
432
456
}
@@ -496,15 +520,15 @@ <h2>Tests:</h2>
496
520
} ) ;
497
521
} ) ;
498
522
} ) ;
499
- scheduleCallback ( cbA ) ;
523
+ scheduleCallback ( NormalPriority , cbA ) ;
500
524
console . log ( 'scheduled cbA' ) ;
501
- scheduleCallback ( cbB ) ; // will throw error
525
+ scheduleCallback ( NormalPriority , cbB ) ; // will throw error
502
526
console . log ( 'scheduled cbB' ) ;
503
- scheduleCallback ( cbC , { timeout : 1 } ) ;
527
+ scheduleCallback ( NormalPriority , cbC , { timeout : 1 } ) ;
504
528
console . log ( 'scheduled cbC' ) ;
505
- scheduleCallback ( cbD , { timeout : 1 } ) ; // will throw error
529
+ scheduleCallback ( NormalPriority , cbD , { timeout : 1 } ) ; // will throw error
506
530
console . log ( 'scheduled cbD' ) ;
507
- scheduleCallback ( cbE , { timeout : 1 } ) ;
531
+ scheduleCallback ( NormalPriority , cbE , { timeout : 1 } ) ;
508
532
console . log ( 'scheduled cbE' ) ;
509
533
} ;
510
534
}
@@ -520,9 +544,9 @@ <h2>Tests:</h2>
520
544
counter ++ ;
521
545
counterNode . innerHTML = counter ;
522
546
waitForTimeToPass ( 100 ) ;
523
- scheduleCallback ( incrementCounterAndScheduleNextCallback ) ;
547
+ scheduleCallback ( NormalPriority , incrementCounterAndScheduleNextCallback ) ;
524
548
}
525
- scheduleCallback ( incrementCounterAndScheduleNextCallback ) ;
549
+ scheduleCallback ( NormalPriority , incrementCounterAndScheduleNextCallback ) ;
526
550
}
527
551
528
552
function runTestEight ( ) {
@@ -542,18 +566,18 @@ <h2>Tests:</h2>
542
566
return count ;
543
567
}
544
568
545
- scheduleCallback ( ( ) => {
569
+ scheduleCallback ( NormalPriority , ( ) => {
546
570
547
571
// size should be 0
548
572
updateTestResult ( 8 , `Queue size: ${ countNodesInStack ( getFirstCallbackNode ( ) ) } .` ) ;
549
573
updateTestResult ( 8 , 'Pausing... press continue to resume.' ) ;
550
574
pauseExecution ( ) ;
551
575
552
- scheduleCallback ( function ( ) {
576
+ scheduleCallback ( NormalPriority , function ( ) {
553
577
updateTestResult ( 8 , 'Finishing...' ) ;
554
578
displayTestResult ( 8 ) ;
555
579
} )
556
- scheduleCallback ( function ( ) {
580
+ scheduleCallback ( NormalPriority , function ( ) {
557
581
updateTestResult ( 8 , 'Done!' ) ;
558
582
displayTestResult ( 8 ) ;
559
583
checkTestResult ( 8 ) ;
@@ -569,6 +593,50 @@ <h2>Tests:</h2>
569
593
continueExecution ( ) ;
570
594
}
571
595
596
+ function runTestNine ( ) {
597
+ clearTestResult ( 9 ) ;
598
+ // We have this to make sure that the thing that goes right after it can get a full frame
599
+ var forceFrameFinish = ( ) => {
600
+ while ( ! shouldYield ( ) ) {
601
+ waitForTimeToPass ( 1 ) ;
602
+ }
603
+ waitForTimeToPass ( 100 ) ;
604
+ }
605
+ scheduleCallback ( NormalPriority , forceFrameFinish ) ;
606
+ scheduleCallback ( NormalPriority , ( ) => {
607
+ var startTime = now ( ) ;
608
+ while ( ! shouldYield ( ) ) { }
609
+ var initialFrameTime = now ( ) - startTime ;
610
+ var newFrameTime = ( initialFrameTime * 2 ) > 60 ? ( initialFrameTime * 2 ) : 60 ;
611
+ var newFrameRate = Math . floor ( 1000 / newFrameTime ) ;
612
+ updateTestResult ( 9 , `Forcing new frame times...` ) ;
613
+ displayTestResult ( 9 ) ;
614
+ forceFrameRate ( newFrameRate ) ;
615
+ var toSchedule = ( again ) => {
616
+ var startTime = now ( ) ;
617
+ while ( ! shouldYield ( ) ) { }
618
+ var frameTime = now ( ) - startTime ;
619
+ if ( frameTime >= ( newFrameTime - 8 ) ) {
620
+ updateTestResult ( 9 , `Using new frame time!` ) ;
621
+ } else {
622
+ updateTestResult ( 9 , `Failed to use new frame time. (off by ${ newFrameTime - frameTime } ms)` ) ;
623
+ }
624
+ displayTestResult ( 9 ) ;
625
+ if ( again ) {
626
+ scheduleCallback ( NormalPriority , forceFrameFinish ) ;
627
+ scheduleCallback ( NormalPriority , ( ) => { toSchedule ( false ) ; } ) ;
628
+ } else {
629
+ updateTestResult ( 9 , `Finished!` ) ;
630
+ forceFrameRate ( 0 ) ;
631
+ displayTestResult ( 9 ) ;
632
+ checkTestResult ( 9 ) ;
633
+ }
634
+ }
635
+ scheduleCallback ( NormalPriority , forceFrameFinish ) ;
636
+ scheduleCallback ( NormalPriority , ( ) => { toSchedule ( true ) ; } ) ;
637
+ } ) ;
638
+ }
639
+
572
640
</ script type="text/babel">
573
641
</ body >
574
- </ html >
642
+ </ html >
0 commit comments