Skip to content

Commit 43c4e5f

Browse files
n8schlossacdlite
authored andcommitted
Add method for forcing a lower framerate
1 parent 1b752f1 commit 43c4e5f

File tree

18 files changed

+158
-141
lines changed

18 files changed

+158
-141
lines changed

fixtures/scheduler/index.html

Lines changed: 103 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ <h2>Tests:</h2>
105105
<div><b>Actual:</b></div>
106106
<div id="test-8"></div>
107107
</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>
108122
</ol>
109123
<script src="../../build/node_modules/react/umd/react.development.js"></script>
110124
<script src="../../build/node_modules/scheduler/umd/scheduler.development.js"></script>
@@ -117,6 +131,9 @@ <h2>Tests:</h2>
117131
unstable_getFirstCallbackNode: getFirstCallbackNode,
118132
unstable_pauseExecution: pauseExecution,
119133
unstable_continueExecution: continueExecution,
134+
unstable_forceFrameRate: forceFrameRate,
135+
unstable_shouldYield: shouldYield,
136+
unstable_NormalPriority: NormalPriority,
120137
} = Scheduler;
121138
function displayTestResult(testNumber) {
122139
const expectationNode = document.getElementById('test-' + testNumber + '-expected');
@@ -188,7 +205,7 @@ <h2>Tests:</h2>
188205
[
189206
'scheduled Cb1',
190207
'frame 1 started',
191-
'cb1 called with argument of {"didTimeout":false}',
208+
'cb1 called with argument of false',
192209
'frame 2 started',
193210
'frame 3 started... we stop counting now.',
194211
],
@@ -197,8 +214,8 @@ <h2>Tests:</h2>
197214
'scheduled CbA',
198215
'scheduled CbB',
199216
'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',
202219
'frame 2 started',
203220
'frame 3 started... we stop counting now.',
204221
],
@@ -208,9 +225,9 @@ <h2>Tests:</h2>
208225
'scheduled CbB',
209226
'frame 1 started',
210227
'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',
214231
'frame 2 started',
215232
'frame 3 started... we stop counting now.',
216233
],
@@ -222,11 +239,11 @@ <h2>Tests:</h2>
222239
'scheduled cbD',
223240
'frame 1 started',
224241
'cbC called with argument of {"didTimeout":true}',
225-
'cbA called with argument of {"didTimeout":false}',
242+
'cbA called with argument of false',
226243
'cbA running and taking some time',
227244
'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',
230247
'frame 3 started... we stop counting now.',
231248
],
232249
// test 5
@@ -243,6 +260,13 @@ <h2>Tests:</h2>
243260
'Finishing...',
244261
'Done!',
245262
],
263+
// test 9
264+
[
265+
'Forcing new frame times...',
266+
'Using new frame time!',
267+
'Using new frame time!',
268+
'Finished!',
269+
],
246270
];
247271
function runTestOne() {
248272
// Test 1
@@ -253,7 +277,7 @@ <h2>Tests:</h2>
253277
const cb1 = (x) => {
254278
updateTestResult(1, 'cb1 called with argument of ' + JSON.stringify(x));
255279
}
256-
scheduleCallback(cb1);
280+
scheduleCallback(NormalPriority, cb1);
257281
updateTestResult(1, 'scheduled Cb1');
258282
logWhenFramesStart(1, () => {
259283
displayTestResult(1);
@@ -271,9 +295,9 @@ <h2>Tests:</h2>
271295
const cbB = (x) => {
272296
updateTestResult(2, 'cbB called with argument of ' + JSON.stringify(x));
273297
}
274-
scheduleCallback(cbA);
298+
scheduleCallback(NormalPriority, cbA);
275299
updateTestResult(2, 'scheduled CbA');
276-
scheduleCallback(cbB);
300+
scheduleCallback(NormalPriority, cbB);
277301
updateTestResult(2, 'scheduled CbB');
278302
logWhenFramesStart(2, () => {
279303
displayTestResult(2);
@@ -288,7 +312,7 @@ <h2>Tests:</h2>
288312
let callbackAIterations = 0;
289313
const cbA = (x) => {
290314
if (callbackAIterations < 1) {
291-
scheduleCallback(cbA);
315+
scheduleCallback(NormalPriority, cbA);
292316
updateTestResult(3, 'scheduled CbA again');
293317
}
294318
updateTestResult(3, 'cbA' + callbackAIterations + ' called with argument of ' + JSON.stringify(x));
@@ -297,9 +321,9 @@ <h2>Tests:</h2>
297321
const cbB = (x) => {
298322
updateTestResult(3, 'cbB called with argument of ' + JSON.stringify(x));
299323
}
300-
scheduleCallback(cbA);
324+
scheduleCallback(NormalPriority, cbA);
301325
updateTestResult(3, 'scheduled CbA');
302-
scheduleCallback(cbB);
326+
scheduleCallback(NormalPriority, cbB);
303327
updateTestResult(3, 'scheduled CbB');
304328
logWhenFramesStart(3, () => {
305329
displayTestResult(3);
@@ -333,13 +357,13 @@ <h2>Tests:</h2>
333357
const cbD = (x) => {
334358
updateTestResult(4, 'cbD called with argument of ' + JSON.stringify(x));
335359
}
336-
scheduleCallback(cbA); // won't time out
360+
scheduleCallback(NormalPriority, cbA); // won't time out
337361
updateTestResult(4, 'scheduled cbA');
338-
scheduleCallback(cbB, {timeout: 100}); // times out later
362+
scheduleCallback(NormalPriority, cbB, {timeout: 100}); // times out later
339363
updateTestResult(4, 'scheduled cbB');
340-
scheduleCallback(cbC, {timeout: 1}); // will time out fast
364+
scheduleCallback(NormalPriority, cbC, {timeout: 1}); // will time out fast
341365
updateTestResult(4, 'scheduled cbC');
342-
scheduleCallback(cbD); // won't time out
366+
scheduleCallback(NormalPriority, cbD); // won't time out
343367
updateTestResult(4, 'scheduled cbD');
344368

345369
// should have run in order of C, A, B, D
@@ -418,15 +442,15 @@ <h2>Tests:</h2>
418442
});
419443
});
420444
});
421-
scheduleCallback(cbA);
445+
scheduleCallback(NormalPriority, cbA);
422446
console.log('scheduled cbA');
423-
scheduleCallback(cbB); // will throw error
447+
scheduleCallback(NormalPriority, cbB); // will throw error
424448
console.log('scheduled cbB');
425-
scheduleCallback(cbC);
449+
scheduleCallback(NormalPriority, cbC);
426450
console.log('scheduled cbC');
427-
scheduleCallback(cbD); // will throw error
451+
scheduleCallback(NormalPriority, cbD); // will throw error
428452
console.log('scheduled cbD');
429-
scheduleCallback(cbE);
453+
scheduleCallback(NormalPriority, cbE);
430454
console.log('scheduled cbE');
431455
};
432456
}
@@ -496,15 +520,15 @@ <h2>Tests:</h2>
496520
});
497521
});
498522
});
499-
scheduleCallback(cbA);
523+
scheduleCallback(NormalPriority, cbA);
500524
console.log('scheduled cbA');
501-
scheduleCallback(cbB); // will throw error
525+
scheduleCallback(NormalPriority, cbB); // will throw error
502526
console.log('scheduled cbB');
503-
scheduleCallback(cbC, {timeout: 1});
527+
scheduleCallback(NormalPriority, cbC, {timeout: 1});
504528
console.log('scheduled cbC');
505-
scheduleCallback(cbD, {timeout: 1}); // will throw error
529+
scheduleCallback(NormalPriority, cbD, {timeout: 1}); // will throw error
506530
console.log('scheduled cbD');
507-
scheduleCallback(cbE, {timeout: 1});
531+
scheduleCallback(NormalPriority, cbE, {timeout: 1});
508532
console.log('scheduled cbE');
509533
};
510534
}
@@ -520,9 +544,9 @@ <h2>Tests:</h2>
520544
counter++;
521545
counterNode.innerHTML = counter;
522546
waitForTimeToPass(100);
523-
scheduleCallback(incrementCounterAndScheduleNextCallback);
547+
scheduleCallback(NormalPriority, incrementCounterAndScheduleNextCallback);
524548
}
525-
scheduleCallback(incrementCounterAndScheduleNextCallback);
549+
scheduleCallback(NormalPriority, incrementCounterAndScheduleNextCallback);
526550
}
527551

