timeout-flow 0.0.17 → 0.0.19
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/ADDITIONAL_TERMS.md +122 -0
- package/LICENSE +694 -0
- package/README.md +307 -237
- package/dist/timeout-flow.min.js +1 -6
- package/package.json +68 -58
- package/robots.txt +79 -3
- package/src/AfterTimer.js +82 -21
- package/src/EveryTimer.js +209 -88
- package/src/TimeoutFlow.js +400 -227
- package/src/TimerBase.js +133 -75
- package/src/abort.js +75 -0
- package/src/after.js +32 -83
- package/src/debounce.js +106 -40
- package/src/every.js +34 -120
- package/src/index.js +24 -19
- package/src/now.js +23 -0
- package/src/parseDuration.js +46 -44
- package/src/raf/ReadMe-Raf.md +94 -94
- package/src/raf/afterRaf.js +204 -72
- package/src/raf/debounceRaf.js +127 -45
- package/src/raf/everyRaf.js +317 -106
- package/src/raf/index.js +28 -8
- package/src/raf/throttleRaf.js +182 -38
- package/src/raf/waitForRaf.js +101 -33
- package/src/resolveDelayAndFn.js +90 -28
- package/src/retry.js +164 -24
- package/src/throttle.js +141 -47
- package/src/waitFor.js +77 -33
- package/types/AfterTimer.d.ts +8 -2
- package/types/EveryTimer.d.ts +21 -4
- package/types/TimeoutFlow.d.ts +6 -18
- package/types/TimerBase.d.ts +15 -4
- package/types/abort.d.ts +31 -0
- package/types/after.d.ts +22 -22
- package/types/debounce.d.ts +28 -6
- package/types/every.d.ts +20 -28
- package/types/index.d.ts +1 -1
- package/types/now.d.ts +1 -0
- package/types/raf/afterRaf.d.ts +35 -5
- package/types/raf/debounceRaf.d.ts +27 -6
- package/types/raf/everyRaf.d.ts +39 -9
- package/types/raf/index.d.ts +6 -5
- package/types/raf/throttleRaf.d.ts +33 -10
- package/types/raf/waitForRaf.d.ts +10 -4
- package/types/resolveDelayAndFn.d.ts +27 -0
- package/types/retry.d.ts +25 -8
- package/types/throttle.d.ts +28 -6
- package/types/waitFor.d.ts +11 -6
- package/LICENSE.MD +0 -285
package/package.json
CHANGED
|
@@ -1,59 +1,69 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "timeout-flow",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"type": "module",
|
|
5
|
-
"description": "Fluent, composable, pauseable JavaScript timers and time control flows — plus RAF utilities for frame-based logic.",
|
|
6
|
-
"keywords": [
|
|
7
|
-
"timeout",
|
|
8
|
-
"interval",
|
|
9
|
-
"flow",
|
|
10
|
-
"raf",
|
|
11
|
-
"animationframe",
|
|
12
|
-
"debounce",
|
|
13
|
-
"retry",
|
|
14
|
-
"javascript",
|
|
15
|
-
"timers",
|
|
16
|
-
"throttle",
|
|
17
|
-
"asynchronous",
|
|
18
|
-
"control-flow",
|
|
19
|
-
"utilities"
|
|
20
|
-
],
|
|
21
|
-
"author": "
|
|
22
|
-
"license": "
|
|
23
|
-
"repository": {
|
|
24
|
-
"type": "git",
|
|
25
|
-
"url": "git+https://github.com/iWhatty/TimeoutFlow-js.git"
|
|
26
|
-
},
|
|
27
|
-
"sideEffects": false,
|
|
28
|
-
"main": "./dist/timeout-flow.min.js",
|
|
29
|
-
"module": "./src/index.js",
|
|
30
|
-
"browser": "./dist/timeout-flow.min.js",
|
|
31
|
-
"exports": {
|
|
32
|
-
".": {
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"
|
|
43
|
-
"src/",
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "timeout-flow",
|
|
3
|
+
"version": "0.0.19",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Fluent, composable, pauseable JavaScript timers and time control flows — plus RAF utilities for frame-based logic.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"timeout",
|
|
8
|
+
"interval",
|
|
9
|
+
"flow",
|
|
10
|
+
"raf",
|
|
11
|
+
"animationframe",
|
|
12
|
+
"debounce",
|
|
13
|
+
"retry",
|
|
14
|
+
"javascript",
|
|
15
|
+
"timers",
|
|
16
|
+
"throttle",
|
|
17
|
+
"asynchronous",
|
|
18
|
+
"control-flow",
|
|
19
|
+
"utilities"
|
|
20
|
+
],
|
|
21
|
+
"author": "WATT3D <WATT3D@protonmail.com>",
|
|
22
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/iWhatty/TimeoutFlow-js.git"
|
|
26
|
+
},
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"main": "./dist/timeout-flow.min.js",
|
|
29
|
+
"module": "./src/index.js",
|
|
30
|
+
"browser": "./dist/timeout-flow.min.js",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./types/index.d.ts",
|
|
34
|
+
"import": "./src/index.js",
|
|
35
|
+
"require": "./dist/timeout-flow.min.js",
|
|
36
|
+
"default": "./src/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./after": "./src/after.js",
|
|
39
|
+
"./every": "./src/every.js",
|
|
40
|
+
"./debounce": "./src/debounce.js",
|
|
41
|
+
"./throttle": "./src/throttle.js",
|
|
42
|
+
"./retry": "./src/retry.js",
|
|
43
|
+
"./wait-for": "./src/waitFor.js",
|
|
44
|
+
"./flow": "./src/TimeoutFlow.js",
|
|
45
|
+
"./parse-duration": "./src/parseDuration.js",
|
|
46
|
+
"./raf": "./src/raf/index.js",
|
|
47
|
+
"./min": "./dist/timeout-flow.min.js"
|
|
48
|
+
},
|
|
49
|
+
"files": [
|
|
50
|
+
"dist/",
|
|
51
|
+
"src/",
|
|
52
|
+
"types/",
|
|
53
|
+
"robots.txt",
|
|
54
|
+
"ADDITIONAL_TERMS.md"
|
|
55
|
+
],
|
|
56
|
+
"types": "./types/index.d.ts",
|
|
57
|
+
"scripts": {
|
|
58
|
+
"dev": "serve testing",
|
|
59
|
+
"test:node": "node testing/node-harness.mjs",
|
|
60
|
+
"build": "node build.js",
|
|
61
|
+
"types": "tsc",
|
|
62
|
+
"prepub": "npm run build && npm run types"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"esbuild": "^0.25.3",
|
|
66
|
+
"serve": "^14.2.4",
|
|
67
|
+
"typescript": "^5.8.3"
|
|
68
|
+
}
|
|
59
69
|
}
|
package/robots.txt
CHANGED
|
@@ -1,3 +1,79 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# WATT3D — AI scraping policy
|
|
2
|
+
#
|
|
3
|
+
# This file enumerates known AI / LLM training and inference crawlers
|
|
4
|
+
# that the WATT3D AI Training Rider does not consent to. See
|
|
5
|
+
# ./ADDITIONAL_TERMS.md for the legal scope and definitions.
|
|
6
|
+
#
|
|
7
|
+
# Honoring robots.txt is voluntary. The rider applies whether or not
|
|
8
|
+
# a bot respects this file. The enumeration is non-exhaustive — bots
|
|
9
|
+
# not listed here are still bound by the rider if they perform
|
|
10
|
+
# Training Use as defined in ADDITIONAL_TERMS.md §1.
|
|
11
|
+
|
|
12
|
+
User-agent: GPTBot
|
|
13
|
+
Disallow: /
|
|
14
|
+
|
|
15
|
+
User-agent: ChatGPT-User
|
|
16
|
+
Disallow: /
|
|
17
|
+
|
|
18
|
+
User-agent: OAI-SearchBot
|
|
19
|
+
Disallow: /
|
|
20
|
+
|
|
21
|
+
User-agent: anthropic-ai
|
|
22
|
+
Disallow: /
|
|
23
|
+
|
|
24
|
+
User-agent: ClaudeBot
|
|
25
|
+
Disallow: /
|
|
26
|
+
|
|
27
|
+
User-agent: Claude-Web
|
|
28
|
+
Disallow: /
|
|
29
|
+
|
|
30
|
+
User-agent: Claude-SearchBot
|
|
31
|
+
Disallow: /
|
|
32
|
+
|
|
33
|
+
User-agent: Google-Extended
|
|
34
|
+
Disallow: /
|
|
35
|
+
|
|
36
|
+
User-agent: Applebot-Extended
|
|
37
|
+
Disallow: /
|
|
38
|
+
|
|
39
|
+
User-agent: Meta-ExternalAgent
|
|
40
|
+
Disallow: /
|
|
41
|
+
|
|
42
|
+
User-agent: Meta-ExternalFetcher
|
|
43
|
+
Disallow: /
|
|
44
|
+
|
|
45
|
+
User-agent: FacebookBot
|
|
46
|
+
Disallow: /
|
|
47
|
+
|
|
48
|
+
User-agent: CCBot
|
|
49
|
+
Disallow: /
|
|
50
|
+
|
|
51
|
+
User-agent: PerplexityBot
|
|
52
|
+
Disallow: /
|
|
53
|
+
|
|
54
|
+
User-agent: Perplexity-User
|
|
55
|
+
Disallow: /
|
|
56
|
+
|
|
57
|
+
User-agent: cohere-ai
|
|
58
|
+
Disallow: /
|
|
59
|
+
|
|
60
|
+
User-agent: Bytespider
|
|
61
|
+
Disallow: /
|
|
62
|
+
|
|
63
|
+
User-agent: Amazonbot
|
|
64
|
+
Disallow: /
|
|
65
|
+
|
|
66
|
+
User-agent: Omgilibot
|
|
67
|
+
Disallow: /
|
|
68
|
+
|
|
69
|
+
User-agent: AI2Bot
|
|
70
|
+
Disallow: /
|
|
71
|
+
|
|
72
|
+
User-agent: Diffbot
|
|
73
|
+
Disallow: /
|
|
74
|
+
|
|
75
|
+
# Everyone else: full access. Search engines and human-driven crawlers
|
|
76
|
+
# remain welcome.
|
|
77
|
+
|
|
78
|
+
User-agent: *
|
|
79
|
+
Allow: /
|
package/src/AfterTimer.js
CHANGED
|
@@ -1,21 +1,82 @@
|
|
|
1
|
-
// ./src/AfterTimer.js
|
|
2
|
-
import { TimerBase } from './TimerBase.js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
1
|
+
// ./src/AfterTimer.js
|
|
2
|
+
import { TimerBase } from './TimerBase.js';
|
|
3
|
+
import { attachAbort } from './abort.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Executes a function once after a delay.
|
|
7
|
+
*/
|
|
8
|
+
export class AfterTimer extends TimerBase {
|
|
9
|
+
/** @type {AbortSignal | null} */
|
|
10
|
+
#signal = null;
|
|
11
|
+
|
|
12
|
+
/** @type {(() => void) | null} */
|
|
13
|
+
#onAbort = null;
|
|
14
|
+
|
|
15
|
+
/** @type {() => void} */
|
|
16
|
+
#cleanupAbort = () => { };
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {string|number} duration - Delay before firing (e.g., '1s', 300)
|
|
20
|
+
* @param {Function} fn - Main function to execute
|
|
21
|
+
* @param {Object} [options]
|
|
22
|
+
* @param {Function} [options.onFinish] - Optional callback when done
|
|
23
|
+
* @param {AbortSignal} [options.signal] - Optional AbortSignal to auto-cancel
|
|
24
|
+
*/
|
|
25
|
+
constructor(duration, fn, { onFinish, signal } = {}) {
|
|
26
|
+
super(duration);
|
|
27
|
+
|
|
28
|
+
this.#signal = signal ?? null;
|
|
29
|
+
this.#onAbort = () => this.cancel();
|
|
30
|
+
|
|
31
|
+
this.#attachAbort();
|
|
32
|
+
|
|
33
|
+
// If already aborted, #attachAbort() will have canceled us.
|
|
34
|
+
if (this.isFinished) return;
|
|
35
|
+
|
|
36
|
+
this.resume(() => {
|
|
37
|
+
this.#cleanupAbortListener();
|
|
38
|
+
fn?.();
|
|
39
|
+
onFinish?.();
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
#attachAbort() {
|
|
44
|
+
if (!this.#signal || !this.#onAbort) return;
|
|
45
|
+
|
|
46
|
+
// If already aborted, terminate immediately.
|
|
47
|
+
if (this.#signal.aborted) {
|
|
48
|
+
super.cancel();
|
|
49
|
+
this.#cleanupAbortListener();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
this.#cleanupAbort = attachAbort(this.#signal, this.#onAbort);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
#cleanupAbortListener() {
|
|
57
|
+
this.#cleanupAbort();
|
|
58
|
+
this.#cleanupAbort = () => { };
|
|
59
|
+
|
|
60
|
+
// Keep signal reference so reset() can reattach.
|
|
61
|
+
this.#onAbort = null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
cancel() {
|
|
65
|
+
if (this.isFinished) return;
|
|
66
|
+
|
|
67
|
+
// Remove listener first to avoid keeping instance alive.
|
|
68
|
+
this.#cleanupAbortListener();
|
|
69
|
+
|
|
70
|
+
super.cancel();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
reset() {
|
|
74
|
+
// Preserve AbortSignal semantics.
|
|
75
|
+
this.#cleanupAbort();
|
|
76
|
+
this.#cleanupAbort = () => { };
|
|
77
|
+
this.#onAbort = () => this.cancel();
|
|
78
|
+
|
|
79
|
+
super.reset();
|
|
80
|
+
this.#attachAbort();
|
|
81
|
+
}
|
|
82
|
+
}
|
package/src/EveryTimer.js
CHANGED
|
@@ -1,88 +1,209 @@
|
|
|
1
|
-
// ./src/EveryTimer.js
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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
|
-
if (this.#
|
|
50
|
-
this
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
this
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
1
|
+
// ./src/EveryTimer.js
|
|
2
|
+
import { attachAbort } from './abort.js';
|
|
3
|
+
import { TimerBase } from './TimerBase.js';
|
|
4
|
+
import { parseDuration } from './parseDuration.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Repeatedly runs a function every N ms, with optional max count.
|
|
8
|
+
*/
|
|
9
|
+
export class EveryTimer {
|
|
10
|
+
#interval;
|
|
11
|
+
#fn;
|
|
12
|
+
#max;
|
|
13
|
+
|
|
14
|
+
#count = 0;
|
|
15
|
+
#finished = false;
|
|
16
|
+
|
|
17
|
+
/** @type {TimerBase | null} */
|
|
18
|
+
#controller = null;
|
|
19
|
+
|
|
20
|
+
/** @type {AbortSignal | null} */
|
|
21
|
+
#signal = null;
|
|
22
|
+
|
|
23
|
+
/** @type {() => void} */
|
|
24
|
+
#cleanupAbort = () => {};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {string|number} duration - Delay between calls (e.g., '1s', 200)
|
|
28
|
+
* @param {Function} fn - Function to call each tick
|
|
29
|
+
* @param {Object} [options]
|
|
30
|
+
* @param {number} [options.max=Infinity] - Max executions
|
|
31
|
+
* @param {boolean} [options.runImmediately=false] - Run `fn` once before first delay
|
|
32
|
+
* @param {AbortSignal} [options.signal] - Optional AbortSignal to auto-cancel
|
|
33
|
+
*/
|
|
34
|
+
constructor(duration, fn, { max = Infinity, runImmediately = false, signal } = {}) {
|
|
35
|
+
this.#interval = parseDuration(duration);
|
|
36
|
+
this.#fn = fn;
|
|
37
|
+
this.#max = max;
|
|
38
|
+
|
|
39
|
+
this.#signal = signal ?? null;
|
|
40
|
+
|
|
41
|
+
// If max is already satisfied, finish immediately.
|
|
42
|
+
if (!(this.#max > 0) && this.#max !== Infinity) {
|
|
43
|
+
this.#finished = true;
|
|
44
|
+
this.#cleanupAbort();
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Abort semantics: if already aborted, immediately finish (terminal).
|
|
49
|
+
if (this.#signal?.aborted) {
|
|
50
|
+
this.#finished = true;
|
|
51
|
+
this.#cleanupAbort();
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Attach abort listener for the lifetime of this timer.
|
|
56
|
+
// (Cleanup happens in #finish().)
|
|
57
|
+
this.#cleanupAbort = attachAbort(this.#signal, () => this.cancel());
|
|
58
|
+
|
|
59
|
+
if (runImmediately) {
|
|
60
|
+
this.#tick();
|
|
61
|
+
} else {
|
|
62
|
+
this.#schedule();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Number of times `fn` has been executed.
|
|
68
|
+
*/
|
|
69
|
+
get count() {
|
|
70
|
+
return this.#count;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Whether the timer is actively running (waiting for next tick).
|
|
75
|
+
*
|
|
76
|
+
* Note: during the brief moment between a tick firing and the next schedule,
|
|
77
|
+
* this may be false — which reflects "no countdown currently pending".
|
|
78
|
+
*/
|
|
79
|
+
get isRunning() {
|
|
80
|
+
return !this.#finished && !!this.#controller && this.#controller.isRunning;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Whether the timer is paused (resumable).
|
|
85
|
+
*/
|
|
86
|
+
get isPaused() {
|
|
87
|
+
return !this.#finished && !!this.#controller && this.#controller.isPaused;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Whether the timer has finished (hit max) or been canceled (terminal).
|
|
92
|
+
*/
|
|
93
|
+
get isFinished() {
|
|
94
|
+
return this.#finished;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
#finish = () => {
|
|
98
|
+
this.#controller?.cancel();
|
|
99
|
+
this.#controller = null;
|
|
100
|
+
|
|
101
|
+
this.#finished = true;
|
|
102
|
+
|
|
103
|
+
this.#cleanupAbort();
|
|
104
|
+
this.#cleanupAbort = () => {};
|
|
105
|
+
this.#signal = null;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
#tick = () => {
|
|
109
|
+
if (this.#finished) return;
|
|
110
|
+
|
|
111
|
+
if (this.#signal?.aborted) {
|
|
112
|
+
this.#finish();
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (this.#count >= this.#max) {
|
|
117
|
+
this.#finish();
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
this.#fn?.(this.#count++);
|
|
122
|
+
if (this.#count >= this.#max) {
|
|
123
|
+
this.#finish();
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
this.#schedule();
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
#schedule() {
|
|
131
|
+
if (this.#finished) return;
|
|
132
|
+
|
|
133
|
+
if (this.#signal?.aborted) {
|
|
134
|
+
this.#finish();
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Fresh controller per interval window
|
|
139
|
+
this.#controller = new TimerBase(this.#interval);
|
|
140
|
+
this.#controller.resume(this.#tick);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
pause() {
|
|
144
|
+
if (this.#finished) return;
|
|
145
|
+
this.#controller?.pause();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
resume() {
|
|
149
|
+
if (this.#finished) return;
|
|
150
|
+
|
|
151
|
+
if (this.#signal?.aborted) {
|
|
152
|
+
this.#finish();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (this.#count >= this.#max) {
|
|
157
|
+
this.#finish();
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Resume existing countdown if paused, otherwise schedule fresh.
|
|
162
|
+
if (this.#controller) {
|
|
163
|
+
if (this.#controller.isPaused) {
|
|
164
|
+
this.#controller.resume(this.#tick);
|
|
165
|
+
} else if (!this.#controller.isRunning) {
|
|
166
|
+
// Controller exists but isn't active (edge); schedule next window
|
|
167
|
+
this.#schedule();
|
|
168
|
+
}
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
this.#schedule();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
cancel() {
|
|
176
|
+
if (this.#finished) return;
|
|
177
|
+
this.#finish();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
reset(restart = false) {
|
|
181
|
+
// Reset is not terminal; keep signal semantics:
|
|
182
|
+
// - If signal exists and is aborted, reset should leave the timer finished.
|
|
183
|
+
this.#controller?.cancel();
|
|
184
|
+
this.#controller = null;
|
|
185
|
+
|
|
186
|
+
this.#count = 0;
|
|
187
|
+
this.#finished = false;
|
|
188
|
+
|
|
189
|
+
if (this.#signal?.aborted) {
|
|
190
|
+
this.#finished = true;
|
|
191
|
+
this.#cleanupAbort();
|
|
192
|
+
this.#cleanupAbort = () => {};
|
|
193
|
+
this.#signal = null;
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (restart) {
|
|
198
|
+
// If max is zero-ish, immediately finish (consistent with constructor)
|
|
199
|
+
if (!(this.#max > 0) && this.#max !== Infinity) {
|
|
200
|
+
this.#finished = true;
|
|
201
|
+
this.#cleanupAbort();
|
|
202
|
+
this.#cleanupAbort = () => {};
|
|
203
|
+
this.#signal = null;
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
this.#schedule();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|