timeout-flow 0.0.1
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/LICENSE +138 -0
- package/ReadMe.md +146 -0
- package/dist/timeout-flow.min.js +2 -0
- package/package.json +46 -0
- package/raf/ReadMe-Raf.md +94 -0
- package/raf/afterRaf.js +72 -0
- package/raf/debounceRaf.js +45 -0
- package/raf/everyRaf.js +106 -0
- package/raf/index.js +8 -0
- package/raf/throttleRaf.js +38 -0
- package/raf/waitForRaf.js +33 -0
- package/src/AfterTimer.js +21 -0
- package/src/EveryTimer.js +88 -0
- package/src/TimeoutFlow.js +228 -0
- package/src/TimerBase.js +98 -0
- package/src/after.js +84 -0
- package/src/debounce.js +17 -0
- package/src/every.js +121 -0
- package/src/index.js +19 -0
- package/src/parseDuration.js +37 -0
- package/src/retry.js +24 -0
- package/src/throttle.js +25 -0
- package/src/waitFor.js +28 -0
package/src/every.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
|
|
2
|
+
// ./src/every.js
|
|
3
|
+
import { parseDuration } from './parseDuration.js';
|
|
4
|
+
|
|
5
|
+
import { EveryTimer } from './EveryTimer.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Runs a function every N ms with optional execution limit.
|
|
9
|
+
*
|
|
10
|
+
* @param {string|number} duration - e.g. '500ms', 1000
|
|
11
|
+
* @param {Function} fn - Function to execute
|
|
12
|
+
* @param {number} [max=Infinity] - Max times to run
|
|
13
|
+
* @param {boolean} [runImmediately=false] - Run immediately on first tick
|
|
14
|
+
* @returns {{
|
|
15
|
+
* pause(): void,
|
|
16
|
+
* resume(): void,
|
|
17
|
+
* cancel(): void,
|
|
18
|
+
* reset(restart?: boolean): void,
|
|
19
|
+
* readonly isRunning: boolean,
|
|
20
|
+
* readonly count: number
|
|
21
|
+
* }}
|
|
22
|
+
*/
|
|
23
|
+
export function every(duration, fn, max = Infinity, runImmediately = false) {
|
|
24
|
+
return new EveryTimer(duration, fn, max, runImmediately);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Runs a function every N milliseconds with optional execution limit.
|
|
31
|
+
* Returns control methods: pause, resume, cancel, reset, and status flags.
|
|
32
|
+
*
|
|
33
|
+
* @param {string|number} duration - e.g. "1s", "500ms", "2m" or raw ms
|
|
34
|
+
* @param {Function} fn - function to execute
|
|
35
|
+
* @param {number} [maxTimes] - optional number of times to run
|
|
36
|
+
* @returns {{
|
|
37
|
+
* pause(): void,
|
|
38
|
+
* resume(): void,
|
|
39
|
+
* cancel(): void,
|
|
40
|
+
* reset(restart?: boolean): void,
|
|
41
|
+
* readonly isRunning: boolean,
|
|
42
|
+
* readonly count: number
|
|
43
|
+
* }}
|
|
44
|
+
*/
|
|
45
|
+
export function _every(duration, fn, max = Infinity, runImmediately = false) {
|
|
46
|
+
const ms = parseDuration(duration);
|
|
47
|
+
let startTime = null;
|
|
48
|
+
let remaining = ms;
|
|
49
|
+
let running = false;
|
|
50
|
+
let timer = null;
|
|
51
|
+
|
|
52
|
+
let count = 0;
|
|
53
|
+
|
|
54
|
+
const tick = () => {
|
|
55
|
+
if (count >= max) {
|
|
56
|
+
cancel();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
fn?.(); // Main Func to exec after delay.
|
|
60
|
+
count++
|
|
61
|
+
scheduleNext(); // keep going
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const scheduleNext = () => {
|
|
65
|
+
timer = setTimeout(tick, ms);
|
|
66
|
+
startTime = Date.now();
|
|
67
|
+
remaining = ms;
|
|
68
|
+
running = true;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const pause = () => {
|
|
72
|
+
if (running) {
|
|
73
|
+
clearTimeout(timer);
|
|
74
|
+
remaining -= Date.now() - startTime;
|
|
75
|
+
running = false;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const resume = () => {
|
|
80
|
+
if (!running && count < max && remaining > 0) {
|
|
81
|
+
timer = setTimeout(tick, remaining);
|
|
82
|
+
startTime = Date.now();
|
|
83
|
+
running = true;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const cancel = () => {
|
|
88
|
+
clearTimeout(timer);
|
|
89
|
+
running = false;
|
|
90
|
+
timer = null;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const reset = (restart = false) => {
|
|
94
|
+
cancel();
|
|
95
|
+
count = 0;
|
|
96
|
+
remaining = ms;
|
|
97
|
+
if (restart) start();
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const start = () => {
|
|
101
|
+
if (runImmediately) {
|
|
102
|
+
tick();
|
|
103
|
+
} else {
|
|
104
|
+
scheduleNext();
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
// Initial launch
|
|
110
|
+
start();
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
pause,
|
|
114
|
+
resume,
|
|
115
|
+
cancel,
|
|
116
|
+
reset,
|
|
117
|
+
get isRunning() { return running; },
|
|
118
|
+
get count() { return count; }
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// ./src/index.js
|
|
2
|
+
|
|
3
|
+
export { parseDuration } from './parseDuration.js';
|
|
4
|
+
|
|
5
|
+
export { flow } from './TimeoutFlow.js';
|
|
6
|
+
|
|
7
|
+
export { throttle } from './throttle.js';
|
|
8
|
+
export { debounce } from './debounce.js';
|
|
9
|
+
|
|
10
|
+
export { after } from './after.js';
|
|
11
|
+
export { every } from './every.js';
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
export { waitFor } from './waitFor.js';
|
|
15
|
+
export { retry } from './retry.js';
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
// Add this line:
|
|
19
|
+
export * from '../raf/index.js';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// ./src/parseDuration.js
|
|
2
|
+
|
|
3
|
+
const timeMultipliers = {
|
|
4
|
+
ms: 1,
|
|
5
|
+
s: 1000,
|
|
6
|
+
m: 60_000,
|
|
7
|
+
h: 3_600_000,
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Parses a duration string like "2s", "1.5m", "500ms" into milliseconds.
|
|
12
|
+
* Also accepts raw number values (treated as ms).
|
|
13
|
+
* @param {string|number} input
|
|
14
|
+
* @returns {number}
|
|
15
|
+
*/
|
|
16
|
+
export function parseDuration(input) {
|
|
17
|
+
if (typeof input === 'number' && Number.isFinite(input)) {
|
|
18
|
+
return input;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (typeof input !== 'string') {
|
|
22
|
+
throw new TypeError(`Expected a duration string or number, got ${typeof input}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
input = input.toLowerCase().trim();
|
|
26
|
+
|
|
27
|
+
const match = input.match(/^(\d+(?:\.\d+)?)\s*(ms|s|m|h)$/);
|
|
28
|
+
|
|
29
|
+
if (!match) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
`Invalid duration format: "${input}". Expected formats like "500ms", "2s", "1.5m", or "1h".`
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const [, value, unit] = match;
|
|
36
|
+
return parseFloat(value) * timeMultipliers[unit];
|
|
37
|
+
}
|
package/src/retry.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { parseDuration } from './parseDuration.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Retries a promise-returning function with delays.
|
|
5
|
+
* @param {Function} fn - async function
|
|
6
|
+
* @param {Object} options
|
|
7
|
+
* @param {number} [options.attempts=3]
|
|
8
|
+
* @param {string} [options.delay='500ms']
|
|
9
|
+
* @param {boolean} [options.backoff=false] - use exponential backoff
|
|
10
|
+
* @returns {Promise}
|
|
11
|
+
*/
|
|
12
|
+
export async function retry(fn, { attempts = 3, delay = '500ms', backoff = false } = {}) {
|
|
13
|
+
const baseDelay = parseDuration(delay);
|
|
14
|
+
|
|
15
|
+
for (let i = 0; i < attempts; i++) {
|
|
16
|
+
try {
|
|
17
|
+
return await fn();
|
|
18
|
+
} catch (err) {
|
|
19
|
+
if (i === attempts - 1) throw err;
|
|
20
|
+
const wait = backoff ? baseDelay * Math.pow(2, i) : baseDelay;
|
|
21
|
+
await new Promise(resolve => setTimeout(resolve, wait));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/throttle.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { parseDuration } from './parseDuration.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Throttles a function to run at most once per given duration.
|
|
5
|
+
* @param {string} duration - e.g. '1s', '500ms'
|
|
6
|
+
* @param {Function} fn - the function to throttle
|
|
7
|
+
* @returns {Function} throttled function
|
|
8
|
+
*/
|
|
9
|
+
export function throttle(duration, fn, { leading = true } = {}) {
|
|
10
|
+
const limit = parseDuration(duration);
|
|
11
|
+
let lastTime = 0;
|
|
12
|
+
|
|
13
|
+
return (...args) => {
|
|
14
|
+
const now = Date.now();
|
|
15
|
+
if (leading && lastTime === 0) {
|
|
16
|
+
lastTime = now;
|
|
17
|
+
return fn(...args);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (now - lastTime >= limit) {
|
|
21
|
+
lastTime = now;
|
|
22
|
+
fn(...args);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
package/src/waitFor.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Waits for a condition to become true, polling at intervals.
|
|
3
|
+
* @param {Function} condition - returns truthy when satisfied
|
|
4
|
+
* @param {Object} options - optional polling control
|
|
5
|
+
* @param {string} [options.interval='250ms']
|
|
6
|
+
* @param {string} [options.timeout] - max wait time
|
|
7
|
+
* @returns {Promise<void>}
|
|
8
|
+
*/
|
|
9
|
+
export function waitFor(condition, { interval = '250ms', timeout } = {}) {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
const start = Date.now();
|
|
12
|
+
const intervalMs = typeof interval === 'string' ? parseDuration(interval) : interval;
|
|
13
|
+
const timeoutMs = timeout ? parseDuration(timeout) : null;
|
|
14
|
+
|
|
15
|
+
const check = () => {
|
|
16
|
+
if (condition()) {
|
|
17
|
+
clearInterval(poller);
|
|
18
|
+
resolve();
|
|
19
|
+
} else if (timeoutMs && Date.now() - start >= timeoutMs) {
|
|
20
|
+
clearInterval(poller);
|
|
21
|
+
reject(new Error('waitFor timed out'));
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const poller = setInterval(check, intervalMs);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|