From 980c154b8b9a79733811ea3b914fa2c8e5fe4ada Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Wed, 25 Nov 2020 00:12:04 +0100 Subject: [PATCH] Types: added definitions for setTimeout, setImmediate and clearTimeout. --- test/ts/test.ts | 14 ++++++++++++++ ts/njs_core.d.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/test/ts/test.ts b/test/ts/test.ts index 42a6ae8a4..2c87f163b 100644 --- a/test/ts/test.ts +++ b/test/ts/test.ts @@ -106,6 +106,20 @@ function buffer(b: Buffer) { b.equals(b); } +function timers() { + var handle:TimerHandle; + + handle = setTimeout(() => {}); + handle = setTimeout(() => {}, 100); + handle = setTimeout((a:string, b:number) => {}, 100, 'foo', 42); + + handle = setImmediate(() => {}); + handle = setImmediate((a:string, b:number) => {}, 'foo', 42); + + clearTimeout(handle); + // Warning: clearTimeout(123); +} + function njs_object() { njs.dump('asdf'); njs.version != process.argv[1]; diff --git a/ts/njs_core.d.ts b/ts/njs_core.d.ts index ae6d07c5b..54172d11e 100644 --- a/ts/njs_core.d.ts +++ b/ts/njs_core.d.ts @@ -607,3 +607,45 @@ interface NjsProcess { } declare const process: NjsProcess; + +/** + * A value returned by `setTimeout()` and `setImmediate()` functions. It's an positive integer now, + * but this may be changed in future, so it should be treated as an opaque value. + */ +type TimerHandle = number & { readonly '': unique symbol }; + +/** + * Schedules the "immediate" execution of the given function after I/O events' callbacks. + * + * @param callback The function to call. + * @param args Optional arguments to pass to the `callback` function. + * @returns A value which identifies the timer created by the call. + * + * @throws {TypeError} if `callback` is not a function. + * @throws {InternalError} if timers are not supported by host environment. + */ +declare function setImmediate(callback: (...args: TArgs) => void, ...args: TArgs): TimerHandle; + +/** + * Schedules a timer which executes the given function after the specified delay. + * + * @param callback The function to call when the timer elapses. + * @param delay The number of milliseconds to wait before calling the `callback`. Defaults to `0`, + * meaning execute "immediately", or more accurately, the next event cycle. + * @param args Optional arguments to pass to the `callback` function. + * @returns A value which identifies the timer created by the call; it can be passed to + * `clearTimeout()` to cancel the timeout. + * + * @throws {TypeError} if `callback` is not a function. + * @throws {InternalError} if timers are not supported by host environment. + */ +declare function setTimeout(callback: (...args: TArgs) => void, delay?: number, ...args: TArgs): TimerHandle; + +/** + * Cancels a timer previously established by calling `setTimeout()`. + * + * Note: Passing an invalid handle silently does nothing; no exception is thrown. + * + * @param handle A value returned by `setTimeout()`. + */ +declare function clearTimeout(handle?: TimerHandle): void;