CoolFace
Apppublic

Pinsave/counterstrike

sourceHugging Faceupdated 3mo agoView on Hugging Face
1likes
timers.d.ts288 linesDownload Raw Back to node
1/**2 * The `timer` module exposes a global API for scheduling functions to3 * be called at some future period of time. Because the timer functions are4 * globals, there is no need to import `node:timers` to use the API.5 *6 * The timer functions within Node.js implement a similar API as the timers API7 * provided by Web Browsers but use a different internal implementation that is8 * built around the Node.js [Event Loop](https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-settimeout).9 * @see [source](https://github.com/nodejs/node/blob/v24.x/lib/timers.js)10 */11declare module "timers" {12    import { Abortable } from "node:events";13    import * as promises from "node:timers/promises";14    export interface TimerOptions extends Abortable {15        /**16         * Set to `false` to indicate that the scheduled `Timeout`17         * should not require the Node.js event loop to remain active.18         * @default true19         */20        ref?: boolean | undefined;21    }22    global {23        namespace NodeJS {24            /**25             * This object is created internally and is returned from `setImmediate()`. It26             * can be passed to `clearImmediate()` in order to cancel the scheduled27             * actions.28             *29             * By default, when an immediate is scheduled, the Node.js event loop will continue30             * running as long as the immediate is active. The `Immediate` object returned by31             * `setImmediate()` exports both `immediate.ref()` and `immediate.unref()`32             * functions that can be used to control this default behavior.33             */34            interface Immediate extends RefCounted, Disposable {35                /**36                 * If true, the `Immediate` object will keep the Node.js event loop active.37                 * @since v11.0.038                 */39                hasRef(): boolean;40                /**41                 * When called, requests that the Node.js event loop _not_ exit so long as the42                 * `Immediate` is active. Calling `immediate.ref()` multiple times will have no43                 * effect.44                 *45                 * By default, all `Immediate` objects are "ref'ed", making it normally unnecessary46                 * to call `immediate.ref()` unless `immediate.unref()` had been called previously.47                 * @since v9.7.048                 * @returns a reference to `immediate`49                 */50                ref(): this;51                /**52                 * When called, the active `Immediate` object will not require the Node.js event53                 * loop to remain active. If there is no other activity keeping the event loop54                 * running, the process may exit before the `Immediate` object's callback is55                 * invoked. Calling `immediate.unref()` multiple times will have no effect.56                 * @since v9.7.057                 * @returns a reference to `immediate`58                 */59                unref(): this;60                /**61                 * Cancels the immediate. This is similar to calling `clearImmediate()`.62                 * @since v20.5.0, v18.18.063                 * @experimental64                 */65                [Symbol.dispose](): void;66                _onImmediate(...args: any[]): void;67            }68            // Legacy interface used in Node.js v9 and prior69            // TODO: remove in a future major version bump70            /** @deprecated Use `NodeJS.Timeout` instead. */71            interface Timer extends RefCounted {72                hasRef(): boolean;73                refresh(): this;74                [Symbol.toPrimitive](): number;75            }76            /**77             * This object is created internally and is returned from `setTimeout()` and78             * `setInterval()`. It can be passed to either `clearTimeout()` or79             * `clearInterval()` in order to cancel the scheduled actions.80             *81             * By default, when a timer is scheduled using either `setTimeout()` or82             * `setInterval()`, the Node.js event loop will continue running as long as the83             * timer is active. Each of the `Timeout` objects returned by these functions84             * export both `timeout.ref()` and `timeout.unref()` functions that can be used to85             * control this default behavior.86             */87            interface Timeout extends RefCounted, Disposable, Timer {88                /**89                 * Cancels the timeout.90                 * @since v0.9.191                 * @legacy Use `clearTimeout()` instead.92                 * @returns a reference to `timeout`93                 */94                close(): this;95                /**96                 * If true, the `Timeout` object will keep the Node.js event loop active.97                 * @since v11.0.098                 */99                hasRef(): boolean;100                /**101                 * When called, requests that the Node.js event loop _not_ exit so long as the102                 * `Timeout` is active. Calling `timeout.ref()` multiple times will have no effect.103                 *104                 * By default, all `Timeout` objects are "ref'ed", making it normally unnecessary105                 * to call `timeout.ref()` unless `timeout.unref()` had been called previously.106                 * @since v0.9.1107                 * @returns a reference to `timeout`108                 */109                ref(): this;110                /**111                 * Sets the timer's start time to the current time, and reschedules the timer to112                 * call its callback at the previously specified duration adjusted to the current113                 * time. This is useful for refreshing a timer without allocating a new114                 * JavaScript object.115                 *116                 * Using this on a timer that has already called its callback will reactivate the117                 * timer.118                 * @since v10.2.0119                 * @returns a reference to `timeout`120                 */121                refresh(): this;122                /**123                 * When called, the active `Timeout` object will not require the Node.js event loop124                 * to remain active. If there is no other activity keeping the event loop running,125                 * the process may exit before the `Timeout` object's callback is invoked. Calling126                 * `timeout.unref()` multiple times will have no effect.127                 * @since v0.9.1128                 * @returns a reference to `timeout`129                 */130                unref(): this;131                /**132                 * Coerce a `Timeout` to a primitive. The primitive can be used to133                 * clear the `Timeout`. The primitive can only be used in the134                 * same thread where the timeout was created. Therefore, to use it135                 * across `worker_threads` it must first be passed to the correct136                 * thread. This allows enhanced compatibility with browser137                 * `setTimeout()` and `setInterval()` implementations.138                 * @since v14.9.0, v12.19.0139                 */140                [Symbol.toPrimitive](): number;141                /**142                 * Cancels the timeout.143                 * @since v20.5.0, v18.18.0144                 * @experimental145                 */146                [Symbol.dispose](): void;147                _onTimeout(...args: any[]): void;148            }149        }150        /**151         * Schedules the "immediate" execution of the `callback` after I/O events'152         * callbacks.153         *154         * When multiple calls to `setImmediate()` are made, the `callback` functions are155         * queued for execution in the order in which they are created. The entire callback156         * queue is processed every event loop iteration. If an immediate timer is queued157         * from inside an executing callback, that timer will not be triggered until the158         * next event loop iteration.159         *160         * If `callback` is not a function, a `TypeError` will be thrown.161         *162         * This method has a custom variant for promises that is available using163         * `timersPromises.setImmediate()`.164         * @since v0.9.1165         * @param callback The function to call at the end of this turn of166         * the Node.js [Event Loop](https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-settimeout)167         * @param args Optional arguments to pass when the `callback` is called.168         * @returns for use with `clearImmediate()`169         */170        function setImmediate<TArgs extends any[]>(171            callback: (...args: TArgs) => void,172            ...args: TArgs173        ): NodeJS.Immediate;174        // Allow a single void-accepting argument to be optional in arguments lists.175        // Allows usage such as `new Promise(resolve => setTimeout(resolve, ms))` (#54258)176        // eslint-disable-next-line @typescript-eslint/no-invalid-void-type177        function setImmediate(callback: (_: void) => void): NodeJS.Immediate;178        namespace setImmediate {179            import __promisify__ = promises.setImmediate;180            export { __promisify__ };181        }182        /**183         * Schedules repeated execution of `callback` every `delay` milliseconds.184         *185         * When `delay` is larger than `2147483647` or less than `1` or `NaN`, the `delay`186         * will be set to `1`. Non-integer delays are truncated to an integer.187         *188         * If `callback` is not a function, a `TypeError` will be thrown.189         *190         * This method has a custom variant for promises that is available using191         * `timersPromises.setInterval()`.192         * @since v0.0.1193         * @param callback The function to call when the timer elapses.194         * @param delay The number of milliseconds to wait before calling the195         * `callback`. **Default:** `1`.196         * @param args Optional arguments to pass when the `callback` is called.197         * @returns for use with `clearInterval()`198         */199        function setInterval<TArgs extends any[]>(200            callback: (...args: TArgs) => void,201            delay?: number,202            ...args: TArgs203        ): NodeJS.Timeout;204        // Allow a single void-accepting argument to be optional in arguments lists.205        // Allows usage such as `new Promise(resolve => setTimeout(resolve, ms))` (#54258)206        // eslint-disable-next-line @typescript-eslint/no-invalid-void-type207        function setInterval(callback: (_: void) => void, delay?: number): NodeJS.Timeout;208        /**209         * Schedules execution of a one-time `callback` after `delay` milliseconds.210         *211         * The `callback` will likely not be invoked in precisely `delay` milliseconds.212         * Node.js makes no guarantees about the exact timing of when callbacks will fire,213         * nor of their ordering. The callback will be called as close as possible to the214         * time specified.215         *216         * When `delay` is larger than `2147483647` or less than `1` or `NaN`, the `delay`217         * will be set to `1`. Non-integer delays are truncated to an integer.218         *219         * If `callback` is not a function, a `TypeError` will be thrown.220         *221         * This method has a custom variant for promises that is available using222         * `timersPromises.setTimeout()`.223         * @since v0.0.1224         * @param callback The function to call when the timer elapses.225         * @param delay The number of milliseconds to wait before calling the226         * `callback`. **Default:** `1`.227         * @param args Optional arguments to pass when the `callback` is called.228         * @returns for use with `clearTimeout()`229         */230        function setTimeout<TArgs extends any[]>(231            callback: (...args: TArgs) => void,232            delay?: number,233            ...args: TArgs234        ): NodeJS.Timeout;235        // Allow a single void-accepting argument to be optional in arguments lists.236        // Allows usage such as `new Promise(resolve => setTimeout(resolve, ms))` (#54258)237        // eslint-disable-next-line @typescript-eslint/no-invalid-void-type238        function setTimeout(callback: (_: void) => void, delay?: number): NodeJS.Timeout;239        namespace setTimeout {240            import __promisify__ = promises.setTimeout;241            export { __promisify__ };242        }243        /**244         * Cancels an `Immediate` object created by `setImmediate()`.245         * @since v0.9.1246         * @param immediate An `Immediate` object as returned by `setImmediate()`.247         */248        function clearImmediate(immediate: NodeJS.Immediate | undefined): void;249        /**250         * Cancels a `Timeout` object created by `setInterval()`.251         * @since v0.0.1252         * @param timeout A `Timeout` object as returned by `setInterval()`253         * or the primitive of the `Timeout` object as a string or a number.254         */255        function clearInterval(timeout: NodeJS.Timeout | string | number | undefined): void;256        /**257         * Cancels a `Timeout` object created by `setTimeout()`.258         * @since v0.0.1259         * @param timeout A `Timeout` object as returned by `setTimeout()`260         * or the primitive of the `Timeout` object as a string or a number.261         */262        function clearTimeout(timeout: NodeJS.Timeout | string | number | undefined): void;263        /**264         * The `queueMicrotask()` method queues a microtask to invoke `callback`. If265         * `callback` throws an exception, the `process` object `'uncaughtException'`266         * event will be emitted.267         *268         * The microtask queue is managed by V8 and may be used in a similar manner to269         * the `process.nextTick()` queue, which is managed by Node.js. The270         * `process.nextTick()` queue is always processed before the microtask queue271         * within each turn of the Node.js event loop.272         * @since v11.0.0273         * @param callback Function to be queued.274         */275        function queueMicrotask(callback: () => void): void;276    }277    import clearImmediate = globalThis.clearImmediate;278    import clearInterval = globalThis.clearInterval;279    import clearTimeout = globalThis.clearTimeout;280    import setImmediate = globalThis.setImmediate;281    import setInterval = globalThis.setInterval;282    import setTimeout = globalThis.setTimeout;283    export { clearImmediate, clearInterval, clearTimeout, promises, setImmediate, setInterval, setTimeout };284}285declare module "node:timers" {286    export * from "timers";287}288