528552
function runTestEight() {
@@ -542,18 +566,18 @@ <h2>Tests:</h2>
542566
return count;
543567
}
544568

545-
scheduleCallback(() => {
569+
scheduleCallback(NormalPriority, () => {
546570

547571
// size should be 0
548572
updateTestResult(8, `Queue size: ${countNodesInStack(getFirstCallbackNode())}.`);
549573
updateTestResult(8, 'Pausing... press continue to resume.');
550574
pauseExecution();
551575

552-
scheduleCallback(function () {
576+
scheduleCallback(NormalPriority, function () {
553577
updateTestResult(8, 'Finishing...');
554578
displayTestResult(8);
555579
})
556-
scheduleCallback(function () {
580+
scheduleCallback(NormalPriority, function () {
557581
updateTestResult(8, 'Done!');
558582
displayTestResult(8);
559583
checkTestResult(8);
@@ -569,6 +593,50 @@ <h2>Tests:</h2>
569593
continueExecution();
570594
}
571595

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+
572640
</script type="text/babel">
573641
</body>
574-
</html>
642+
</html>

packages/react-events/drag.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/react-events/focus.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/react-events/hover.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/react-events/npm/drag.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/react-events/npm/focus.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/react-events/npm/hover.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/react-events/npm/press.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/react-events/npm/swipe.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/react-events/press.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/react-events/swipe.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)