bc826e0b6aafd4e19d84ec9de948b21f9b5ef8cdcbbe86f9fe1f28fc718c15120e170f20c3547b7f4751a47b49ab3d3a7450cccbd0eb20fcb0f80de04259f1 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * The `timer` module exposes a global API for scheduling functions to
  3. * be called at some future period of time. Because the timer functions are
  4. * 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 API
  7. * provided by Web Browsers but use a different internal implementation that is
  8. * 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. */
  11. declare 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 true
  19. */
  20. ref?: boolean | undefined;
  21. }
  22. global {
  23. namespace NodeJS {
  24. /**
  25. * This object is created internally and is returned from `setImmediate()`. It
  26. * can be passed to `clearImmediate()` in order to cancel the scheduled
  27. * actions.
  28. *
  29. * By default, when an immediate is scheduled, the Node.js event loop will continue
  30. * running as long as the immediate is active. The `Immediate` object returned by
  31. * `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.0
  38. */
  39. hasRef(): boolean;
  40. /**
  41. * When called, requests that the Node.js event loop _not_ exit so long as the
  42. * `Immediate` is active. Calling `immediate.ref()` multiple times will have no
  43. * effect.
  44. *
  45. * By default, all `Immediate` objects are "ref'ed", making it normally unnecessary
  46. * to call `immediate.ref()` unless `immediate.unref()` had been called previously.
  47. * @since v9.7.0
  48. * @returns a reference to `immediate`
  49. */
  50. ref(): this;
  51. /**
  52. * When called, the active `Immediate` object will not require the Node.js event
  53. * loop to remain active. If there is no other activity keeping the event loop
  54. * running, the process may exit before the `Immediate` object's callback is
  55. * invoked. Calling `immediate.unref()` multiple times will have no effect.
  56. * @since v9.7.0
  57. * @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.0
  63. */
  64. [Symbol.dispose](): void;
  65. _onImmediate(...args: any[]): void;
  66. }
  67. // Legacy interface used in Node.js v9 and prior
  68. // TODO: remove in a future major version bump
  69. /** @deprecated Use `NodeJS.Timeout` instead. */
  70. interface Timer extends RefCounted {
  71. hasRef(): boolean;
  72. refresh(): this;
  73. [Symbol.toPrimitive](): number;
  74. }
  75. /**
  76. * This object is created internally and is returned from `setTimeout()` and
  77. * `setInterval()`. It can be passed to either `clearTimeout()` or
  78. * `clearInterval()` in order to cancel the scheduled actions.
  79. *
  80. * By default, when a timer is scheduled using either `setTimeout()` or
  81. * `setInterval()`, the Node.js event loop will continue running as long as the
  82. * timer is active. Each of the `Timeout` objects returned by these functions
  83. * export both `timeout.ref()` and `timeout.unref()` functions that can be used to
  84. * control this default behavior.
  85. */
  86. interface Timeout extends RefCounted, Disposable, Timer {
  87. /**
  88. * Cancels the timeout.
  89. * @since v0.9.1
  90. * @legacy Use `clearTimeout()` instead.
  91. * @returns a reference to `timeout`
  92. */
  93. close(): this;
  94. /**
  95. * If true, the `Timeout` object will keep the Node.js event loop active.
  96. * @since v11.0.0
  97. */
  98. hasRef(): boolean;
  99. /**
  100. * When called, requests that the Node.js event loop _not_ exit so long as the
  101. * `Timeout` is active. Calling `timeout.ref()` multiple times will have no effect.
  102. *
  103. * By default, all `Timeout` objects are "ref'ed", making it normally unnecessary
  104. * to call `timeout.ref()` unless `timeout.unref()` had been called previously.
  105. * @since v0.9.1
  106. * @returns a reference to `timeout`
  107. */
  108. ref(): this;
  109. /**
  110. * Sets the timer's start time to the current time, and reschedules the timer to
  111. * call its callback at the previously specified duration adjusted to the current
  112. * time. This is useful for refreshing a timer without allocating a new
  113. * JavaScript object.
  114. *
  115. * Using this on a timer that has already called its callback will reactivate the
  116. * timer.
  117. * @since v10.2.0
  118. * @returns a reference to `timeout`
  119. */
  120. refresh(): this;
  121. /**
  122. * When called, the active `Timeout` object will not require the Node.js event loop
  123. * to remain active. If there is no other activity keeping the event loop running,
  124. * the process may exit before the `Timeout` object's callback is invoked. Calling
  125. * `timeout.unref()` multiple times will have no effect.
  126. * @since v0.9.1
  127. * @returns a reference to `timeout`
  128. */
  129. unref(): this;
  130. /**
  131. * Coerce a `Timeout` to a primitive. The primitive can be used to
  132. * clear the `Timeout`. The primitive can only be used in the
  133. * same thread where the timeout was created. Therefore, to use it
  134. * across `worker_threads` it must first be passed to the correct
  135. * thread. This allows enhanced compatibility with browser
  136. * `setTimeout()` and `setInterval()` implementations.
  137. * @since v14.9.0, v12.19.0
  138. */
  139. [Symbol.toPrimitive](): number;
  140. /**
  141. * Cancels the timeout.
  142. * @since v20.5.0, v18.18.0
  143. */
  144. [Symbol.dispose](): void;
  145. _onTimeout(...args: any[]): void;
  146. }
  147. }
  148. /**
  149. * Schedules the "immediate" execution of the `callback` after I/O events'
  150. * callbacks.
  151. *
  152. * When multiple calls to `setImmediate()` are made, the `callback` functions are
  153. * queued for execution in the order in which they are created. The entire callback
  154. * queue is processed every event loop iteration. If an immediate timer is queued
  155. * from inside an executing callback, that timer will not be triggered until the
  156. * next event loop iteration.
  157. *
  158. * If `callback` is not a function, a `TypeError` will be thrown.
  159. *
  160. * This method has a custom variant for promises that is available using
  161. * `timersPromises.setImmediate()`.
  162. * @since v0.9.1
  163. * @param callback The function to call at the end of this turn of
  164. * the Node.js [Event Loop](https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-settimeout)
  165. * @param args Optional arguments to pass when the `callback` is called.
  166. * @returns for use with `clearImmediate()`
  167. */
  168. function setImmediate<TArgs extends any[]>(
  169. callback: (...args: TArgs) => void,
  170. ...args: TArgs
  171. ): NodeJS.Immediate;
  172. // Allow a single void-accepting argument to be optional in arguments lists.
  173. // Allows usage such as `new Promise(resolve => setTimeout(resolve, ms))` (#54258)
  174. // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
  175. function setImmediate(callback: (_: void) => void): NodeJS.Immediate;
  176. namespace setImmediate {
  177. import __promisify__ = promises.setImmediate;
  178. export { __promisify__ };
  179. }
  180. /**
  181. * Schedules repeated execution of `callback` every `delay` milliseconds.
  182. *
  183. * When `delay` is larger than `2147483647` or less than `1` or `NaN`, the `delay`
  184. * will be set to `1`. Non-integer delays are truncated to an integer.
  185. *
  186. * If `callback` is not a function, a `TypeError` will be thrown.
  187. *
  188. * This method has a custom variant for promises that is available using
  189. * `timersPromises.setInterval()`.
  190. * @since v0.0.1
  191. * @param callback The function to call when the timer elapses.
  192. * @param delay The number of milliseconds to wait before calling the
  193. * `callback`. **Default:** `1`.
  194. * @param args Optional arguments to pass when the `callback` is called.
  195. * @returns for use with `clearInterval()`
  196. */
  197. function setInterval<TArgs extends any[]>(
  198. callback: (...args: TArgs) => void,
  199. delay?: number,
  200. ...args: TArgs
  201. ): NodeJS.Timeout;
  202. // Allow a single void-accepting argument to be optional in arguments lists.
  203. // Allows usage such as `new Promise(resolve => setTimeout(resolve, ms))` (#54258)
  204. // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
  205. function setInterval(callback: (_: void) => void, delay?: number): NodeJS.Timeout;
  206. /**
  207. * Schedules execution of a one-time `callback` after `delay` milliseconds.
  208. *
  209. * The `callback` will likely not be invoked in precisely `delay` milliseconds.
  210. * Node.js makes no guarantees about the exact timing of when callbacks will fire,
  211. * nor of their ordering. The callback will be called as close as possible to the
  212. * time specified.
  213. *
  214. * When `delay` is larger than `2147483647` or less than `1` or `NaN`, the `delay`
  215. * will be set to `1`. Non-integer delays are truncated to an integer.
  216. *
  217. * If `callback` is not a function, a `TypeError` will be thrown.
  218. *
  219. * This method has a custom variant for promises that is available using
  220. * `timersPromises.setTimeout()`.
  221. * @since v0.0.1
  222. * @param callback The function to call when the timer elapses.
  223. * @param delay The number of milliseconds to wait before calling the
  224. * `callback`. **Default:** `1`.
  225. * @param args Optional arguments to pass when the `callback` is called.
  226. * @returns for use with `clearTimeout()`
  227. */
  228. function setTimeout<TArgs extends any[]>(
  229. callback: (...args: TArgs) => void,
  230. delay?: number,
  231. ...args: TArgs
  232. ): NodeJS.Timeout;
  233. // Allow a single void-accepting argument to be optional in arguments lists.
  234. // Allows usage such as `new Promise(resolve => setTimeout(resolve, ms))` (#54258)
  235. // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
  236. function setTimeout(callback: (_: void) => void, delay?: number): NodeJS.Timeout;
  237. namespace setTimeout {
  238. import __promisify__ = promises.setTimeout;
  239. export { __promisify__ };
  240. }
  241. /**
  242. * Cancels an `Immediate` object created by `setImmediate()`.
  243. * @since v0.9.1
  244. * @param immediate An `Immediate` object as returned by `setImmediate()`.
  245. */
  246. function clearImmediate(immediate: NodeJS.Immediate | undefined): void;
  247. /**
  248. * Cancels a `Timeout` object created by `setInterval()`.
  249. * @since v0.0.1
  250. * @param timeout A `Timeout` object as returned by `setInterval()`
  251. * or the primitive of the `Timeout` object as a string or a number.
  252. */
  253. function clearInterval(timeout: NodeJS.Timeout | string | number | undefined): void;
  254. /**
  255. * Cancels a `Timeout` object created by `setTimeout()`.
  256. * @since v0.0.1
  257. * @param timeout A `Timeout` object as returned by `setTimeout()`
  258. * or the primitive of the `Timeout` object as a string or a number.
  259. */
  260. function clearTimeout(timeout: NodeJS.Timeout | string | number | undefined): void;
  261. /**
  262. * The `queueMicrotask()` method queues a microtask to invoke `callback`. If
  263. * `callback` throws an exception, the `process` object `'uncaughtException'`
  264. * event will be emitted.
  265. *
  266. * The microtask queue is managed by V8 and may be used in a similar manner to
  267. * the `process.nextTick()` queue, which is managed by Node.js. The
  268. * `process.nextTick()` queue is always processed before the microtask queue
  269. * within each turn of the Node.js event loop.
  270. * @since v11.0.0
  271. * @param callback Function to be queued.
  272. */
  273. function queueMicrotask(callback: () => void): void;
  274. }
  275. import clearImmediate = globalThis.clearImmediate;
  276. import clearInterval = globalThis.clearInterval;
  277. import clearTimeout = globalThis.clearTimeout;
  278. import setImmediate = globalThis.setImmediate;
  279. import setInterval = globalThis.setInterval;
  280. import setTimeout = globalThis.setTimeout;
  281. export { clearImmediate, clearInterval, clearTimeout, promises, setImmediate, setInterval, setTimeout };
  282. }
  283. declare module "node:timers" {
  284. export * from "timers";
  285. }