MCPcopy Create free account
hub / github.com/Distributive-Network/PythonMonkey / Timeout

Class Timeout

python/pythonmonkey/builtin_modules/timers.js:24–96  ·  view source on GitHub ↗

* Implement Node.js-style `timeoutId` class returned from setTimeout() and setInterval() * @see https://nodejs.org/api/timers.html#class-timeout

Source from the content-addressed store, hash-verified

22 * @see https://nodejs.org/api/timers.html#class-timeout
23 */
24class Timeout
25{
26 /** @type {number} an integer */
27 #numericId;
28
29 /**
30 * @param {number} numericId
31 */
32 constructor(numericId)
33 {
34 this.#numericId = numericId;
35 }
36
37 /**
38 * If `true`, the `Timeout` object will keep the event-loop active.
39 * @returns {boolean}
40 */
41 hasRef()
42 {
43 return timerHasRef(this.#numericId);
44 }
45
46 /**
47 * When called, requests that the event-loop **not exit** so long as the `Timeout` is active.
48 *
49 * By default, all `Timeout` objects are "ref'ed", making it normally unnecessary to call `timeout.ref()` unless `timeout.unref()` had been called previously.
50 */
51 ref()
52 {
53 timerAddRef(this.#numericId);
54 return this; // allow chaining
55 }
56
57 /**
58 * When called, the active `Timeout` object will not require the event-loop to remain active.
59 * If there is no other activity keeping the event-loop running, the process may exit before the `Timeout` object's callback is invoked.
60 */
61 unref()
62 {
63 timerRemoveRef(this.#numericId);
64 return this; // allow chaining
65 }
66
67 /**
68 * Sets the timer's start time to the current time,
69 * and reschedules the timer to call its callback at the previously specified duration adjusted to the current time.
70 *
71 * Using this on a timer that has already called its callback will reactivate the timer.
72 */
73 refresh()
74 {
75 throw new DOMException('Timeout.refresh() method is not supported by PythonMonkey.', 'NotSupportedError');
76 // TODO: Do we really need to closely resemble the Node.js API?
77 // This one is not easy to implement since we need to memorize the callback function and delay parameters in every `setTimeout`/`setInterval` call.
78 }
79
80 /**
81 * Cancels the timeout.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected