timeout-flow 0.0.16 → 0.0.18
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/README.md +239 -144
- package/dist/timeout-flow.min.js +1 -6
- package/package.json +2 -1
- package/src/AfterTimer.js +64 -3
- package/src/EveryTimer.js +139 -18
- package/src/TimeoutFlow.js +291 -118
- package/src/TimerBase.js +81 -23
- package/src/abort.js +75 -0
- package/src/after.js +20 -71
- package/src/context-js.bat +58 -0
- package/src/context.txt +2604 -0
- package/src/debounce.js +90 -24
- package/src/every.js +19 -105
- package/src/index.js +7 -2
- package/src/now.js +23 -0
- package/src/parseDuration.js +22 -20
- package/src/raf/afterRaf.js +198 -66
- package/src/raf/debounceRaf.js +108 -26
- package/src/raf/everyRaf.js +278 -67
- package/src/raf/index.js +26 -6
- package/src/raf/throttleRaf.js +175 -31
- package/src/raf/waitForRaf.js +82 -14
- package/src/resolveDelayAndFn.js +69 -7
- package/src/retry.js +152 -12
- package/src/throttle.js +123 -24
- package/src/waitFor.js +68 -24
- 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 +33 -5
- package/types/waitFor.d.ts +11 -6
package/src/abort.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// ./src/abort.js
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Standardized AbortError factory.
|
|
8
|
+
* Works in browsers and Node runtimes that may not have DOMException.
|
|
9
|
+
*
|
|
10
|
+
* We resolve the best implementation once at module load time.
|
|
11
|
+
* This avoids feature detection in the hot path.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
let createAbortError;
|
|
15
|
+
|
|
16
|
+
// Prefer native DOMException when available (browser + modern Node).
|
|
17
|
+
if (typeof DOMException !== 'undefined') {
|
|
18
|
+
createAbortError = (message = 'Aborted') =>
|
|
19
|
+
new DOMException(message, 'AbortError');
|
|
20
|
+
} else {
|
|
21
|
+
// Fallback for runtimes without DOMException.
|
|
22
|
+
createAbortError = (message = 'Aborted') => {
|
|
23
|
+
const err = new Error(message);
|
|
24
|
+
err.name = 'AbortError';
|
|
25
|
+
return err;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { createAbortError };
|
|
30
|
+
/**
|
|
31
|
+
* Attach an AbortSignal listener and return a cleanup function.
|
|
32
|
+
* - If `signal` is missing, returns a no-op cleanup.
|
|
33
|
+
* - If already aborted, calls `onAbort()` immediately and returns a no-op cleanup.
|
|
34
|
+
*
|
|
35
|
+
* @param {AbortSignal | null | undefined} signal
|
|
36
|
+
* @param {() => void} onAbort
|
|
37
|
+
* @returns {() => void} cleanup
|
|
38
|
+
*/
|
|
39
|
+
export function attachAbort(signal, onAbort) {
|
|
40
|
+
if (!signal) return () => { };
|
|
41
|
+
if (signal.aborted) {
|
|
42
|
+
onAbort();
|
|
43
|
+
return () => { };
|
|
44
|
+
}
|
|
45
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
46
|
+
return () => signal.removeEventListener('abort', onAbort);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Manage AbortSignal listener lifecycle for "pending work" cases.
|
|
51
|
+
* Call `add()` when work becomes pending and `remove()` when it’s resolved/canceled.
|
|
52
|
+
*
|
|
53
|
+
* @param {AbortSignal | null | undefined} signal
|
|
54
|
+
* @param {() => void} onAbort
|
|
55
|
+
* @returns {{ add: () => void, remove: () => void, reset: () => void }}
|
|
56
|
+
*/
|
|
57
|
+
export function pendingAbort(signal, onAbort) {
|
|
58
|
+
let cleanup = null;
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
add() {
|
|
62
|
+
if (cleanup) return;
|
|
63
|
+
cleanup = attachAbort(signal, onAbort);
|
|
64
|
+
},
|
|
65
|
+
remove() {
|
|
66
|
+
if (!cleanup) return;
|
|
67
|
+
cleanup();
|
|
68
|
+
cleanup = null;
|
|
69
|
+
},
|
|
70
|
+
reset() {
|
|
71
|
+
if (cleanup) cleanup();
|
|
72
|
+
cleanup = null;
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
package/src/after.js
CHANGED
|
@@ -1,84 +1,33 @@
|
|
|
1
1
|
// ./src/after.js
|
|
2
|
-
import { parseDuration } from './parseDuration.js';
|
|
3
|
-
|
|
4
2
|
import { AfterTimer } from './AfterTimer.js';
|
|
3
|
+
import { resolveDelayFnOptions } from './resolveDelayAndFn.js';
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* Schedules a one-time delayed function.
|
|
8
7
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* }
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return new AfterTimer(duration, fn, onFinish);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Schedules a one-time delayed function.
|
|
26
|
-
* @param {string} duration - e.g. "5s", "500ms", "2m"
|
|
27
|
-
* @param {Function} fn - callback to run
|
|
8
|
+
* Preferred:
|
|
9
|
+
* - after(fn, duration, [options])
|
|
10
|
+
*
|
|
11
|
+
* Also supported (legacy/alt):
|
|
12
|
+
* - after(duration, fn, [options])
|
|
13
|
+
*
|
|
14
|
+
* @param {Function|string|number} a - Function or duration (see overloads)
|
|
15
|
+
* @param {Function|string|number|Object} b - Duration, function, or options
|
|
16
|
+
* @param {Object} [c] - Options
|
|
17
|
+
* @param {Function} [c.onFinish] - Optional callback after `fn` runs
|
|
18
|
+
* @param {AbortSignal} [c.signal] - Optional AbortSignal to auto-cancel
|
|
28
19
|
* @returns {{
|
|
29
20
|
* pause(): void,
|
|
30
21
|
* resume(): void,
|
|
31
22
|
* cancel(): void,
|
|
23
|
+
* reset(): void,
|
|
32
24
|
* readonly isRunning: boolean,
|
|
25
|
+
* readonly isPaused: boolean,
|
|
26
|
+
* readonly isFinished: boolean
|
|
33
27
|
* }}
|
|
34
|
-
|
|
35
|
-
export function
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
let running = true;
|
|
40
|
-
let timer = null;
|
|
41
|
-
|
|
42
|
-
const schedule = () => {
|
|
43
|
-
startTime = Date.now();
|
|
44
|
-
timer = setTimeout(() => {
|
|
45
|
-
running = false;
|
|
46
|
-
fn?.(); // Main Func to exec after delay.
|
|
47
|
-
onFinish?.(); // Callback after running, if passed.
|
|
48
|
-
}, remaining);
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const pause = () => {
|
|
53
|
-
if (running) {
|
|
54
|
-
clearTimeout(timer);
|
|
55
|
-
remaining -= Date.now() - startTime;
|
|
56
|
-
running = false;
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const resume = () => {
|
|
61
|
-
if (!running && remaining > 0) {
|
|
62
|
-
startTime = Date.now();
|
|
63
|
-
running = true;
|
|
64
|
-
schedule();
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const cancel = () => {
|
|
69
|
-
clearTimeout(timer);
|
|
70
|
-
running = false;
|
|
71
|
-
timer = null;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
// Scedule Timer for execution.
|
|
75
|
-
schedule();
|
|
76
|
-
|
|
77
|
-
return {
|
|
78
|
-
pause,
|
|
79
|
-
resume,
|
|
80
|
-
cancel,
|
|
81
|
-
get isRunning() { return running; }
|
|
82
|
-
};
|
|
83
|
-
|
|
28
|
+
*/
|
|
29
|
+
export function after(a, b, c) {
|
|
30
|
+
// require explicit duration: defaultDelay = undefined
|
|
31
|
+
const { fn, delay, options } = resolveDelayFnOptions(a, b, c, undefined);
|
|
32
|
+
return new AfterTimer(delay, fn, options);
|
|
84
33
|
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
@echo off
|
|
2
|
+
setlocal EnableExtensions EnableDelayedExpansion
|
|
3
|
+
|
|
4
|
+
REM ===================== CONFIG =====================
|
|
5
|
+
set "EXT=.js"
|
|
6
|
+
set "OUT=context.txt"
|
|
7
|
+
REM ==================================================
|
|
8
|
+
|
|
9
|
+
REM Switch to UTF-8 (optional but nice for code)
|
|
10
|
+
chcp 65001 >nul
|
|
11
|
+
|
|
12
|
+
REM Root directory (absolute)
|
|
13
|
+
set "ROOTDIR=%CD%"
|
|
14
|
+
|
|
15
|
+
REM Clear old output if it exists
|
|
16
|
+
if exist "%OUT%" del "%OUT%"
|
|
17
|
+
|
|
18
|
+
REM Count matching files (recursive)
|
|
19
|
+
set "COUNT=0"
|
|
20
|
+
for /r "%ROOTDIR%" %%F in (*%EXT%) do set /a COUNT+=1
|
|
21
|
+
|
|
22
|
+
if "%COUNT%"=="0" (
|
|
23
|
+
echo No files with extension "%EXT%" found under "%ROOTDIR%".
|
|
24
|
+
goto :eof
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
echo Building "%OUT%" from %COUNT% file(s) with extension "%EXT%"...
|
|
28
|
+
echo.
|
|
29
|
+
|
|
30
|
+
REM Preamble
|
|
31
|
+
>>"%OUT%" echo ===== Context dump for folder: %ROOTDIR% =====
|
|
32
|
+
>>"%OUT%" echo Extension: %EXT%
|
|
33
|
+
>>"%OUT%" echo Generated on: %DATE% %TIME%
|
|
34
|
+
>>"%OUT%" echo.
|
|
35
|
+
|
|
36
|
+
REM Iterate recursively in a stable sorted order via DIR
|
|
37
|
+
for /f "delims=" %%F in ('dir /b /s /a-d "*%EXT%"') do (
|
|
38
|
+
REM Compute relative path
|
|
39
|
+
set "FULL=%%F"
|
|
40
|
+
set "REL=!FULL:%ROOTDIR%\=!"
|
|
41
|
+
|
|
42
|
+
echo Adding !REL!
|
|
43
|
+
|
|
44
|
+
>>"%OUT%" echo.
|
|
45
|
+
>>"%OUT%" echo ===== FILE: !REL! =====
|
|
46
|
+
>>"%OUT%" echo.
|
|
47
|
+
|
|
48
|
+
REM Concatenate file contents
|
|
49
|
+
type "%%F" >>"%OUT%"
|
|
50
|
+
|
|
51
|
+
>>"%OUT%" echo.
|
|
52
|
+
>>"%OUT%" echo ===== END FILE: !REL! =====
|
|
53
|
+
>>"%OUT%" echo.
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
echo.
|
|
57
|
+
echo Done. Output: "%OUT%"
|
|
58
|
+
endlocal
|