1db8df3661c3d39a840bd62cddaf795e9e0b1849f87392f49c4087cdc722347d7cbb63156af82b959eef038292fb4f3da6ff44783f79247f0858dcee089490 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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/v22.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. * @experimental
  64. */
  65. [Symbol.dispose](): void;
  66. _onImmediate(...args: any[]): void;
  67. }
  68. // Legacy interface used in Node.js v9 and prior
  69. // TODO: remove in a future major version bump
  70. /** @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()` and
  78. * `setInterval()`. It can be passed to either `clearTimeout()` or
  79. * `clearInterval()` in order to cancel the scheduled actions.
  80. *
  81. * By default, when a timer is scheduled using either `setTimeout()` or
  82. * `setInterval()`, the Node.js event loop will continue running as long as the
  83. * timer is active. Each of the `Timeout` objects returned by these functions
  84. * export both `timeout.ref()` and `timeout.unref()` functions that can be used to
  85. * control this default behavior.
  86. */
  87. interface Timeout extends RefCounted, Disposable, Timer {
  88. /**
  89. * Cancels the timeout.
  90. * @since v0.9.1
  91. * @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.0
  98. */
  99. hasRef(): boolean;
  100. /**
  101. * When called, requests that the Node.js event loop _not_ exit so long as the
  102. * `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 unnecessary
  105. * to call `timeout.ref()` unless `timeout.unref()` had been called previously.
  106. * @since v0.9.1
  107. * @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 to
  112. * call its callback at the previously specified duration adjusted to the current
  113. * time. This is useful for refreshing a timer without allocating a new
  114. * JavaScript object.
  115. *
  116. * Using this on a timer that has already called its callback will reactivate the
  117. * timer.
  118. * @since v10.2.0
  119. * @returns a reference to `timeout`
  120. */
  121. refresh(): this;
  122. /**
  123. * When called, the active `Timeout` object will not require the Node.js event loop
  124. * 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. Calling
  126. * `timeout.unref()` multiple times will have no effect.
  127. * @since v0.9.1
  128. * @returns a reference to `timeout`
  129. */
  130. unref(): this;
  131. /**
  132. * Coerce a `Timeout` to a primitive. The primitive can be used to
  133. * clear the `Timeout`. The primitive can only be used in the
  134. * same thread where the timeout was created. Therefore, to use it
  135. * across `worker_threads` it must first be passed to the correct
  136. * thread. This allows enhanced compatibility with browser
  137. * `setTimeout()` and `setInterval()` implementations.
  138. * @since v14.9.0, v12.19.0
  139. */
  140. [Symbol.toPrimitive](): number;
  141. /**
  142. * Cancels the timeout.
  143. * @since v20.5.0, v18.18.0
  144. * @experimental
  145. */
  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 are
  155. * queued for execution in the order in which they are created. The entire callback
  156. * queue is processed every event loop iteration. If an immediate timer is queued
  157. * from inside an executing callback, that timer will not be triggered until the
  158. * 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 using
  163. * `timersPromises.setImmediate()`.
  164. * @since v0.9.1
  165. * @param callback The function to call at the end of this turn of
  166. * 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: TArgs
  173. ): 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-type
  177. 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 using
  191. * `timersPromises.setInterval()`.
  192. * @since v0.0.1
  193. * @param callback The function to call when the timer elapses.
  194. * @param delay The number of milliseconds to wait before calling the
  195. * `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: TArgs
  203. ): 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-type
  207. 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 the
  214. * 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 using
  222. * `timersPromises.setTimeout()`.
  223. * @since v0.0.1
  224. * @param callback The function to call when the timer elapses.
  225. * @param delay The number of milliseconds to wait before calling the
  226. * `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: TArgs
  234. ): 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-type
  238. 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.1
  246. * @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.1
  252. * @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.1
  259. * @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`. If
  265. * `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 to
  269. * the `process.nextTick()` queue, which is managed by Node.js. The
  270. * `process.nextTick()` queue is always processed before the microtask queue
  271. * within each turn of the Node.js event loop.
  272. * @since v11.0.0
  273. * @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. }
  285. declare module "node:timers" {
  286. export * from "timers";
  287. }