• Wrap a function in a Job that restarts each time the resulting function is called, thereby canceling any nested jobs and cleaning up any resources used by previous calls. (This can be useful for such things as canceling an in-progress search when the user types more text in a field.)

    The restarting job will be ended when the job that invoked restarting() is finished, canceled, or restarted. Calling the wrapped function after its job has ended will result in an error. You can wrap any function any number of times: each call to restarting() creates a new, distinct "restarting job" and function wrapper to go with it.

    Type Parameters

    Parameters

    • task: F

      (Optional) The function to be wrapped. This can be any function: the returned wrapper function will match its call signature exactly, including overloads. (So for example you could wrap the start API via restarting(start), to create a function you can pass job-start functions to. When called, the function would cancel any outstanding job from a previous call, and start the new one in its place.)

    Returns F

    A function of identical type to the input function. If no input function was given, the returned function will just take one argument (a zero-argument function optionally returning a CleanupFn).

  • Returns ((task: (() => OptionalCleanup)) => void)