Skip to content

Commit b558f99

Browse files
atscottthePunderWoman
authored andcommitted
refactor(core): Update callback schedulers to cancel pending timers (angular#57186)
Rather than leaving the timers around as no-ops, this commit updates the logic to also attempt to clear or cancel the timers. This is helpful for the eventual goal of running the scheduler in the `fakeAsync` zone (if the test is running in `fakeAsync`) rather than scheduling in the root zone and making it impossible to flush. PR Close angular#57186
1 parent b3836c2 commit b558f99

File tree

6 files changed

+25
-38
lines changed

6 files changed

+25
-38
lines changed

packages/core/src/util/callback_scheduler.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {global} from './global';
9+
import {noop} from './noop';
1010

1111
/**
1212
* Gets a scheduling function that runs the callback after the first of setTimeout and
@@ -35,38 +35,40 @@ import {global} from './global';
3535
* @returns a function to cancel the scheduled callback
3636
*/
3737
export function scheduleCallbackWithRafRace(callback: Function): () => void {
38-
let executeCallback = true;
39-
setTimeout(() => {
40-
if (!executeCallback) {
41-
return;
38+
let timeoutId: number;
39+
let animationFrameId: number;
40+
function cleanup() {
41+
callback = noop;
42+
try {
43+
if (animationFrameId !== undefined && typeof cancelAnimationFrame === 'function') {
44+
cancelAnimationFrame(animationFrameId);
45+
}
46+
if (timeoutId !== undefined) {
47+
clearTimeout(timeoutId);
48+
}
49+
} catch {
50+
// Clearing/canceling can fail in tests due to the timing of functions being patched and unpatched
51+
// Just ignore the errors - we protect ourselves from this issue by also making the callback a no-op.
4252
}
43-
executeCallback = false;
53+
}
54+
timeoutId = setTimeout(() => {
4455
callback();
45-
});
46-
if (typeof global['requestAnimationFrame'] === 'function') {
47-
global['requestAnimationFrame'](() => {
48-
if (!executeCallback) {
49-
return;
50-
}
51-
executeCallback = false;
56+
cleanup();
57+
}) as unknown as number;
58+
if (typeof requestAnimationFrame === 'function') {
59+
animationFrameId = requestAnimationFrame(() => {
5260
callback();
61+
cleanup();
5362
});
5463
}
5564

56-
return () => {
57-
executeCallback = false;
58-
};
65+
return () => cleanup();
5966
}
6067

6168
export function scheduleCallbackWithMicrotask(callback: Function): () => void {
62-
let executeCallback = true;
63-
queueMicrotask(() => {
64-
if (executeCallback) {
65-
callback();
66-
}
67-
});
69+
queueMicrotask(() => callback());
6870

6971
return () => {
70-
executeCallback = false;
72+
callback = noop;
7173
};
7274
}

packages/core/test/bundling/animations-standalone/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,9 +608,6 @@
608608
{
609609
"name": "_getInsertInFrontOfRNodeWithI18n"
610610
},
611-
{
612-
"name": "_global"
613-
},
614611
{
615612
"name": "_icuContainerIterate"
616613
},

packages/core/test/bundling/defer/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,6 @@
548548
{
549549
"name": "_getInsertInFrontOfRNodeWithI18n"
550550
},
551-
{
552-
"name": "_global"
553-
},
554551
{
555552
"name": "_icuContainerIterate"
556553
},

packages/core/test/bundling/hello_world/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,6 @@
371371
{
372372
"name": "_currentInjector"
373373
},
374-
{
375-
"name": "_global"
376-
},
377374
{
378375
"name": "_icuContainerIterate"
379376
},

packages/core/test/bundling/hydration/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,6 @@
548548
{
549549
"name": "_getInsertInFrontOfRNodeWithI18n"
550550
},
551-
{
552-
"name": "_global"
553-
},
554551
{
555552
"name": "_icuContainerIterate"
556553
},

packages/core/test/bundling/standalone_bootstrap/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,6 @@
443443
{
444444
"name": "_getInsertInFrontOfRNodeWithI18n"
445445
},
446-
{
447-
"name": "_global"
448-
},
449446
{
450447
"name": "_icuContainerIterate"
451448
},

0 commit comments

Comments
 (0)