]> git.kaiwu.me - njs.git/commitdiff
Types: added definitions for timer methods.
authorJakub Jirutka <jakub@jirutka.cz>
Tue, 24 Nov 2020 23:12:04 +0000 (00:12 +0100)
committerJakub Jirutka <jakub@jirutka.cz>
Tue, 24 Nov 2020 23:12:04 +0000 (00:12 +0100)
test/ts/test.ts
ts/njs_core.d.ts

index 4aae7812d883abb46eca3047044315162ae46752..65cf52f74de14f660041bbf33713b64bf6423dac 100644 (file)
@@ -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];
index ae6d07c5b547b16ad0486ba96c7196bef07f3bfb..54172d11eca87f2a6681e338689e540cc351bc40 100644 (file)
@@ -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;