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.
Files changed (49) hide show
  1. package/ADDITIONAL_TERMS.md +122 -0
  2. package/LICENSE +694 -0
  3. package/README.md +307 -237
  4. package/dist/timeout-flow.min.js +1 -6
  5. package/package.json +68 -58
  6. package/robots.txt +79 -3
  7. package/src/AfterTimer.js +82 -21
  8. package/src/EveryTimer.js +209 -88
  9. package/src/TimeoutFlow.js +400 -227
  10. package/src/TimerBase.js +133 -75
  11. package/src/abort.js +75 -0
  12. package/src/after.js +32 -83
  13. package/src/debounce.js +106 -40
  14. package/src/every.js +34 -120
  15. package/src/index.js +24 -19
  16. package/src/now.js +23 -0
  17. package/src/parseDuration.js +46 -44
  18. package/src/raf/ReadMe-Raf.md +94 -94
  19. package/src/raf/afterRaf.js +204 -72
  20. package/src/raf/debounceRaf.js +127 -45
  21. package/src/raf/everyRaf.js +317 -106
  22. package/src/raf/index.js +28 -8
  23. package/src/raf/throttleRaf.js +182 -38
  24. package/src/raf/waitForRaf.js +101 -33
  25. package/src/resolveDelayAndFn.js +90 -28
  26. package/src/retry.js +164 -24
  27. package/src/throttle.js +141 -47
  28. package/src/waitFor.js +77 -33
  29. package/types/AfterTimer.d.ts +8 -2
  30. package/types/EveryTimer.d.ts +21 -4
  31. package/types/TimeoutFlow.d.ts +6 -18
  32. package/types/TimerBase.d.ts +15 -4
  33. package/types/abort.d.ts +31 -0
  34. package/types/after.d.ts +22 -22
  35. package/types/debounce.d.ts +28 -6
  36. package/types/every.d.ts +20 -28
  37. package/types/index.d.ts +1 -1
  38. package/types/now.d.ts +1 -0
  39. package/types/raf/afterRaf.d.ts +35 -5
  40. package/types/raf/debounceRaf.d.ts +27 -6
  41. package/types/raf/everyRaf.d.ts +39 -9
  42. package/types/raf/index.d.ts +6 -5
  43. package/types/raf/throttleRaf.d.ts +33 -10
  44. package/types/raf/waitForRaf.d.ts +10 -4
  45. package/types/resolveDelayAndFn.d.ts +27 -0
  46. package/types/retry.d.ts +25 -8
  47. package/types/throttle.d.ts +28 -6
  48. package/types/waitFor.d.ts +11 -6
  49. package/LICENSE.MD +0 -285
package/package.json CHANGED
@@ -1,59 +1,69 @@
1
- {
2
- "name": "timeout-flow",
3
- "version": "0.0.17",
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": "DR.WATT",
22
- "license": "DR.WATT License v3.0",
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
- "browser": {
34
- "import": "./dist/timeout-flow.min.js",
35
- "require": "./dist/timeout-flow.min.js"
36
- },
37
- "import": "./src/index.js",
38
- "require": "./dist/timeout-flow.min.js"
39
- }
40
- },
41
- "files": [
42
- "dist/",
43
- "src/",
44
- "types/",
45
- "robots.txt"
46
- ],
47
- "types": "./types/index.d.ts",
48
- "scripts": {
49
- "dev": "serve testing",
50
- "build": "node build.js",
51
- "types": "tsc",
52
- "prepub": "npm run build && npm run types"
53
- },
54
- "devDependencies": {
55
- "esbuild": "^0.25.3",
56
- "serve": "^14.2.4",
57
- "typescript": "^5.8.3"
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
- User-agent: *
2
- Disallow: /
3
- License: Dr.Watt v3.0
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
- * Executes a function once after a delay.
6
- */
7
- export class AfterTimer extends TimerBase {
8
- /**
9
- * @param {string|number} duration - Delay before firing (e.g., '1s', 300)
10
- * @param {Function} fn - Main function to execute
11
- * @param {Function} [onFinish] - Optional callback when done
12
- */
13
- constructor(duration, fn, onFinish) {
14
- super(duration);
15
- this.resume(() => {
16
- fn?.();
17
- onFinish?.();
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 { TimerBase } from './TimerBase.js';
3
- import { parseDuration } from './parseDuration.js';
4
-
5
- /**
6
- * Repeatedly runs a function every N ms, with optional max count.
7
- */
8
- export class EveryTimer {
9
- #interval;
10
- #fn;
11
- #max;
12
- #count = 0;
13
- #running = false;
14
- #controller = null;
15
-
16
- /**
17
- * @param {string|number} duration - Delay between calls (e.g., '1s', 200)
18
- * @param {Function} fn - Function to call each tick
19
- * @param {number} [max=Infinity] - Max executions
20
- * @param {boolean} [runImmediately=false] - Run `fn` once before first delay
21
- */
22
- constructor(duration, fn, max = Infinity, runImmediately = false) {
23
- this.#interval = parseDuration(duration);
24
- this.#fn = fn;
25
- this.#max = max;
26
-
27
- if (runImmediately) {
28
- this.#tick();
29
- } else {
30
- this.#schedule();
31
- }
32
- }
33
-
34
- /**
35
- * Number of times `fn` has been executed.
36
- */
37
- get count() {
38
- return this.#count;
39
- }
40
-
41
- /**
42
- * Whether the timer is actively running.
43
- */
44
- get isRunning() {
45
- return this.#running;
46
- }
47
-
48
- #tick = () => {
49
- if (this.#count >= this.#max) {
50
- this.cancel();
51
- return;
52
- }
53
-
54
- this.#fn?.(this.#count++);
55
- this.#schedule();
56
- };
57
-
58
- #schedule() {
59
- this.#controller = new TimerBase(this.#interval);
60
- this.#running = true;
61
- this.#controller.resume(this.#tick);
62
- }
63
-
64
- pause() {
65
- this.#controller?.pause();
66
- this.#running = false;
67
- }
68
-
69
- resume() {
70
- if (!this.#running && this.#count < this.#max) {
71
- this.#controller?.resume(this.#tick);
72
- this.#running = true;
73
- }
74
- }
75
-
76
- cancel() {
77
- this.#controller?.cancel();
78
- this.#running = false;
79
- }
80
-
81
- reset(restart = false) {
82
- this.cancel();
83
- this.#count = 0;
84
- if (restart) this.#schedule();
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
+ }