97
Scheduler = require('scheduler');
98
});
99
100
- describe('scheduleWork', () => {
100
+ describe('scheduleCallback', () => {
101
it('calls the callback within the frame when not blocked', () => {
102
- const {unstable_scheduleWork: scheduleWork} = Scheduler;
102
+ const {unstable_scheduleCallback: scheduleCallback} = Scheduler;
103
const cb = jest.fn();
104
- scheduleWork(cb);
104
+ scheduleCallback(cb);
105
advanceOneFrame({timeLeftInFrame: 15});
106
expect(cb).toHaveBeenCalledTimes(1);
107
// should not have timed out and should include a timeRemaining method
111
112
describe('with multiple callbacks', () => {
113
it('accepts multiple callbacks and calls within frame when not blocked', () => {
114
- const {unstable_scheduleWork: scheduleWork} = Scheduler;
114
+ const {unstable_scheduleCallback: scheduleCallback} = Scheduler;
115
const callbackLog = [];
116
const callbackA = jest.fn(() => callbackLog.push('A'));
117
const callbackB = jest.fn(() => callbackLog.push('B'));
118
- scheduleWork(callbackA);
118
+ scheduleCallback(callbackA);
119
// initially waits to call the callback
120
expect(callbackLog).toEqual([]);
121
// waits while second callback is passed
122
- scheduleWork(callbackB);
122
+ scheduleCallback(callbackB);
123
expect(callbackLog).toEqual([]);
124
// after a delay, calls as many callbacks as it has time for
125
advanceOneFrame({timeLeftInFrame: 15});
137
});
138
139
it("accepts callbacks betweeen animationFrame and postMessage and doesn't stall", () => {
140
- const {unstable_scheduleWork: scheduleWork} = Scheduler;
140
+ const {unstable_scheduleCallback: scheduleCallback} = Scheduler;
141
const callbackLog = [];
142
const callbackA = jest.fn(() => callbackLog.push('A'));
143
const callbackB = jest.fn(() => callbackLog.push('B'));
144
const callbackC = jest.fn(() => callbackLog.push('C'));
145
- scheduleWork(callbackA);
145
+ scheduleCallback(callbackA);
146
// initially waits to call the callback
147
expect(callbackLog).toEqual([]);
148
runRAFCallbacks();
149
// this should schedule work *after* the requestAnimationFrame but before the message handler
150
- scheduleWork(callbackB);
150
+ scheduleCallback(callbackB);
151
expect(callbackLog).toEqual([]);
152
// now it should drain the message queue and do all scheduled work
153
runPostMessageCallbacks({timeLeftInFrame: 15});
157
advanceOneFrame({timeLeftInFrame: 15});
158
159
// see if more work can be done now.
160
- scheduleWork(callbackC);
160
+ scheduleCallback(callbackC);
161
expect(callbackLog).toEqual(['A', 'B']);
162
advanceOneFrame({timeLeftInFrame: 15});
163
expect(callbackLog).toEqual(['A', 'B', 'C']);
167
'schedules callbacks in correct order and' +
168
'keeps calling them if there is time',
169
() => {
170
- const {unstable_scheduleWork: scheduleWork} = Scheduler;
170
+ const {unstable_scheduleCallback: scheduleCallback} = Scheduler;
171
const callbackLog = [];
172
const callbackA = jest.fn(() => {
173
callbackLog.push('A');
174
- scheduleWork(callbackC);
174
+ scheduleCallback(callbackC);
175
});
176
const callbackB = jest.fn(() => {
177
callbackLog.push('B');
180
callbackLog.push('C');
181
});
182
183
- scheduleWork(callbackA);
183
+ scheduleCallback(callbackA);
184
// initially waits to call the callback
185
expect(callbackLog).toEqual([]);
186
// continues waiting while B is scheduled
187
- scheduleWork(callbackB);
187
+ scheduleCallback(callbackB);
188
expect(callbackLog).toEqual([]);
189
// after a delay, calls the scheduled callbacks,
190
// and also calls new callbacks scheduled by current callbacks
193
},
194
);
195
196
- it('schedules callbacks in correct order when callbacks have many nested scheduleWork calls', () => {
197
- const {unstable_scheduleWork: scheduleWork} = Scheduler;
196
+ it('schedules callbacks in correct order when callbacks have many nested scheduleCallback calls', () => {
197
+ const {unstable_scheduleCallback: scheduleCallback} = Scheduler;
198
const callbackLog = [];
199
const callbackA = jest.fn(() => {
200
callbackLog.push('A');
201
- scheduleWork(callbackC);
202
- scheduleWork(callbackD);
201
+ scheduleCallback(callbackC);
202
+ scheduleCallback(callbackD);
203
});
204
const callbackB = jest.fn(() => {
205
callbackLog.push('B');
206
- scheduleWork(callbackE);
207
- scheduleWork(callbackF);
206
+ scheduleCallback(callbackE);
207
+ scheduleCallback(callbackF);
208
});
209
const callbackC = jest.fn(() => {
210
callbackLog.push('C');
219
callbackLog.push('F');
220
});
221
222
- scheduleWork(callbackA);
223
- scheduleWork(callbackB);
222
+ scheduleCallback(callbackA);
223
+ scheduleCallback(callbackB);
224
// initially waits to call the callback
225
expect(callbackLog).toEqual([]);
226
// while flushing callbacks, calls as many as it has time for
228
expect(callbackLog).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
229
});
230
231
- it('schedules callbacks in correct order when they use scheduleWork to schedule themselves', () => {
232
- const {unstable_scheduleWork: scheduleWork} = Scheduler;
231
+ it('schedules callbacks in correct order when they use scheduleCallback to schedule themselves', () => {
232
+ const {unstable_scheduleCallback: scheduleCallback} = Scheduler;
233
const callbackLog = [];
234
let callbackAIterations = 0;
235
const callbackA = jest.fn(() => {
236
if (callbackAIterations < 1) {
237
- scheduleWork(callbackA);
237
+ scheduleCallback(callbackA);
238
}
239
callbackLog.push('A' + callbackAIterations);
240
callbackAIterations++;
241
});
242
const callbackB = jest.fn(() => callbackLog.push('B'));
243
244
- scheduleWork(callbackA);
244
+ scheduleCallback(callbackA);
245
// initially waits to call the callback
246
expect(callbackLog).toEqual([]);
247
- scheduleWork(callbackB);
247
+ scheduleCallback(callbackB);
248
expect(callbackLog).toEqual([]);
249
// after a delay, calls the latest callback passed
250
advanceOneFrame({timeLeftInFrame: 15});
260
261
describe('when there is no more time left in the frame', () => {
262
it('calls any callback which has timed out, waits for others', () => {
263
- const {unstable_scheduleWork: scheduleWork} = Scheduler;
263
+ const {unstable_scheduleCallback: scheduleCallback} = Scheduler;
264
startOfLatestFrame = 1000000000000;
265
currentTime = startOfLatestFrame - 10;
266
const callbackLog = [];
269
const callbackB = jest.fn(() => callbackLog.push('B'));
270
const callbackC = jest.fn(() => callbackLog.push('C'));
271
272
- scheduleWork(callbackA); // won't time out
273
- scheduleWork(callbackB, {timeout: 100}); // times out later
274
- scheduleWork(callbackC, {timeout: 2}); // will time out fast
272
+ scheduleCallback(callbackA); // won't time out
273
+ scheduleCallback(callbackB, {timeout: 100}); // times out later
274
+ scheduleCallback(callbackC, {timeout: 2}); // will time out fast
275
276
// push time ahead a bit so that we have no idle time
277
advanceOneFrame({timePastFrameDeadline: 16});
295
296
describe('when there is some time left in the frame', () => {
297
it('calls timed out callbacks and then any more pending callbacks, defers others if time runs out', () => {
298
- const {unstable_scheduleWork: scheduleWork} = Scheduler;
298
+ const {unstable_scheduleCallback: scheduleCallback} = Scheduler;
299
startOfLatestFrame = 1000000000000;
300
currentTime = startOfLatestFrame - 10;
301
const callbackLog = [];
309
const callbackC = jest.fn(() => callbackLog.push('C'));
310
const callbackD = jest.fn(() => callbackLog.push('D'));
311
312
- scheduleWork(callbackA, {timeout: 100}); // won't time out
313
- scheduleWork(callbackB, {timeout: 100}); // times out later
314
- scheduleWork(callbackC, {timeout: 2}); // will time out fast
315
- scheduleWork(callbackD, {timeout: 200}); // won't time out
312
+ scheduleCallback(callbackA, {timeout: 100}); // won't time out
313
+ scheduleCallback(callbackB, {timeout: 100}); // times out later
314
+ scheduleCallback(callbackC, {timeout: 2}); // will time out fast
315
+ scheduleCallback(callbackD, {timeout: 200}); // won't time out
316
317
advanceOneFrame({timeLeftInFrame: 15}); // runs rAF and postMessage callbacks
318
341
});
342
});
343
344
- describe('cancelScheduledWork', () => {
344
+ describe('cancelCallback', () => {
345
it('cancels the scheduled callback', () => {
346
const {
347
- unstable_scheduleWork: scheduleWork,
348
- unstable_cancelScheduledWork: cancelScheduledWork,
347
+ unstable_scheduleCallback: scheduleCallback,
348
+ unstable_cancelCallback: cancelCallback,
349
} = Scheduler;
350
const cb = jest.fn();
351
- const callbackId = scheduleWork(cb);
351
+ const callbackId = scheduleCallback(cb);
352
expect(cb).toHaveBeenCalledTimes(0);
353
- cancelScheduledWork(callbackId);
353
+ cancelCallback(callbackId);
354
advanceOneFrame({timeLeftInFrame: 15});
355
expect(cb).toHaveBeenCalledTimes(0);
356
});
358
describe('with multiple callbacks', () => {
359
it('when called more than once', () => {
360
const {
361
- unstable_scheduleWork: scheduleWork,
362
- unstable_cancelScheduledWork: cancelScheduledWork,
361
+ unstable_scheduleCallback: scheduleCallback,
362
+ unstable_cancelCallback: cancelCallback,
363
} = Scheduler;
364
const callbackLog = [];
365
const callbackA = jest.fn(() => callbackLog.push('A'));
366
const callbackB = jest.fn(() => callbackLog.push('B'));
367
const callbackC = jest.fn(() => callbackLog.push('C'));
368
- scheduleWork(callbackA);
369
- const callbackId = scheduleWork(callbackB);
370
- scheduleWork(callbackC);
371
- cancelScheduledWork(callbackId);
372
- cancelScheduledWork(callbackId);
373
- cancelScheduledWork(callbackId);
368
+ scheduleCallback(callbackA);
369
+ const callbackId = scheduleCallback(callbackB);
370
+ scheduleCallback(callbackC);
371
+ cancelCallback(callbackId);
372
+ cancelCallback(callbackId);
373
+ cancelCallback(callbackId);
374
// Initially doesn't call anything
375
expect(callbackLog).toEqual([]);
376
advanceOneFrame({timeLeftInFrame: 15});
382
383
it('when one callback cancels the next one', () => {
384
const {
385
- unstable_scheduleWork: scheduleWork,
386
- unstable_cancelScheduledWork: cancelScheduledWork,
385
+ unstable_scheduleCallback: scheduleCallback,
386
+ unstable_cancelCallback: cancelCallback,
387
} = Scheduler;
388
const callbackLog = [];
389
let callbackBId;
390
const callbackA = jest.fn(() => {
391
callbackLog.push('A');
392
- cancelScheduledWork(callbackBId);
392
+ cancelCallback(callbackBId);
393
});
394
const callbackB = jest.fn(() => callbackLog.push('B'));
395
- scheduleWork(callbackA);
396
- callbackBId = scheduleWork(callbackB);
395
+ scheduleCallback(callbackA);
396
+ callbackBId = scheduleCallback(callbackB);
397
// Initially doesn't call anything
398
expect(callbackLog).toEqual([]);
399
advanceOneFrame({timeLeftInFrame: 15});
421
*
422
*/
423
it('still calls all callbacks within same frame', () => {
424
- const {unstable_scheduleWork: scheduleWork} = Scheduler;
424
+ const {unstable_scheduleCallback: scheduleCallback} = Scheduler;
425
const callbackLog = [];
426
const callbackA = jest.fn(() => callbackLog.push('A'));
427
const callbackB = jest.fn(() => {
434
throw new Error('D error');
435
});
436
const callbackE = jest.fn(() => callbackLog.push('E'));
437
- scheduleWork(callbackA);
438
- scheduleWork(callbackB);
439
- scheduleWork(callbackC);
440
- scheduleWork(callbackD);
441
- scheduleWork(callbackE);
437
+ scheduleCallback(callbackA);
438
+ scheduleCallback(callbackB);
439
+ scheduleCallback(callbackC);
440
+ scheduleCallback(callbackD);
441
+ scheduleCallback(callbackE);
442
// Initially doesn't call anything
443
expect(callbackLog).toEqual([]);
444
catchPostMessageErrors = true;
467
*
468
*/
469
it('and with some timed out callbacks, still calls all callbacks within same frame', () => {
470
- const {unstable_scheduleWork: scheduleWork} = Scheduler;
470
+ const {unstable_scheduleCallback: scheduleCallback} = Scheduler;
471
const callbackLog = [];
472
const callbackA = jest.fn(() => {
473
callbackLog.push('A');
480
throw new Error('D error');
481
});
482
const callbackE = jest.fn(() => callbackLog.push('E'));
483
- scheduleWork(callbackA);
484
- scheduleWork(callbackB);
485
- scheduleWork(callbackC, {timeout: 2}); // times out fast
486
- scheduleWork(callbackD, {timeout: 2}); // times out fast
487
- scheduleWork(callbackE, {timeout: 2}); // times out fast
483
+ scheduleCallback(callbackA);
484
+ scheduleCallback(callbackB);
485
+ scheduleCallback(callbackC, {timeout: 2}); // times out fast
486
+ scheduleCallback(callbackD, {timeout: 2}); // times out fast
487
+ scheduleCallback(callbackE, {timeout: 2}); // times out fast
488
// Initially doesn't call anything
489
expect(callbackLog).toEqual([]);
490
catchPostMessageErrors = true;
513
*
514
*/
515
it('still calls all callbacks within same frame', () => {
516
- const {unstable_scheduleWork: scheduleWork} = Scheduler;
516
+ const {unstable_scheduleCallback: scheduleCallback} = Scheduler;
517
const callbackLog = [];
518
const callbackA = jest.fn(() => {
519
callbackLog.push('A');
535
callbackLog.push('E');
536
throw new Error('E error');
537
});
538
- scheduleWork(callbackA);
539
- scheduleWork(callbackB);
540
- scheduleWork(callbackC);
541
- scheduleWork(callbackD);
542
- scheduleWork(callbackE);
538
+ scheduleCallback(callbackA);
539
+ scheduleCallback(callbackB);
540
+ scheduleCallback(callbackC);
541
+ scheduleCallback(callbackD);
542
+ scheduleCallback(callbackE);
543
// Initially doesn't call anything
544
expect(callbackLog).toEqual([]);
545
catchPostMessageErrors = true;
574
*
575
*/
576
it('and with all timed out callbacks, still calls all callbacks within same frame', () => {
577
- const {unstable_scheduleWork: scheduleWork} = Scheduler;
577
+ const {unstable_scheduleCallback: scheduleCallback} = Scheduler;
578
const callbackLog = [];
579
const callbackA = jest.fn(() => {
580
callbackLog.push('A');
596
callbackLog.push('E');
597
throw new Error('E error');
598
});
599
- scheduleWork(callbackA, {timeout: 2}); // times out fast
600
- scheduleWork(callbackB, {timeout: 2}); // times out fast
601
- scheduleWork(callbackC, {timeout: 2}); // times out fast
602
- scheduleWork(callbackD, {timeout: 2}); // times out fast
603
- scheduleWork(callbackE, {timeout: 2}); // times out fast
599
+ scheduleCallback(callbackA, {timeout: 2}); // times out fast
600
+ scheduleCallback(callbackB, {timeout: 2}); // times out fast
601
+ scheduleCallback(callbackC, {timeout: 2}); // times out fast
602
+ scheduleCallback(callbackD, {timeout: 2}); // times out fast
603
+ scheduleCallback(callbackE, {timeout: 2}); // times out fast
604
// Initially doesn't call anything
605
expect(callbackLog).toEqual([]);
606
catchPostMessageErrors = true;
655
*
656
*/
657
it('still calls all callbacks within same frame', () => {
658
- const {unstable_scheduleWork: scheduleWork} = Scheduler;
658
+ const {unstable_scheduleCallback: scheduleCallback} = Scheduler;
659
startOfLatestFrame = 1000000000000;
660
currentTime = startOfLatestFrame - 10;
661
catchPostMessageErrors = true;
689
});
690
const callbackG = jest.fn(() => callbackLog.push('G'));
691
692
- scheduleWork(callbackA);
693
- scheduleWork(callbackB);
694
- scheduleWork(callbackC);
695
- scheduleWork(callbackD);
696
- scheduleWork(callbackE);
697
- scheduleWork(callbackF);
698
- scheduleWork(callbackG);
692
+ scheduleCallback(callbackA);
693
+ scheduleCallback(callbackB);
694
+ scheduleCallback(callbackC);
695
+ scheduleCallback(callbackD);
696
+ scheduleCallback(callbackE);
697
+ scheduleCallback(callbackF);
698
+ scheduleCallback(callbackG);
699
700
// does nothing initially
701
expect(callbackLog).toEqual([]);