/* * @nevware21/ts-utils * https://github.com/nevware21/ts-utils * * Copyright (c) 2022 NevWare21 Solutions LLC * Licensed under the MIT license. */ import { fnApply } from "../funcs/funcs"; import { isArray, isFunction } from "../helpers/base"; import { ClearTimeoutOverrideFn, TimeoutOverrideFn, TimeoutOverrideFuncs } from "../helpers/types"; import { ArrSlice, CALL, LENGTH, UNDEF_VALUE } from "../internal/constants"; import { _getGlobalConfig } from "../internal/global"; import { ITimerHandler, _createTimerHandler } from "./handler"; export { ClearTimeoutOverrideFn, TimeoutOverrideFn, TimeoutOverrideFuncs }; // Package instance timeout override functions let _setTimeoutFn: TimeoutOverrideFn | undefined; let _clearTimeoutFn: ClearTimeoutOverrideFn | undefined; let _microtaskCallback: (() => void) | undefined; /** * Returns the [setTimeout, clearTimeout] overrides of an override, which is either the setTimeout function alone or the * pair, with defValue for any that isn't given */ function _getOverrides(overrideFn: TimeoutOverrideFn | TimeoutOverrideFuncs | undefined, defValue: null | undefined): [any, any] { let isArr = isArray(overrideFn); let len = isArr ? (overrideFn as TimeoutOverrideFuncs)[LENGTH] : 0; return [ len > 0 ? (overrideFn as TimeoutOverrideFuncs)[0] : (!isArr ? overrideFn : defValue), len > 1 ? (overrideFn as TimeoutOverrideFuncs)[1] : defValue ]; } /** * Returns the override function to use: the given one, else the package instance override, else the global override * (at idx of the global [setTimeout, clearTimeout] pair). The caller falls back to the native function, which is only * read when there is no override, as a runtime may not define it (and only supply the overrides). */ function _resolveFn(theFn: F, packageFn: F | undefined, idx: number): F | undefined { let result = isFunction(theFn) ? theFn : packageFn; if (!result) { // Get global timeout overrides if available let globalOverrides = _getGlobalConfig().tmOut; if (isArray(globalOverrides) && isFunction(globalOverrides[idx])) { result = globalOverrides[idx] as any; } } return result; } /** * @internal * @ignore * Creates the timer handler, the callers MUST pass a copy of their arguments (not the arguments object itself) * as the callback (`theArgs[0]`) is replaced with a wrapper and the arguments are kept and reused by `refresh()` * after the callers have returned. */ function _createTimeoutWith(startTimer: boolean, overrideFn: TimeoutOverrideFn | TimeoutOverrideFuncs, theArgs: any[]): ITimerHandler { let overrides = _getOverrides(overrideFn, UNDEF_VALUE); // Use package instance override functions if provided and no specific override was given // If no package overrides, try global overrides before falling back to native functions let setFn = _resolveFn(overrides[0], _setTimeoutFn, 0) || setTimeout; let clearFn = _resolveFn(overrides[1], _clearTimeoutFn, 1) || clearTimeout; let timerFn = theArgs[0]; let starts = 0; let starting = 0; theArgs[0] = function () { let started = starts; let cancelled: boolean | undefined; // Microtask fallback hook for environments without native queueMicrotask support, this allows us to // simulate the running of microtasks before standard timeouts. try { if (_microtaskCallback) { // While a setTimeout override runs the callback synchronously the handler still has the previous // (cleared) timer, which isn't the timer being run let enabled = !starting && handler.h.enabled; // Run any pending microtasks before running the timeout callback to allow any microtasks // scheduled within the callback to run before the next timeout. _runMicrotasks(); // Like a native microtask (which runs before the timer), a microtask which leaves the timer // cancelled stops the callback cancelled = enabled && !handler.h.enabled; } } finally { // The timer has run, a timer started by a microtask (run above) is kept handler.dn(started !== starts); } // Pass this callbacks arguments directly (apply accepts any array-like), this function has no named // parameters and the arguments are not modified, so there is no need to slice them into a new array !cancelled && fnApply(timerFn, UNDEF_VALUE, arguments); }; // The timer is started after the handler is assigned (rather than by _createTimerHandler()), as a setTimeout // override may call the callback synchronously, which uses the handler let handler = _createTimerHandler((timerId?: any) => { if (timerId && timerId.refresh) { timerId.refresh(); } else { timerId && fnApply(clearFn, UNDEF_VALUE, [ timerId ]); // When the callback is run synchronously (handler.dn()) the handler ignores the returned id, so it isn't enabled. // The starts in progress are counted (rather than a flag), as the override may first run another callback which // starts this timer again (a nested start), and the count is restored when the setTimeout throws starting++; try { timerId = fnApply(setFn, UNDEF_VALUE, theArgs); } finally { starting--; } } // Counted once the timer has been started (so not when the setTimeout throws) starts++; return timerId; }, function (timerId: any) { fnApply(clearFn, UNDEF_VALUE, [ timerId ]); }); startTimer && handler.h.refresh(); return handler.h; } /** * @internal * @ignore * Runs the pending fallback microtasks (when there is no native queueMicrotask), which is done before the callback of a * timeout or an interval so (like native microtasks) they run before a timer which was started before them. */ export function _runMicrotasks(): void { let microTasksFn = _microtaskCallback; if (microTasksFn) { _microtaskCallback = UNDEF_VALUE; microTasksFn(); } } /** * @internal * Internal function to set the microtask callback used by the microtask scheduling functions, this is used as * a hook to allow the timeout implementation to run any pending microtasks before running a timeout callback * @since 0.15.0 * @param queueFn - The function to be called to flush the microtask queue, this will be set by the microtask * scheduling functions when they need to schedule a flush of the microtask queue */ export function _setMicrotaskCallback(queueFn: (() => void) | undefined): void { if (!_microtaskCallback && isFunction(queueFn)) { _microtaskCallback = queueFn; } } /** * Sets the setTimeout and clearTimeout override functions for this package/closure instance to be used by all timeout operations * when no specific override functions are provided. If called with no parameters or undefined, * it will reset both overrides to undefined, reverting to native implementations. * * @since 0.12.3 * @group Timer * * @param overrideFn - setTimeout override function this will be called instead of the `setTimeout`, if the value * of `overrideFn` is null or undefined it will reset both setTimeout and clearTimeout to their native implementations. * May also be an array with both the setTimeout and clearTimeout override functions, if either is not provided the * default native functions will be used * @remarks * These override functions apply only to this package instance and do not affect the global setTimeout/clearTimeout functions * used elsewhere in your application. * @example * ```ts * // Override with a single function for setTimeout * // The native clearTimeout will still be used * function customSetTimeout(callback: (...args: TArgs) => void, ms?: number, ...args: TArgs) { * console.log(`Setting timeout for ${ms}ms`); * return setTimeout(callback, ms); * } * * // Set the timeout override function * setTimeoutOverrides(customSetTimeout); * * // Now all timeout operations will use the custom setTimeout * let timer = scheduleTimeout(() => { * console.log("Timer triggered"); * }, 100); * * // Reset to native implementations * setTimeoutOverrides(undefined); * ``` * @example * ```ts * // Override both setTimeout and clearTimeout with custom implementations * function customSetTimeout(callback: (...args: TArgs) => void, ms?: number, ...args: TArgs) { * console.log(`Setting timeout for ${ms}ms`); * return setTimeout(callback, ms); * } * * function customClearTimeout(timeoutId: number) { * console.log(`Clearing timeout ${timeoutId}`); * return clearTimeout(timeoutId); * } * * // Set both override functions * setTimeoutOverrides([customSetTimeout, customClearTimeout]); * * // Now all timeout operations will use the custom implementations * let timer = scheduleTimeout(() => { * console.log("Timer triggered"); * }, 100); * * // This will use the custom clearTimeout * timer.cancel(); * * // Reset to native implementations * setTimeoutOverrides(); * ``` */ export function setTimeoutOverrides(overrideFn?: TimeoutOverrideFn | TimeoutOverrideFuncs): void { let overrides = _getOverrides(overrideFn, UNDEF_VALUE); _setTimeoutFn = overrides[0]; _clearTimeoutFn = overrides[1]; } /** * Sets global timeout override functions that will be used as fallback by all timeout operations * when no specific or default package override functions are provided. If called with no parameters or * undefined, it will reset the global overrides to undefined. * * @since 0.12.3 * @group Timer * * @param overrideFn - setTimeout override function this will be called instead of the `setTimeout`, if the value * of `overrideFn` is null or undefined it will reset the global overrides to undefined. * May also be an array with both the setTimeout and clearTimeout override functions, if either is not provided the * default native functions will be used * @remarks * The global timeout overrides serve as a fallback when no specific or package level overrides are provided. * The priority order for timeout functions is: * 1. Specific override provided to the individual function call for {@link scheduleTimeoutWith} or {@link createTimeoutWith} * 2. Package instance overrides set via {@link setTimeoutOverrides} * 3. Global overrides set via this function * 4. Native setTimeout/clearTimeout functions * @example * ```ts * // Override with global custom setTimeout and clearTimeout implementations * function customSetTimeout(callback: (...args: TArgs) => void, ms?: number, ...args: TArgs) { * console.log(`Global: Setting timeout for ${ms}ms`); * return setTimeout(callback, ms); * } * * function customClearTimeout(timeoutId: number) { * console.log(`Global: Clearing timeout ${timeoutId}`); * return clearTimeout(timeoutId); * } * * // Set global override functions * setGlobalTimeoutOverrides([customSetTimeout, customClearTimeout]); * * // Now all timeout operations will use these global implementations when no other overrides are provided * let timer = scheduleTimeout(() => { * console.log("Timer triggered"); * }, 100); * * // Reset global overrides * setGlobalTimeoutOverrides(); * ``` */ export function setGlobalTimeoutOverrides(overrideFn?: TimeoutOverrideFn | TimeoutOverrideFuncs): void { // Set the global timeout overrides, or reset them when no override is provided _getGlobalConfig().tmOut = overrideFn ? _getOverrides(overrideFn, null) : undefined; } /** * Creates and starts a timer which executes a function or specified piece of code once the timer expires, this is simular * to using `setTimeout` but provides a return object for cancelling and restarting (refresh) the timer. * * The timer may be cancelled (cleared) by calling the `cancel()` function on the returned {@link ITimerHandler}, or * you can "reschedule" and/or "restart" the timer by calling the `refresh()` function on the returned {@link ITimerHandler} * instance * * @since 0.4.4 * @group Timer * * @param callback - The function to be executed after the timer expires. * @param timeout - The time, in milliseconds that the timer should wait before the specified * function or code is executed. If this parameter is omitted, a value of 0 is used, meaning * execute "immediately", or more accurately, the next event cycle. * @param args - Additional arguments which are passed through to the function specified by `callback`. * @returns A {@link ITimerHandler} instance which can be used to cancel the timeout. * @example * ```ts * let timeoutCalled = false; * let theTimeout = scheduleTimeout(() => { * // This callback will be called after 100ms as this uses setTimeout() * timeoutCalled = true; * }, 100); * * // Instead of calling clearTimeout() with the returned value from setTimeout() the returned * // handler instance can be used instead to cancel the timer * theTimeout.cancel(); * theTimeout.enabled; // false * * // You can start the timer via enabled * theTimeout.enabled = true; * * // You can also "restart" the timer, whether it has previously triggered not not via the `refresh()` * theTimeout.refresh(); * ``` */ export function scheduleTimeout(callback: (...args: A) => void, timeout: number, ...args: A): ITimerHandler; /** * Creates and starts a timer which executes a function or specified piece of code once the timer expires, this is simular * to using `setTimeout` but provides a return object for cancelling and restarting (refresh) the timer. * * The timer may be cancelled (cleared) by calling the `cancel()` function on the returned {@link ITimerHandler}, or * you can "reschedule" and/or "restart" the timer by calling the `refresh()` function on the returned {@link ITimerHandler} * instance * * @since 0.4.4 * @group Timer * * @param callback - The function to be executed after the timer expires. * @param timeout - The time, in milliseconds that the timer should wait before the specified * function or code is executed. If this parameter is omitted, a value of 0 is used, meaning * execute "immediately", or more accurately, the next event cycle. * @param args - Additional arguments which are passed through to the function specified by `callback`. * @returns A {@link ITimerHandler} instance which can be used to cancel the timeout. * @example * ```ts * let timeoutCalled = false; * let theTimeout = scheduleTimeout(() => { * // This callback will be called after 100ms as this uses setTimeout() * timeoutCalled = true; * }, 100); * * // Instead of calling clearTimeout() with the returned value from setTimeout() the returned * // handler instance can be used instead to cancel the timer * theTimeout.cancel(); * theTimeout.enabled; // false * * // You can start the timer via enabled * theTimeout.enabled = true; * * // You can also "restart" the timer, whether it has previously triggered not not via the `refresh()` * theTimeout.refresh(); * ``` */ export function scheduleTimeout(callback: (...args: A) => void, timeout: number): ITimerHandler { return _createTimeoutWith(true, UNDEF_VALUE, ArrSlice[CALL](arguments)); } /** * Creates and starts a timer which executes a function or specified piece of code once the timer expires. The overrideFn will be * used to create the timer, this is simular to using `setTimeout` but provides a return object for cancelling and restarting * (refresh) the timer. * * The timer may be cancelled (cleared) by calling the `cancel()` function on the returned {@link ITimerHandler}, or * you can "reschedule" and/or "restart" the timer by calling the `refresh()` function on the returned {@link ITimerHandler} * instance * * @since 0.4.4 * @group Timer * * @param overrideFn - setTimeout override function this will be called instead of the `setTimeout`, if the value * of `overrideFn` is null or undefined it will revert back to the native `setTimeout`. May also be an array with contains * both the setTimeout and clearTimeout override functions, if either is not provided the default native functions will be used * @param callback - The function to be executed after the timer expires. * @param timeout - The time, in milliseconds that the timer should wait before the specified * function or code is executed. If this parameter is omitted, a value of 0 is used, meaning * execute "immediately", or more accurately, the next event cycle. * @param args - Additional arguments which are passed through to the function specified by `callback`. * @returns A {@link ITimerHandler} instance which can be used to cancel the timeout. * @example * ```ts * let timeoutCalled = false; * // Your own "setTimeout" implementation to allow you to perform additional operations or possible wrap * // the callback to add timings. * function newSetTimeoutFn(callback: TimeoutOverrideFn) { * overrideCalled ++; * return setTimeout(callback, timeout); * } * * let theTimeout = scheduleTimeoutWith(newSetTimeoutFn, () => { * // This callback will be called after 100ms as this uses setTimeout() * timeoutCalled = true; * }, 100); * * // Instead of calling clearTimeout() with the returned value from setTimeout() the returned * // handler instance can be used instead to cancel the timer * theTimeout.cancel(); * theTimeout.enabled; // false * * // You can start the timer via enabled * theTimeout.enabled = true; * * // You can also "restart" the timer, whether it has previously triggered not not via the `refresh()` * theTimeout.refresh(); * ``` * @example * ```ts * let timeoutCalled = false; * // Your own "setTimeout" implementation to allow you to perform additional operations or possible wrap * // the callback to add timings. * function newSetTimeoutFn(callback: TimeoutOverrideFn) { * overrideCalled ++; * return setTimeout(callback, timeout); * } * * // Your own "clearTimeout" implementation to allow you to perform additional operations or possible wrap * // the callback to add timings. * function newClearTimeoutFn(timeoutId: number) { * overrideCalled ++; * return clearTimeout( timeout); * } * * let theTimeout = scheduleTimeoutWith([newSetTimeoutFn, newClearTimeoutFn], () => { * // This callback will be called after 100ms as this uses setTimeout() * timeoutCalled = true; * }, 100); * * // Instead of calling clearTimeout() with the returned value from setTimeout() the returned * // handler instance can be used instead to cancel the timer, internally this will call the newClearTimeoutFn * theTimeout.cancel(); * theTimeout.enabled; // false * * // You can start the timer via enabled * theTimeout.enabled = true; * * // You can also "restart" the timer, whether it has previously triggered not not via the `refresh()` * theTimeout.refresh(); * ``` */ export function scheduleTimeoutWith(overrideFn: TimeoutOverrideFn | TimeoutOverrideFuncs, callback: (...args: A) => void, timeout: number, ...args: A): ITimerHandler; /** * Creates and starts a timer which executes a function or specified piece of code once the timer expires. The overrideFn will be * used to create the timer, this is simular to using `setTimeout` but provides a return object for cancelling and restarting * (refresh) the timer. * * The timer may be cancelled (cleared) by calling the `cancel()` function on the returned {@link ITimerHandler}, or * you can "reschedule" and/or "restart" the timer by calling the `refresh()` function on the returned {@link ITimerHandler} * instance * * @since 0.4.4 * @group Timer * * @param overrideFn - setTimeout override function this will be called instead of the `setTimeout`, if the value * of `overrideFn` is null or undefined it will revert back to the native `setTimeout`. May also be an array with contains * both the setTimeout and clearTimeout override functions, if either is not provided the default native functions will be used * @param callback - The function to be executed after the timer expires. * @param timeout - The time, in milliseconds that the timer should wait before the specified * function or code is executed. If this parameter is omitted, a value of 0 is used, meaning * execute "immediately", or more accurately, the next event cycle. * @param args - Additional arguments which are passed through to the function specified by `callback`. * @returns A {@link ITimerHandler} instance which can be used to cancel the timeout. * @example * ```ts * let timeoutCalled = false; * // Your own "setTimeout" implementation to allow you to perform additional operations or possible wrap * // the callback to add timings. * function newSetTimeoutFn(callback: TimeoutOverrideFn) { * overrideCalled ++; * return setTimeout(callback, timeout); * } * * let theTimeout = scheduleTimeoutWith(newSetTimeoutFn, () => { * // This callback will be called after 100ms as this uses setTimeout() * timeoutCalled = true; * }, 100); * * // Instead of calling clearTimeout() with the returned value from setTimeout() the returned * // handler instance can be used instead to cancel the timer * theTimeout.cancel(); * theTimeout.enabled; // false * * // You can start the timer via enabled * theTimeout.enabled = true; * * // You can also "restart" the timer, whether it has previously triggered not not via the `refresh()` * theTimeout.refresh(); * ``` * @example * ```ts * let timeoutCalled = false; * // Your own "setTimeout" implementation to allow you to perform additional operations or possible wrap * // the callback to add timings. * function newSetTimeoutFn(callback: TimeoutOverrideFn) { * overrideCalled ++; * return setTimeout(callback, timeout); * } * * // Your own "clearTimeout" implementation to allow you to perform additional operations or possible wrap * // the callback to add timings. * function newClearTimeoutFn(timeoutId: number) { * overrideCalled ++; * return clearTimeout( timeout); * } * * let theTimeout = scheduleTimeoutWith([newSetTimeoutFn, newClearTimeoutFn], () => { * // This callback will be called after 100ms as this uses setTimeout() * timeoutCalled = true; * }, 100); * * // Instead of calling clearTimeout() with the returned value from setTimeout() the returned * // handler instance can be used instead to cancel the timer, internally this will call the newClearTimeoutFn * theTimeout.cancel(); * theTimeout.enabled; // false * * // You can start the timer via enabled * theTimeout.enabled = true; * * // You can also "restart" the timer, whether it has previously triggered not not via the `refresh()` * theTimeout.refresh(); * ``` */ export function scheduleTimeoutWith(overrideFn: TimeoutOverrideFn | TimeoutOverrideFuncs, callback: (...args: A) => void, timeout: number): ITimerHandler { return _createTimeoutWith(true, overrideFn, ArrSlice[CALL](arguments, 1)); } /** * Creates a non-running (paused) timer which will execute a function or specified piece of code when enabled and the timer expires, * this is simular to using `scheduleTimeout` but the timer is not enabled (running) and you MUST call `refresh` to start the timer. * * The timer may be cancelled (cleared) by calling the `cancel()` function on the returned {@link ITimerHandler}, or * you can "reschedule" and/or "restart" the timer by calling the `refresh()` function on the returned {@link ITimerHandler} * instance * * @since 0.7.0 * @group Timer * * @param callback - The function to be executed after the timer expires. * @param timeout - The time, in milliseconds that the timer should wait before the specified * function or code is executed. If this parameter is omitted, a value of 0 is used, meaning * execute "immediately", or more accurately, the next event cycle. * @param args - Additional arguments which are passed through to the function specified by `callback`. * @returns A {@link ITimerHandler} instance which can be used to cancel the timeout. * @example * ```ts * let timeoutCalled = false; * let theTimeout = createTimeout(() => { * // This callback will be called after 100ms as this uses setTimeout() * timeoutCalled = true; * }, 100); * * // As the timer is not started you will need to call "refresh" to start the timer * theTimeout.refresh(); * * // or set enabled to true * theTimeout.enabled = true; * ``` */ export function createTimeout(callback: (...args: A) => void, timeout: number, ...args: A): ITimerHandler; /** * Creates a non-running (paused) timer which will execute a function or specified piece of code when enabled and the timer expires, * this is simular to using `scheduleTimeout` but the timer is not enabled (running) and you MUST call `refresh` to start the timer. * * The timer may be cancelled (cleared) by calling the `cancel()` function on the returned {@link ITimerHandler}, or * you can "reschedule" and/or "restart" the timer by calling the `refresh()` function on the returned {@link ITimerHandler} * instance * * @since 0.7.0 * @group Timer * * @param callback - The function to be executed after the timer expires. * @param timeout - The time, in milliseconds that the timer should wait before the specified * function or code is executed. If this parameter is omitted, a value of 0 is used, meaning * execute "immediately", or more accurately, the next event cycle. * @param args - Additional arguments which are passed through to the function specified by `callback`. * @returns A {@link ITimerHandler} instance which can be used to cancel the timeout. * @example * ```ts * let timeoutCalled = false; * let theTimeout = createTimeout(() => { * // This callback will be called after 100ms as this uses setTimeout() * timeoutCalled = true; * }, 100); * * // As the timer is not started you will need to call "refresh" to start the timer * theTimeout.refresh(); * * // or set enabled to true * theTimeout.enabled = true; * ``` */ export function createTimeout(callback: (...args: A) => void, timeout: number): ITimerHandler { return _createTimeoutWith(false, UNDEF_VALUE, ArrSlice[CALL](arguments)); } /** * Creates a non-running (paused) timer which will execute a function or specified piece of code when enabled once the timer expires. * The overrideFn will be used to create the timer, this is simular to using `scheduleTimeoutWith` but the timer is not enabled (running) * and you MUST call `refresh` to start the timer. * * The timer may be cancelled (cleared) by calling the `cancel()` function on the returned {@link ITimerHandler}, or * you can "reschedule" and/or "restart" the timer by calling the `refresh()` function on the returned {@link ITimerHandler} * instance * * @since 0.7.0 * @group Timer * * @param overrideFn - setTimeout override function this will be called instead of the `setTimeout`, if the value * of `overrideFn` is null or undefined it will revert back to the native `setTimeout`. May also be an array with contains * both the setTimeout and clearTimeout override functions, if either is not provided the default native functions will be used * @param callback - The function to be executed after the timer expires. * @param timeout - The time, in milliseconds that the timer should wait before the specified * function or code is executed. If this parameter is omitted, a value of 0 is used, meaning * execute "immediately", or more accurately, the next event cycle. * @param args - Additional arguments which are passed through to the function specified by `callback`. * @returns A {@link ITimerHandler} instance which can be used to cancel the timeout. * @example * ```ts * let timeoutCalled = false; * // Your own "setTimeout" implementation to allow you to perform additional operations or possible wrap * // the callback to add timings. * function newSetTimeoutFn(callback: TimeoutOverrideFn) { * overrideCalled ++; * return setTimeout(callback, timeout); * } * * let theTimeout = createTimeoutWith(newSetTimeoutFn, () => { * // This callback will be called after 100ms as this uses setTimeout() * timeoutCalled = true; * }, 100); * * // As the timer is not started you will need to call "refresh" to start the timer * theTimeout.refresh(); * * // or set enabled to true * theTimeout.enabled = true; * ``` * @example * ```ts * let timeoutCalled = false; * // Your own "setTimeout" implementation to allow you to perform additional operations or possible wrap * // the callback to add timings. * function newSetTimeoutFn(callback: TimeoutOverrideFn) { * overrideCalled ++; * return setTimeout(callback, timeout); * } * * // Your own "clearTimeout" implementation to allow you to perform additional operations or possible wrap * // the callback to add timings. * function newClearTimeoutFn(timeoutId: number) { * overrideCalled ++; * return clearTimeout( timeout); * } * * let theTimeout = createTimeoutWith([newSetTimeoutFn, newClearTimeoutFn], () => { * // This callback will be called after 100ms as this uses setTimeout() * timeoutCalled = true; * }, 100); * * // As the timer is not started you will need to call "refresh" to start the timer * theTimeout.refresh(); * * // or set enabled to true * theTimeout.enabled = true; * ``` */ export function createTimeoutWith(overrideFn: TimeoutOverrideFn | TimeoutOverrideFuncs, callback: (...args: A) => void, timeout: number, ...args: A): ITimerHandler; /** * Creates a non-running (paused) timer which will execute a function or specified piece of code when enabled once the timer expires. * The overrideFn will be used to create the timer, this is simular to using `scheduleTimeoutWith` but the timer is not enabled (running) * and you MUST call `refresh` to start the timer. * * The timer may be cancelled (cleared) by calling the `cancel()` function on the returned {@link ITimerHandler}, or * you can "reschedule" and/or "restart" the timer by calling the `refresh()` function on the returned {@link ITimerHandler} * instance * * @since 0.7.0 * @group Timer * * @param overrideFn - setTimeout override function this will be called instead of the `setTimeout`, if the value * of `overrideFn` is null or undefined it will revert back to the native `setTimeout`. May also be an array with contains * both the setTimeout and clearTimeout override functions, if either is not provided the default native functions will be used * @param callback - The function to be executed after the timer expires. * @param timeout - The time, in milliseconds that the timer should wait before the specified * function or code is executed. If this parameter is omitted, a value of 0 is used, meaning * execute "immediately", or more accurately, the next event cycle. * @param args - Additional arguments which are passed through to the function specified by `callback`. * @returns A {@link ITimerHandler} instance which can be used to cancel the timeout. * @example * ```ts * let timeoutCalled = false; * // Your own "setTimeout" implementation to allow you to perform additional operations or possible wrap * // the callback to add timings. * function newSetTimeoutFn(callback: TimeoutOverrideFn) { * overrideCalled ++; * return setTimeout(callback, timeout); * } * * let theTimeout = createTimeoutWith(newSetTimeoutFn, () => { * // This callback will be called after 100ms as this uses setTimeout() * timeoutCalled = true; * }, 100); * * // As the timer is not started you will need to call "refresh" to start the timer * theTimeout.refresh(); * * // or set enabled to true * theTimeout.enabled = true; * ``` * @example * ```ts * let timeoutCalled = false; * // Your own "setTimeout" implementation to allow you to perform additional operations or possible wrap * // the callback to add timings. * function newSetTimeoutFn(callback: TimeoutOverrideFn) { * overrideCalled ++; * return setTimeout(callback, timeout); * } * * // Your own "clearTimeout" implementation to allow you to perform additional operations or possible wrap * // the callback to add timings. * function newClearTimeoutFn(timeoutId: number) { * overrideCalled ++; * return clearTimeout( timeout); * } * * let theTimeout = createTimeoutWith([newSetTimeoutFn, newClearTimeoutFn], () => { * // This callback will be called after 100ms as this uses setTimeout() * timeoutCalled = true; * }, 100); * * // As the timer is not started you will need to call "refresh" to start the timer * theTimeout.refresh(); * * // or set enabled to true * theTimeout.enabled = true; * ``` */ export function createTimeoutWith(overrideFn: TimeoutOverrideFn | TimeoutOverrideFuncs, callback: (...args: A) => void, timeout: number): ITimerHandler { return _createTimeoutWith(false, overrideFn, ArrSlice[CALL](arguments, 1)); }