@gjsify/timers 0.3.13 → 0.3.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/esm/index.js +54 -39
- package/lib/esm/promises.js +70 -58
- package/lib/esm/timeout.js +105 -105
- package/package.json +3 -3
package/lib/esm/index.js
CHANGED
|
@@ -1,52 +1,67 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Immediate, Timeout } from "./timeout.js";
|
|
2
|
+
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* Schedule a callback to be called after `delay` milliseconds.
|
|
6
|
+
* Returns a Timeout object with ref/unref/refresh methods.
|
|
7
|
+
*/
|
|
2
8
|
function _setTimeout(callback, delay = 0, ...args) {
|
|
3
|
-
|
|
9
|
+
return new Timeout(callback, delay, args, false);
|
|
4
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* Cancel a timeout created by setTimeout.
|
|
13
|
+
*/
|
|
5
14
|
function _clearTimeout(timeout) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
15
|
+
if (timeout instanceof Timeout) {
|
|
16
|
+
timeout.close();
|
|
17
|
+
} else if (timeout != null) {
|
|
18
|
+
clearTimeout(timeout);
|
|
19
|
+
}
|
|
11
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Schedule a callback to be called repeatedly every `delay` milliseconds.
|
|
23
|
+
* Returns a Timeout object with ref/unref/refresh methods.
|
|
24
|
+
*/
|
|
12
25
|
function _setInterval(callback, delay = 0, ...args) {
|
|
13
|
-
|
|
26
|
+
return new Timeout(callback, delay, args, true);
|
|
14
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Cancel an interval created by setInterval.
|
|
30
|
+
*/
|
|
15
31
|
function _clearInterval(timeout) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
32
|
+
if (timeout instanceof Timeout) {
|
|
33
|
+
timeout.close();
|
|
34
|
+
} else if (timeout != null) {
|
|
35
|
+
clearInterval(timeout);
|
|
36
|
+
}
|
|
21
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Schedule a callback to be called on the next iteration of the event loop.
|
|
40
|
+
* Returns an Immediate object with ref/unref methods.
|
|
41
|
+
*/
|
|
22
42
|
function _setImmediate(callback, ...args) {
|
|
23
|
-
|
|
43
|
+
return new Immediate(callback, args);
|
|
24
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Cancel an immediate created by setImmediate.
|
|
47
|
+
*/
|
|
25
48
|
function _clearImmediate(immediate) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
49
|
+
if (immediate instanceof Immediate) {
|
|
50
|
+
immediate.close();
|
|
51
|
+
} else if (immediate != null) {
|
|
52
|
+
clearTimeout(immediate);
|
|
53
|
+
}
|
|
31
54
|
}
|
|
32
|
-
var
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
};
|
|
42
|
-
export {
|
|
43
|
-
Immediate,
|
|
44
|
-
Timeout,
|
|
45
|
-
_clearImmediate as clearImmediate,
|
|
46
|
-
_clearInterval as clearInterval,
|
|
47
|
-
_clearTimeout as clearTimeout,
|
|
48
|
-
index_default as default,
|
|
49
|
-
_setImmediate as setImmediate,
|
|
50
|
-
_setInterval as setInterval,
|
|
51
|
-
_setTimeout as setTimeout
|
|
55
|
+
var src_default = {
|
|
56
|
+
setTimeout: _setTimeout,
|
|
57
|
+
clearTimeout: _clearTimeout,
|
|
58
|
+
setInterval: _setInterval,
|
|
59
|
+
clearInterval: _clearInterval,
|
|
60
|
+
setImmediate: _setImmediate,
|
|
61
|
+
clearImmediate: _clearImmediate,
|
|
62
|
+
Timeout,
|
|
63
|
+
Immediate
|
|
52
64
|
};
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
export { Immediate, Timeout, _clearImmediate as clearImmediate, _clearInterval as clearInterval, _clearTimeout as clearTimeout, src_default as default, _setImmediate as setImmediate, _setInterval as setInterval, _setTimeout as setTimeout };
|
package/lib/esm/promises.js
CHANGED
|
@@ -1,65 +1,77 @@
|
|
|
1
1
|
import { Timeout } from "./timeout.js";
|
|
2
|
+
|
|
3
|
+
//#region src/promises.ts
|
|
4
|
+
/**
|
|
5
|
+
* Returns a promise that resolves after `delay` milliseconds.
|
|
6
|
+
* Supports AbortSignal for cancellation.
|
|
7
|
+
*/
|
|
2
8
|
function setTimeout(delay = 0, value, options) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
if (options?.signal?.aborted) {
|
|
11
|
+
reject(options.signal.reason ?? new DOMException("The operation was aborted", "AbortError"));
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
const timeout = new Timeout(() => {
|
|
15
|
+
cleanup();
|
|
16
|
+
resolve(value);
|
|
17
|
+
}, delay, [], false);
|
|
18
|
+
if (options?.ref === false) timeout.unref();
|
|
19
|
+
let onAbort;
|
|
20
|
+
function cleanup() {
|
|
21
|
+
if (onAbort && options?.signal) {
|
|
22
|
+
options.signal.removeEventListener("abort", onAbort);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (options?.signal) {
|
|
26
|
+
onAbort = () => {
|
|
27
|
+
timeout.close();
|
|
28
|
+
reject(options.signal.reason ?? new DOMException("The operation was aborted", "AbortError"));
|
|
29
|
+
};
|
|
30
|
+
options.signal.addEventListener("abort", onAbort, { once: true });
|
|
31
|
+
}
|
|
32
|
+
});
|
|
27
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Returns a promise that resolves on the next event loop iteration.
|
|
36
|
+
* Supports AbortSignal for cancellation.
|
|
37
|
+
*/
|
|
28
38
|
function setImmediate(value, options) {
|
|
29
|
-
|
|
39
|
+
return setTimeout(0, value, options);
|
|
30
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns an async iterable that yields at `delay` ms intervals.
|
|
43
|
+
* Supports AbortSignal for cancellation.
|
|
44
|
+
*/
|
|
31
45
|
async function* setInterval(delay = 0, value, options) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
46
|
+
if (options?.signal?.aborted) {
|
|
47
|
+
throw options.signal.reason ?? new DOMException("The operation was aborted", "AbortError");
|
|
48
|
+
}
|
|
49
|
+
while (true) {
|
|
50
|
+
if (options?.signal?.aborted) {
|
|
51
|
+
throw options.signal.reason ?? new DOMException("The operation was aborted", "AbortError");
|
|
52
|
+
}
|
|
53
|
+
yield await new Promise((resolve, reject) => {
|
|
54
|
+
const timeout = new Timeout(() => {
|
|
55
|
+
cleanup();
|
|
56
|
+
resolve(value);
|
|
57
|
+
}, delay, [], false);
|
|
58
|
+
if (options?.ref === false) timeout.unref();
|
|
59
|
+
let onAbort;
|
|
60
|
+
function cleanup() {
|
|
61
|
+
if (onAbort && options?.signal) {
|
|
62
|
+
options.signal.removeEventListener("abort", onAbort);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (options?.signal) {
|
|
66
|
+
onAbort = () => {
|
|
67
|
+
timeout.close();
|
|
68
|
+
reject(options.signal.reason ?? new DOMException("The operation was aborted", "AbortError"));
|
|
69
|
+
};
|
|
70
|
+
options.signal.addEventListener("abort", onAbort, { once: true });
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
60
74
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
setTimeout
|
|
65
|
-
};
|
|
75
|
+
|
|
76
|
+
//#endregion
|
|
77
|
+
export { setImmediate, setInterval, setTimeout };
|
package/lib/esm/timeout.js
CHANGED
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
class Immediate {
|
|
71
|
-
_id = null;
|
|
72
|
-
_ref = true;
|
|
73
|
-
_cancelled = false;
|
|
74
|
-
constructor(callback, args) {
|
|
75
|
-
Promise.resolve().then(() => {
|
|
76
|
-
if (!this._cancelled) {
|
|
77
|
-
callback(...args);
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
ref() {
|
|
82
|
-
this._ref = true;
|
|
83
|
-
return this;
|
|
84
|
-
}
|
|
85
|
-
unref() {
|
|
86
|
-
this._ref = false;
|
|
87
|
-
return this;
|
|
88
|
-
}
|
|
89
|
-
hasRef() {
|
|
90
|
-
return this._ref;
|
|
91
|
-
}
|
|
92
|
-
close() {
|
|
93
|
-
this._cancelled = true;
|
|
94
|
-
if (this._id != null) {
|
|
95
|
-
clearTimeout(this._id);
|
|
96
|
-
this._id = null;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
[Symbol.toPrimitive]() {
|
|
100
|
-
return this._id;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
export {
|
|
104
|
-
Immediate,
|
|
105
|
-
Timeout
|
|
1
|
+
//#region src/timeout.ts
|
|
2
|
+
var Timeout = class {
|
|
3
|
+
_id;
|
|
4
|
+
_ref = true;
|
|
5
|
+
_callback;
|
|
6
|
+
_delay;
|
|
7
|
+
_args;
|
|
8
|
+
_isInterval;
|
|
9
|
+
constructor(callback, delay, args, isInterval) {
|
|
10
|
+
this._callback = callback;
|
|
11
|
+
this._delay = delay;
|
|
12
|
+
this._args = args;
|
|
13
|
+
this._isInterval = isInterval;
|
|
14
|
+
if (isInterval) {
|
|
15
|
+
this._id = setInterval(callback, delay, ...args);
|
|
16
|
+
} else {
|
|
17
|
+
this._id = setTimeout(callback, delay, ...args);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Mark this timeout as referenced (default).
|
|
22
|
+
* Referenced timers keep the event loop alive.
|
|
23
|
+
*/
|
|
24
|
+
ref() {
|
|
25
|
+
this._ref = true;
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Mark this timeout as unreferenced.
|
|
30
|
+
* Unreferenced timers do not keep the event loop alive.
|
|
31
|
+
*/
|
|
32
|
+
unref() {
|
|
33
|
+
this._ref = false;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
/** Whether this timeout is referenced. */
|
|
37
|
+
hasRef() {
|
|
38
|
+
return this._ref;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Reset the timer's start time to now and reschedule.
|
|
42
|
+
*/
|
|
43
|
+
refresh() {
|
|
44
|
+
if (this._id != null) {
|
|
45
|
+
if (this._isInterval) {
|
|
46
|
+
clearInterval(this._id);
|
|
47
|
+
this._id = setInterval(this._callback, this._delay, ...this._args);
|
|
48
|
+
} else {
|
|
49
|
+
clearTimeout(this._id);
|
|
50
|
+
this._id = setTimeout(this._callback, this._delay, ...this._args);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
/** Close/clear this timer. */
|
|
56
|
+
close() {
|
|
57
|
+
if (this._id != null) {
|
|
58
|
+
if (this._isInterval) {
|
|
59
|
+
clearInterval(this._id);
|
|
60
|
+
} else {
|
|
61
|
+
clearTimeout(this._id);
|
|
62
|
+
}
|
|
63
|
+
this._id = null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** Get the underlying timer ID (for clearTimeout/clearInterval). */
|
|
67
|
+
[Symbol.toPrimitive]() {
|
|
68
|
+
return this._id;
|
|
69
|
+
}
|
|
106
70
|
};
|
|
71
|
+
var Immediate = class {
|
|
72
|
+
_id = null;
|
|
73
|
+
_ref = true;
|
|
74
|
+
_cancelled = false;
|
|
75
|
+
constructor(callback, args) {
|
|
76
|
+
Promise.resolve().then(() => {
|
|
77
|
+
if (!this._cancelled) {
|
|
78
|
+
callback(...args);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
ref() {
|
|
83
|
+
this._ref = true;
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
unref() {
|
|
87
|
+
this._ref = false;
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
hasRef() {
|
|
91
|
+
return this._ref;
|
|
92
|
+
}
|
|
93
|
+
close() {
|
|
94
|
+
this._cancelled = true;
|
|
95
|
+
if (this._id != null) {
|
|
96
|
+
clearTimeout(this._id);
|
|
97
|
+
this._id = null;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
[Symbol.toPrimitive]() {
|
|
101
|
+
return this._id;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
//#endregion
|
|
106
|
+
export { Immediate, Timeout };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/timers",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.15",
|
|
4
4
|
"description": "Node.js timers module for Gjs",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "lib/esm/index.js",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"timers"
|
|
35
35
|
],
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@gjsify/cli": "^0.3.
|
|
38
|
-
"@gjsify/unit": "^0.3.
|
|
37
|
+
"@gjsify/cli": "^0.3.15",
|
|
38
|
+
"@gjsify/unit": "^0.3.15",
|
|
39
39
|
"@types/node": "^25.6.0",
|
|
40
40
|
"typescript": "^6.0.3"
|
|
41
41
|
}
|