[njs] Types: added definitions for timer methods.

Dmitry Volyntsev xeioex at nginx.com
Mon Nov 30 18:13:40 UTC 2020


details:   https://hg.nginx.org/njs/rev/5e29ce36383e
branches:  
changeset: 1578:5e29ce36383e
user:      Jakub Jirutka <jakub at jirutka.cz>
date:      Wed Nov 25 00:12:04 2020 +0100
description:
Types: added definitions for timer methods.

diffstat:

 test/ts/test.ts  |  14 ++++++++++++++
 ts/njs_core.d.ts |  42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 0 deletions(-)

diffs (73 lines):

diff -r 5a6d8e16591b -r 5e29ce36383e test/ts/test.ts
--- a/test/ts/test.ts	Fri Nov 27 13:17:53 2020 +0000
+++ b/test/ts/test.ts	Wed Nov 25 00:12:04 2020 +0100
@@ -122,6 +122,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 -r 5a6d8e16591b -r 5e29ce36383e ts/njs_core.d.ts
--- a/ts/njs_core.d.ts	Fri Nov 27 13:17:53 2020 +0000
+++ b/ts/njs_core.d.ts	Wed Nov 25 00:12:04 2020 +0100
@@ -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<TArgs extends any[]>(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<TArgs extends any[]>(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;


More information about the nginx-devel mailing list