Welcome! Log In Create A New Profile

Advanced

[njs] Types: added definitions for timer methods.

Dmitry Volyntsev
November 30, 2020 01:14PM
details: https://hg.nginx.org/njs/rev/5e29ce36383e
branches:
changeset: 1578:5e29ce36383e
user: Jakub Jirutka <jakub@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;
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Types: added definitions for timer methods.

Dmitry Volyntsev 438 November 30, 2020 01:14PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 296
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready