(callback: Function)
| 35 | * @returns a function to cancel the scheduled callback |
| 36 | */ |
| 37 | export 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 | |
| 68 | export function scheduleCallbackWithMicrotask(callback: Function): () => void { |
| 69 | queueMicrotask(() => callback()); |
no test coverage detected
searching dependent graphs…