katanakit-js 2.2.1 → 2.12.9
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 +72 -437
- package/dist/adapters/astro/astro.service.d.ts +103 -15
- package/dist/adapters/astro/astro.service.d.ts.map +1 -1
- package/dist/adapters/astro/astro.service.js +145 -75
- package/dist/adapters/astro/astro.service.js.map +1 -1
- package/dist/adapters/astro/index.d.ts +1 -1
- package/dist/adapters/astro/index.d.ts.map +1 -1
- package/dist/adapters/astro/index.js +1 -1
- package/dist/adapters/astro/index.js.map +1 -1
- package/dist/adapters/astro/rss.service.d.ts +85 -105
- package/dist/adapters/astro/rss.service.d.ts.map +1 -1
- package/dist/adapters/astro/rss.service.js +172 -168
- package/dist/adapters/astro/rss.service.js.map +1 -1
- package/dist/adapters/astro/seo.service.d.ts +10 -0
- package/dist/adapters/astro/seo.service.d.ts.map +1 -0
- package/dist/adapters/astro/seo.service.js +3 -0
- package/dist/adapters/astro/seo.service.js.map +1 -0
- package/dist/adapters/express/app.d.ts +30 -1
- package/dist/adapters/express/app.d.ts.map +1 -1
- package/dist/adapters/express/app.js +31 -3
- package/dist/adapters/express/app.js.map +1 -1
- package/dist/adapters/express/index.d.ts +2 -2
- package/dist/adapters/express/index.d.ts.map +1 -1
- package/dist/adapters/express/index.js +2 -2
- package/dist/adapters/express/index.js.map +1 -1
- package/dist/adapters/express/main.js +2 -2
- package/dist/adapters/express/main.js.map +1 -1
- package/dist/adapters/express/products.controller.d.ts +67 -8
- package/dist/adapters/express/products.controller.d.ts.map +1 -1
- package/dist/adapters/express/products.controller.js +105 -49
- package/dist/adapters/express/products.controller.js.map +1 -1
- package/dist/adapters/express/server.d.ts +37 -13
- package/dist/adapters/express/server.d.ts.map +1 -1
- package/dist/adapters/express/server.js +118 -53
- package/dist/adapters/express/server.js.map +1 -1
- package/dist/adapters/nuxt/nuxt.service.d.ts +8 -6
- package/dist/adapters/nuxt/nuxt.service.d.ts.map +1 -1
- package/dist/adapters/nuxt/nuxt.service.js +34 -10
- package/dist/adapters/nuxt/nuxt.service.js.map +1 -1
- package/dist/adapters/vue/vue.service.d.ts +5 -2
- package/dist/adapters/vue/vue.service.d.ts.map +1 -1
- package/dist/adapters/vue/vue.service.js +41 -7
- package/dist/adapters/vue/vue.service.js.map +1 -1
- package/dist/config/index.d.ts +2 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +1 -1
- package/dist/config/index.js.map +1 -1
- package/dist/config/seo-meta.types.d.ts +177 -0
- package/dist/config/seo-meta.types.d.ts.map +1 -0
- package/dist/config/seo-meta.types.js +11 -0
- package/dist/config/seo-meta.types.js.map +1 -0
- package/dist/config/seo.service.d.ts +41 -58
- package/dist/config/seo.service.d.ts.map +1 -1
- package/dist/config/seo.service.js +468 -136
- package/dist/config/seo.service.js.map +1 -1
- package/dist/config/site.config.d.ts +10 -50
- package/dist/config/site.config.d.ts.map +1 -1
- package/dist/config/site.config.js +8 -36
- package/dist/config/site.config.js.map +1 -1
- package/dist/core/index.d.ts +1 -3
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +1 -3
- package/dist/core/index.js.map +1 -1
- package/dist/core/services/dates.service.d.ts +149 -22
- package/dist/core/services/dates.service.d.ts.map +1 -1
- package/dist/core/services/dates.service.js +201 -54
- package/dist/core/services/dates.service.js.map +1 -1
- package/dist/core/services/error.service.d.ts +32 -20
- package/dist/core/services/error.service.d.ts.map +1 -1
- package/dist/core/services/error.service.js +44 -32
- package/dist/core/services/error.service.js.map +1 -1
- package/dist/core/services/formatter.service.d.ts +245 -35
- package/dist/core/services/formatter.service.d.ts.map +1 -1
- package/dist/core/services/formatter.service.js +281 -57
- package/dist/core/services/formatter.service.js.map +1 -1
- package/dist/core/services/generator.service.d.ts +115 -26
- package/dist/core/services/generator.service.d.ts.map +1 -1
- package/dist/core/services/generator.service.js +191 -56
- package/dist/core/services/generator.service.js.map +1 -1
- package/dist/core/services/geometry.service.d.ts +369 -39
- package/dist/core/services/geometry.service.d.ts.map +1 -1
- package/dist/core/services/geometry.service.js +424 -66
- package/dist/core/services/geometry.service.js.map +1 -1
- package/dist/core/services/http.service.d.ts +244 -21
- package/dist/core/services/http.service.d.ts.map +1 -1
- package/dist/core/services/http.service.js +419 -98
- package/dist/core/services/http.service.js.map +1 -1
- package/dist/core/services/logger.service.d.ts +95 -29
- package/dist/core/services/logger.service.d.ts.map +1 -1
- package/dist/core/services/logger.service.js +167 -41
- package/dist/core/services/logger.service.js.map +1 -1
- package/dist/core/services/reactive.service.d.ts +146 -19
- package/dist/core/services/reactive.service.d.ts.map +1 -1
- package/dist/core/services/reactive.service.js +278 -137
- package/dist/core/services/reactive.service.js.map +1 -1
- package/dist/core/services/timing.service.d.ts +142 -20
- package/dist/core/services/timing.service.d.ts.map +1 -1
- package/dist/core/services/timing.service.js +324 -185
- package/dist/core/services/timing.service.js.map +1 -1
- package/dist/core/services/utils.service.d.ts +228 -43
- package/dist/core/services/utils.service.d.ts.map +1 -1
- package/dist/core/services/utils.service.js +305 -125
- package/dist/core/services/utils.service.js.map +1 -1
- package/dist/infrastructure/dom/dom.service.d.ts +307 -44
- package/dist/infrastructure/dom/dom.service.d.ts.map +1 -1
- package/dist/infrastructure/dom/dom.service.js +408 -129
- package/dist/infrastructure/dom/dom.service.js.map +1 -1
- package/dist/infrastructure/index.d.ts +0 -3
- package/dist/infrastructure/index.d.ts.map +1 -1
- package/dist/infrastructure/index.js +0 -3
- package/dist/infrastructure/index.js.map +1 -1
- package/dist/infrastructure/observer/observer.service.d.ts +124 -31
- package/dist/infrastructure/observer/observer.service.d.ts.map +1 -1
- package/dist/infrastructure/observer/observer.service.js +227 -131
- package/dist/infrastructure/observer/observer.service.js.map +1 -1
- package/dist/infrastructure/sensors/sensors.service.d.ts +154 -22
- package/dist/infrastructure/sensors/sensors.service.d.ts.map +1 -1
- package/dist/infrastructure/sensors/sensors.service.js +264 -112
- package/dist/infrastructure/sensors/sensors.service.js.map +1 -1
- package/dist/infrastructure/storage/storage.service.d.ts +123 -45
- package/dist/infrastructure/storage/storage.service.d.ts.map +1 -1
- package/dist/infrastructure/storage/storage.service.js +236 -66
- package/dist/infrastructure/storage/storage.service.js.map +1 -1
- package/dist/infrastructure/theme/theme.service.d.ts +93 -29
- package/dist/infrastructure/theme/theme.service.d.ts.map +1 -1
- package/dist/infrastructure/theme/theme.service.js +198 -96
- package/dist/infrastructure/theme/theme.service.js.map +1 -1
- package/dist/infrastructure/viewport/viewport.service.d.ts +233 -35
- package/dist/infrastructure/viewport/viewport.service.d.ts.map +1 -1
- package/dist/infrastructure/viewport/viewport.service.js +358 -139
- package/dist/infrastructure/viewport/viewport.service.js.map +1 -1
- package/dist/infrastructure/worker/worker.service.d.ts +84 -38
- package/dist/infrastructure/worker/worker.service.d.ts.map +1 -1
- package/dist/infrastructure/worker/worker.service.js +234 -133
- package/dist/infrastructure/worker/worker.service.js.map +1 -1
- package/dist/prisma/index.d.ts +2 -0
- package/dist/prisma/index.d.ts.map +1 -0
- package/dist/prisma/index.js +2 -0
- package/dist/prisma/index.js.map +1 -0
- package/dist/prisma/schema.json +11 -33
- package/dist/prisma/use-prisma.d.ts +68 -0
- package/dist/prisma/use-prisma.d.ts.map +1 -0
- package/dist/prisma/use-prisma.js +82 -0
- package/dist/prisma/use-prisma.js.map +1 -0
- package/dist/types/index.d.ts +20 -12
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +124 -125
- package/CHANGELOG.md +0 -101
|
@@ -1,217 +1,356 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useLogger } from "./logger.service.js";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
3
|
+
* Executes a callback after `ms` milliseconds, returning a cancellable
|
|
4
|
+
* promise that resolves with the callback's result.
|
|
5
|
+
*
|
|
6
|
+
* @typeParam T - The callback's return type.
|
|
7
|
+
* @param callback - Function to execute after the delay.
|
|
8
|
+
* @param ms - Delay in milliseconds.
|
|
9
|
+
* @returns A `TimeoutControl` with `promise` and `cancel()`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const { promise, cancel } = useSetTimeout(() => 42, 1000);
|
|
14
|
+
* cancel(); // rejects the promise with "Timeout cancelled"
|
|
15
|
+
* ```
|
|
5
16
|
*/
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
useDelay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
16
|
-
/** Alias for delay, more semantic for sleep operations. */
|
|
17
|
-
useSleep = (ms) => this.useDelay(ms);
|
|
18
|
-
useSetTimeout = (callback, ms) => {
|
|
19
|
-
let timerId;
|
|
20
|
-
let isCancelled = false;
|
|
21
|
-
const promise = new Promise((resolve, reject) => {
|
|
22
|
-
timerId = setTimeout(async () => {
|
|
23
|
-
if (isCancelled)
|
|
24
|
-
return;
|
|
25
|
-
try {
|
|
26
|
-
const result = await callback();
|
|
27
|
-
resolve(result);
|
|
28
|
-
}
|
|
29
|
-
catch (error) {
|
|
30
|
-
reject(error);
|
|
31
|
-
}
|
|
32
|
-
}, ms);
|
|
33
|
-
});
|
|
34
|
-
const cancel = () => {
|
|
35
|
-
isCancelled = true;
|
|
36
|
-
clearTimeout(timerId);
|
|
37
|
-
};
|
|
38
|
-
return { promise, cancel };
|
|
39
|
-
};
|
|
40
|
-
useInterval = (callback, ms, immediate = false) => {
|
|
41
|
-
let timerId = null;
|
|
42
|
-
let isPaused = false;
|
|
43
|
-
let isStopped = false;
|
|
44
|
-
let isExecuting = false;
|
|
45
|
-
const execute = async () => {
|
|
46
|
-
if (isPaused || isStopped || isExecuting)
|
|
17
|
+
export function useSetTimeout(callback, ms) {
|
|
18
|
+
let timerId;
|
|
19
|
+
let isCancelled = false;
|
|
20
|
+
let rejectFn;
|
|
21
|
+
const promise = new Promise((resolve, reject) => {
|
|
22
|
+
rejectFn = reject;
|
|
23
|
+
timerId = setTimeout(async () => {
|
|
24
|
+
if (isCancelled)
|
|
47
25
|
return;
|
|
48
|
-
isExecuting = true;
|
|
49
26
|
try {
|
|
50
|
-
await callback();
|
|
27
|
+
const result = await callback();
|
|
28
|
+
resolve(result);
|
|
51
29
|
}
|
|
52
30
|
catch (error) {
|
|
53
|
-
|
|
31
|
+
reject(error);
|
|
54
32
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
timerId
|
|
63
|
-
await execute();
|
|
64
|
-
scheduleNext();
|
|
65
|
-
}, ms);
|
|
66
|
-
};
|
|
67
|
-
const stop = () => {
|
|
68
|
-
isStopped = true;
|
|
69
|
-
isPaused = false;
|
|
70
|
-
if (timerId) {
|
|
71
|
-
clearTimeout(timerId);
|
|
72
|
-
timerId = null;
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
const pause = () => {
|
|
76
|
-
if (isStopped)
|
|
77
|
-
return;
|
|
78
|
-
isPaused = true;
|
|
79
|
-
if (timerId) {
|
|
80
|
-
clearTimeout(timerId);
|
|
81
|
-
timerId = null;
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
const resume = () => {
|
|
85
|
-
if (isStopped || !isPaused)
|
|
86
|
-
return;
|
|
87
|
-
isPaused = false;
|
|
88
|
-
scheduleNext();
|
|
89
|
-
};
|
|
90
|
-
const isRunning = () => !isStopped && !isPaused;
|
|
91
|
-
if (immediate) {
|
|
92
|
-
execute().then(() => scheduleNext());
|
|
33
|
+
}, ms);
|
|
34
|
+
});
|
|
35
|
+
const cancel = () => {
|
|
36
|
+
if (isCancelled)
|
|
37
|
+
return;
|
|
38
|
+
isCancelled = true;
|
|
39
|
+
if (timerId !== undefined) {
|
|
40
|
+
clearTimeout(timerId);
|
|
93
41
|
}
|
|
94
|
-
|
|
42
|
+
rejectFn?.(new Error("Timeout cancelled"));
|
|
43
|
+
};
|
|
44
|
+
return { promise, cancel };
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Repeatedly executes a callback at `ms` intervals with pause/resume/stop
|
|
48
|
+
* controls. Uses recursive `setTimeout` internally to avoid overlap.
|
|
49
|
+
*
|
|
50
|
+
* @param callback - Function executed on each tick.
|
|
51
|
+
* @param ms - Interval in milliseconds.
|
|
52
|
+
* @param immediate - If `true`, fires the callback immediately before the
|
|
53
|
+
* first interval (defaults to `false`).
|
|
54
|
+
* @returns An `IntervalControl` with `pause`, `resume`, `stop`, and
|
|
55
|
+
* `isRunning`.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* const timer = useInterval(() => console.log("tick"), 1000);
|
|
60
|
+
* setTimeout(() => timer.pause(), 3000);
|
|
61
|
+
* setTimeout(() => timer.resume(), 5000);
|
|
62
|
+
* setTimeout(() => timer.stop(), 10000);
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export function useInterval(callback, ms, immediate = false) {
|
|
66
|
+
let timerId = null;
|
|
67
|
+
let isPaused = false;
|
|
68
|
+
let isStopped = false;
|
|
69
|
+
let isExecuting = false;
|
|
70
|
+
const execute = async () => {
|
|
71
|
+
if (isPaused || isStopped || isExecuting)
|
|
72
|
+
return;
|
|
73
|
+
isExecuting = true;
|
|
74
|
+
try {
|
|
75
|
+
await callback();
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
useLogger("error", "[interval] Callback error:", error);
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
isExecuting = false;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
const scheduleNext = () => {
|
|
85
|
+
if (isStopped || isPaused)
|
|
86
|
+
return;
|
|
87
|
+
timerId = setTimeout(async () => {
|
|
88
|
+
await execute();
|
|
95
89
|
scheduleNext();
|
|
90
|
+
}, ms);
|
|
91
|
+
};
|
|
92
|
+
const stop = () => {
|
|
93
|
+
isStopped = true;
|
|
94
|
+
isPaused = false;
|
|
95
|
+
if (timerId) {
|
|
96
|
+
clearTimeout(timerId);
|
|
97
|
+
timerId = null;
|
|
96
98
|
}
|
|
97
|
-
return { pause, resume, stop, isRunning };
|
|
98
99
|
};
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
func(...args);
|
|
108
|
-
}
|
|
109
|
-
catch (error) {
|
|
110
|
-
useLog("error", "[debounce] Callback error:", error);
|
|
111
|
-
}
|
|
112
|
-
timeoutId = undefined;
|
|
113
|
-
}, delayMs);
|
|
114
|
-
};
|
|
100
|
+
const pause = () => {
|
|
101
|
+
if (isStopped)
|
|
102
|
+
return;
|
|
103
|
+
isPaused = true;
|
|
104
|
+
if (timerId) {
|
|
105
|
+
clearTimeout(timerId);
|
|
106
|
+
timerId = null;
|
|
107
|
+
}
|
|
115
108
|
};
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
109
|
+
const resume = () => {
|
|
110
|
+
if (isStopped || !isPaused)
|
|
111
|
+
return;
|
|
112
|
+
isPaused = false;
|
|
113
|
+
scheduleNext();
|
|
114
|
+
};
|
|
115
|
+
const isRunning = () => !isStopped && !isPaused;
|
|
116
|
+
if (immediate) {
|
|
117
|
+
execute().then(() => scheduleNext());
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
scheduleNext();
|
|
121
|
+
}
|
|
122
|
+
return { pause, resume, stop, isRunning };
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Returns a debounced version of `func` that delays invocation until
|
|
126
|
+
* `delayMs` milliseconds of quiet.
|
|
127
|
+
*
|
|
128
|
+
* @typeParam T - The wrapped function type.
|
|
129
|
+
* @param func - Function to debounce.
|
|
130
|
+
* @param delayMs - Debounce delay in milliseconds.
|
|
131
|
+
* @returns A debounced function with the same signature.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```ts
|
|
135
|
+
* const debouncedSearch = useDebounce(fetchResults, 300);
|
|
136
|
+
* input.addEventListener("input", () => debouncedSearch(input.value));
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
export function useDebounce(func, delayMs) {
|
|
140
|
+
let timeoutId;
|
|
141
|
+
return (...args) => {
|
|
142
|
+
if (timeoutId !== undefined) {
|
|
143
|
+
clearTimeout(timeoutId);
|
|
144
|
+
}
|
|
145
|
+
timeoutId = setTimeout(() => {
|
|
146
|
+
try {
|
|
147
|
+
func(...args);
|
|
130
148
|
}
|
|
131
|
-
|
|
132
|
-
|
|
149
|
+
catch (error) {
|
|
150
|
+
useLogger("error", "[debounce] Callback error:", error);
|
|
133
151
|
}
|
|
134
|
-
timeoutId =
|
|
135
|
-
|
|
136
|
-
try {
|
|
137
|
-
func(...args);
|
|
138
|
-
}
|
|
139
|
-
catch (error) {
|
|
140
|
-
useLog("error", "[debounceImmediate] Callback error:", error);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
timeoutId = undefined;
|
|
144
|
-
lastCallTime = undefined;
|
|
145
|
-
}, delayMs);
|
|
146
|
-
};
|
|
152
|
+
timeoutId = undefined;
|
|
153
|
+
}, delayMs);
|
|
147
154
|
};
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Leading-edge debounce: fires immediately on the first call, then ignores
|
|
158
|
+
* calls until `delayMs` of quiet. Additional calls during the wait window
|
|
159
|
+
* schedule a single trailing invocation with the latest arguments.
|
|
160
|
+
*
|
|
161
|
+
* @typeParam T - The wrapped function type.
|
|
162
|
+
* @param func - Function to debounce.
|
|
163
|
+
* @param delayMs - Debounce delay in milliseconds.
|
|
164
|
+
* @returns A debounced function with the same signature.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* const save = useDebounceImmediate(submitForm, 500);
|
|
169
|
+
* save("a"); // fires immediately
|
|
170
|
+
* save("b"); // queued
|
|
171
|
+
* save("c"); // queued (replaces "b")
|
|
172
|
+
* // after 500ms of quiet: fires with "c"
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
export function useDebounceImmediate(func, delayMs) {
|
|
176
|
+
let timeoutId;
|
|
177
|
+
let invoked = false;
|
|
178
|
+
let lastArgs = null;
|
|
179
|
+
return (...args) => {
|
|
180
|
+
lastArgs = args;
|
|
181
|
+
if (!invoked) {
|
|
182
|
+
invoked = true;
|
|
183
|
+
lastArgs = null;
|
|
184
|
+
try {
|
|
185
|
+
func(...args);
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
useLogger("error", "[debounceImmediate] Callback error:", error);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (timeoutId !== undefined) {
|
|
192
|
+
clearTimeout(timeoutId);
|
|
193
|
+
}
|
|
194
|
+
timeoutId = setTimeout(() => {
|
|
195
|
+
if (lastArgs) {
|
|
152
196
|
try {
|
|
153
|
-
func(...
|
|
197
|
+
func(...lastArgs);
|
|
154
198
|
}
|
|
155
199
|
catch (error) {
|
|
156
|
-
|
|
200
|
+
useLogger("error", "[debounceImmediate] Callback error:", error);
|
|
157
201
|
}
|
|
158
|
-
inThrottle = true;
|
|
159
|
-
setTimeout(() => {
|
|
160
|
-
inThrottle = false;
|
|
161
|
-
}, limitMs);
|
|
162
202
|
}
|
|
163
|
-
|
|
203
|
+
invoked = false;
|
|
204
|
+
lastArgs = null;
|
|
205
|
+
timeoutId = undefined;
|
|
206
|
+
}, delayMs);
|
|
164
207
|
};
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
}, limitMs);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Returns a throttled version of `func` that fires at most once per
|
|
211
|
+
* `limitMs` milliseconds (leading edge).
|
|
212
|
+
*
|
|
213
|
+
* @typeParam T - The wrapped function type.
|
|
214
|
+
* @param func - Function to throttle.
|
|
215
|
+
* @param limitMs - Minimum interval between invocations in milliseconds.
|
|
216
|
+
* @returns A throttled function with the same signature.
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```ts
|
|
220
|
+
* const throttledScroll = useThrottle(onScroll, 100);
|
|
221
|
+
* window.addEventListener("scroll", throttledScroll);
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
export function useThrottle(func, limitMs) {
|
|
225
|
+
let inThrottle = false;
|
|
226
|
+
return (...args) => {
|
|
227
|
+
if (!inThrottle) {
|
|
228
|
+
try {
|
|
229
|
+
func(...args);
|
|
189
230
|
}
|
|
190
|
-
|
|
191
|
-
|
|
231
|
+
catch (error) {
|
|
232
|
+
useLogger("error", "[throttle] Callback error:", error);
|
|
192
233
|
}
|
|
193
|
-
|
|
234
|
+
inThrottle = true;
|
|
235
|
+
setTimeout(() => {
|
|
236
|
+
inThrottle = false;
|
|
237
|
+
}, limitMs);
|
|
238
|
+
}
|
|
194
239
|
};
|
|
195
|
-
|
|
196
|
-
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Throttle with trailing invocation: fires on the leading edge and also
|
|
243
|
+
* fires once at the end of the throttle window with the latest arguments.
|
|
244
|
+
*
|
|
245
|
+
* @typeParam T - The wrapped function type.
|
|
246
|
+
* @param func - Function to throttle.
|
|
247
|
+
* @param limitMs - Minimum interval between invocations in milliseconds.
|
|
248
|
+
* @returns A throttled function with the same signature.
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```ts
|
|
252
|
+
* const throttledResize = useThrottleTrailing(onResize, 200);
|
|
253
|
+
* window.addEventListener("resize", throttledResize);
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
export function useThrottleTrailing(func, limitMs) {
|
|
257
|
+
let inThrottle = false;
|
|
258
|
+
let lastArgs = null;
|
|
259
|
+
return (...args) => {
|
|
260
|
+
if (!inThrottle) {
|
|
197
261
|
try {
|
|
198
|
-
|
|
262
|
+
func(...args);
|
|
199
263
|
}
|
|
200
264
|
catch (error) {
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
if (i < iterations - 1 && delayMs > 0) {
|
|
204
|
-
await this.useDelay(delayMs);
|
|
265
|
+
useLogger("error", "[throttleTrailing] Callback error:", error);
|
|
205
266
|
}
|
|
267
|
+
inThrottle = true;
|
|
268
|
+
lastArgs = null;
|
|
269
|
+
setTimeout(() => {
|
|
270
|
+
inThrottle = false;
|
|
271
|
+
if (lastArgs) {
|
|
272
|
+
try {
|
|
273
|
+
func(...lastArgs);
|
|
274
|
+
}
|
|
275
|
+
catch (error) {
|
|
276
|
+
useLogger("error", "[throttleTrailing] Callback error:", error);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}, limitMs);
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
lastArgs = args;
|
|
206
283
|
}
|
|
207
284
|
};
|
|
208
|
-
useRace = async (promise, timeoutMs, errorMessage = "Operation timed out") => {
|
|
209
|
-
const timeoutPromise = new Promise((_, reject) => {
|
|
210
|
-
setTimeout(() => reject(new Error(errorMessage)), timeoutMs);
|
|
211
|
-
});
|
|
212
|
-
return Promise.race([promise, timeoutPromise]);
|
|
213
|
-
};
|
|
214
285
|
}
|
|
215
|
-
|
|
216
|
-
|
|
286
|
+
/**
|
|
287
|
+
* Executes a callback `iterations` times with an optional delay between
|
|
288
|
+
* each iteration.
|
|
289
|
+
*
|
|
290
|
+
* @param callback - Function called on each iteration with the current
|
|
291
|
+
* index (0-based).
|
|
292
|
+
* @param iterations - Number of times to repeat.
|
|
293
|
+
* @param delayMs - Delay between iterations in milliseconds (defaults to
|
|
294
|
+
* `0`).
|
|
295
|
+
*
|
|
296
|
+
* @example
|
|
297
|
+
* ```ts
|
|
298
|
+
* await useRepeat((i) => console.log(`Attempt ${i + 1}`), 3, 1000);
|
|
299
|
+
* // Attempt 1 ... (1s) ... Attempt 2 ... (1s) ... Attempt 3
|
|
300
|
+
* ```
|
|
301
|
+
*/
|
|
302
|
+
export async function useRepeat(callback, iterations, delayMs = 0) {
|
|
303
|
+
for (let i = 0; i < iterations; i++) {
|
|
304
|
+
try {
|
|
305
|
+
await callback(i);
|
|
306
|
+
}
|
|
307
|
+
catch (error) {
|
|
308
|
+
useLogger("error", "[repeat] Callback error:", error);
|
|
309
|
+
}
|
|
310
|
+
if (i < iterations - 1 && delayMs > 0) {
|
|
311
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Races a promise against a timeout. Clears the timer when the promise
|
|
317
|
+
* wins so the timeout rejection cannot become an unhandled rejection.
|
|
318
|
+
*
|
|
319
|
+
* @typeParam T - The promise's resolved type.
|
|
320
|
+
* @param promise - The promise to race.
|
|
321
|
+
* @param timeoutMs - Timeout in milliseconds.
|
|
322
|
+
* @param errorMessage - Error message on timeout (defaults to
|
|
323
|
+
* `"Operation timed out"`).
|
|
324
|
+
* @returns The resolved value of the promise.
|
|
325
|
+
* @throws {Error} If the timeout fires first.
|
|
326
|
+
*
|
|
327
|
+
* @example
|
|
328
|
+
* ```ts
|
|
329
|
+
* try {
|
|
330
|
+
* const data = await useRace(fetch("/api"), 5000);
|
|
331
|
+
* } catch (e) {
|
|
332
|
+
* console.error(e); // "Operation timed out"
|
|
333
|
+
* }
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
336
|
+
export async function useRace(promise, timeoutMs, errorMessage = "Operation timed out") {
|
|
337
|
+
let timeoutId;
|
|
338
|
+
let settled = false;
|
|
339
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
340
|
+
timeoutId = setTimeout(() => {
|
|
341
|
+
if (!settled) {
|
|
342
|
+
reject(new Error(errorMessage));
|
|
343
|
+
}
|
|
344
|
+
}, timeoutMs);
|
|
345
|
+
});
|
|
346
|
+
try {
|
|
347
|
+
return await Promise.race([promise, timeoutPromise]);
|
|
348
|
+
}
|
|
349
|
+
finally {
|
|
350
|
+
settled = true;
|
|
351
|
+
if (timeoutId !== undefined) {
|
|
352
|
+
clearTimeout(timeoutId);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
217
356
|
//# sourceMappingURL=timing.service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timing.service.js","sourceRoot":"","sources":["../../../src/core/services/timing.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"timing.service.js","sourceRoot":"","sources":["../../../src/core/services/timing.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAIhD;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,aAAa,CAAI,QAA8B,EAAE,EAAU;IAC1E,IAAI,OAA4B,CAAC;IACjC,IAAI,WAAW,GAAG,KAAK,CAAC;IACxB,IAAI,QAAkD,CAAC;IAEvD,MAAM,OAAO,GAAG,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAClD,QAAQ,GAAG,MAAM,CAAC;QAClB,OAAO,GAAG,UAAU,CAAC,KAAK,IAAI,EAAE;YAC/B,IAAI,WAAW;gBAAE,OAAO;YAExB,IAAI,CAAC;gBACJ,MAAM,MAAM,GAAG,MAAM,QAAQ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