• A React hook that periodically invokes a given function to keep it "warmed up." This can be useful for functions that require initialization or caching to improve performance during actual usage.

    Type Parameters

    • T extends ((arg?) => any)

      The type of the function to be warmed up.

    Parameters

    • fn: Warmupable<T>

      The function to be warmed up. It should accept an optional argument and handle a special warmup call with { __warmup: true }.

    • options: UseFunctionWarmupOptions = {}

      Configuration options for the warmup behavior.

    Returns void

    void

    Remarks

    • If the immediate option is enabled, the function will be warmed up immediately using requestIdleCallback if available, or a setTimeout fallback.
    • Errors thrown during the warmup process are caught and ignored.
    • The periodic warmup is cleaned up automatically when the component unmounts.

    Example

    import { useFunctionWarmup } from './use-function-warmup';

    const myFunction = (arg?: any) => {
    if (arg?.__warmup) {
    console.log('Warming up...');
    } else {
    console.log('Executing function...');
    }
    };

    function MyComponent() {
    useFunctionWarmup(myFunction, { intervalMs: 60000, immediate: true });

    return <div>My Component</div>;
    }