@cadenza.io/core 3.9.2 → 3.10.0
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/dist/index.d.mts +16 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +61 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +61 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -159,6 +159,67 @@ var SignalBroker = class _SignalBroker {
|
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
|
+
schedule(signal, context, timeoutMs = 6e4, exactDateTime) {
|
|
163
|
+
let delay = timeoutMs;
|
|
164
|
+
if (exactDateTime != null) {
|
|
165
|
+
delay = exactDateTime.getTime() - Date.now();
|
|
166
|
+
}
|
|
167
|
+
delay = Math.max(0, timeoutMs);
|
|
168
|
+
const controller = new AbortController();
|
|
169
|
+
const { signal: signalController } = controller;
|
|
170
|
+
const tick = () => this.emit(signal, context);
|
|
171
|
+
const timerId = setTimeout(() => {
|
|
172
|
+
if (!signalController.aborted) tick();
|
|
173
|
+
}, delay);
|
|
174
|
+
signalController.addEventListener("abort", () => clearTimeout(timerId));
|
|
175
|
+
return controller;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Emits `signal` repeatedly with a fixed interval.
|
|
179
|
+
*
|
|
180
|
+
* @param signal
|
|
181
|
+
* @param context
|
|
182
|
+
* @param intervalMs
|
|
183
|
+
* @param leading If true, emits immediately (unless a startDateTime is given and we are before it).
|
|
184
|
+
* @param startDateTime Optional absolute Date when the *first* emission after `leading` should occur.
|
|
185
|
+
* @returns a handle with `clear()` to stop the loop.
|
|
186
|
+
*/
|
|
187
|
+
throttle(signal, context, intervalMs = 6e4, leading = false, startDateTime) {
|
|
188
|
+
if (intervalMs <= 0) {
|
|
189
|
+
throw new Error("intervalMs must be a positive number");
|
|
190
|
+
}
|
|
191
|
+
const emit = () => this.emit(signal, context);
|
|
192
|
+
if (leading) {
|
|
193
|
+
const now = Date.now();
|
|
194
|
+
const start = startDateTime?.getTime();
|
|
195
|
+
if (!start || start <= now) {
|
|
196
|
+
emit();
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
let firstDelay = intervalMs;
|
|
200
|
+
if (startDateTime) {
|
|
201
|
+
let slot = startDateTime.getTime();
|
|
202
|
+
const now = Date.now();
|
|
203
|
+
while (slot < now) {
|
|
204
|
+
slot += intervalMs;
|
|
205
|
+
}
|
|
206
|
+
firstDelay = slot - now;
|
|
207
|
+
}
|
|
208
|
+
let timer = null;
|
|
209
|
+
let stopped = false;
|
|
210
|
+
const scheduleNext = () => {
|
|
211
|
+
if (stopped) return;
|
|
212
|
+
emit();
|
|
213
|
+
timer = setTimeout(scheduleNext, intervalMs);
|
|
214
|
+
};
|
|
215
|
+
timer = setTimeout(scheduleNext, firstDelay);
|
|
216
|
+
return {
|
|
217
|
+
clear() {
|
|
218
|
+
stopped = true;
|
|
219
|
+
if (timer !== null) clearTimeout(timer);
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
}
|
|
162
223
|
/**
|
|
163
224
|
* Emits a signal and bubbles to matching wildcards/parents (e.g., 'a.b.action' triggers 'a.b.action', 'a.b.*', 'a.*').
|
|
164
225
|
* @param signal The signal name.
|