MCPcopy
hub / github.com/angular/angular / scheduleCallbackWithRafRace

Function scheduleCallbackWithRafRace

packages/core/src/util/callback_scheduler.ts:37–66  ·  view source on GitHub ↗
(callback: Function)

Source from the content-addressed store, hash-verified

35 * @returns a function to cancel the scheduled callback
36 */
37export function scheduleCallbackWithRafRace(callback: Function): () => void {
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.
52 }
53 }
54 timeoutId = setTimeout(() => {
55 callback();
56 cleanup();
57 }) as unknown as number;
58 if (typeof requestAnimationFrame === 'function') {
59 animationFrameId = requestAnimationFrame(() => {
60 callback();
61 cleanup();
62 });
63 }
64
65 return () => cleanup();
66}
67
68export function scheduleCallbackWithMicrotask(callback: Function): () => void {
69 queueMicrotask(() => callback());

Callers 2

scheduleCheckStableFunction · 0.90

Calls 3

setTimeoutFunction · 0.85
cleanupFunction · 0.70
callbackFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…