@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 CHANGED
@@ -1,6 +1,10 @@
1
1
  type AnyObject = {
2
2
  [key: string | number]: any;
3
3
  };
4
+ interface ThrottleHandle {
5
+ /** Stop the repeating emission */
6
+ clear(): void;
7
+ }
4
8
 
5
9
  declare class GraphContext {
6
10
  readonly id: string;
@@ -708,6 +712,18 @@ declare class SignalBroker {
708
712
  * @edge Removes all instances if duplicate; deletes if empty.
709
713
  */
710
714
  unsubscribe(signal: string, routineOrTask: Task | GraphRoutine): void;
715
+ schedule(signal: string, context: AnyObject, timeoutMs?: number, exactDateTime?: Date): AbortController;
716
+ /**
717
+ * Emits `signal` repeatedly with a fixed interval.
718
+ *
719
+ * @param signal
720
+ * @param context
721
+ * @param intervalMs
722
+ * @param leading If true, emits immediately (unless a startDateTime is given and we are before it).
723
+ * @param startDateTime Optional absolute Date when the *first* emission after `leading` should occur.
724
+ * @returns a handle with `clear()` to stop the loop.
725
+ */
726
+ throttle(signal: string, context: AnyObject, intervalMs?: number, leading?: boolean, startDateTime?: Date): ThrottleHandle;
711
727
  /**
712
728
  * Emits a signal and bubbles to matching wildcards/parents (e.g., 'a.b.action' triggers 'a.b.action', 'a.b.*', 'a.*').
713
729
  * @param signal The signal name.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  type AnyObject = {
2
2
  [key: string | number]: any;
3
3
  };
4
+ interface ThrottleHandle {
5
+ /** Stop the repeating emission */
6
+ clear(): void;
7
+ }
4
8
 
5
9
  declare class GraphContext {
6
10
  readonly id: string;
@@ -708,6 +712,18 @@ declare class SignalBroker {
708
712
  * @edge Removes all instances if duplicate; deletes if empty.
709
713
  */
710
714
  unsubscribe(signal: string, routineOrTask: Task | GraphRoutine): void;
715
+ schedule(signal: string, context: AnyObject, timeoutMs?: number, exactDateTime?: Date): AbortController;
716
+ /**
717
+ * Emits `signal` repeatedly with a fixed interval.
718
+ *
719
+ * @param signal
720
+ * @param context
721
+ * @param intervalMs
722
+ * @param leading If true, emits immediately (unless a startDateTime is given and we are before it).
723
+ * @param startDateTime Optional absolute Date when the *first* emission after `leading` should occur.
724
+ * @returns a handle with `clear()` to stop the loop.
725
+ */
726
+ throttle(signal: string, context: AnyObject, intervalMs?: number, leading?: boolean, startDateTime?: Date): ThrottleHandle;
711
727
  /**
712
728
  * Emits a signal and bubbles to matching wildcards/parents (e.g., 'a.b.action' triggers 'a.b.action', 'a.b.*', 'a.*').
713
729
  * @param signal The signal name.
package/dist/index.js CHANGED
@@ -196,6 +196,67 @@ var SignalBroker = class _SignalBroker {
196
196
  }
197
197
  }
198
198
  }
199
+ schedule(signal, context, timeoutMs = 6e4, exactDateTime) {
200
+ let delay = timeoutMs;
201
+ if (exactDateTime != null) {
202
+ delay = exactDateTime.getTime() - Date.now();
203
+ }
204
+ delay = Math.max(0, timeoutMs);
205
+ const controller = new AbortController();
206
+ const { signal: signalController } = controller;
207
+ const tick = () => this.emit(signal, context);
208
+ const timerId = setTimeout(() => {
209
+ if (!signalController.aborted) tick();
210
+ }, delay);
211
+ signalController.addEventListener("abort", () => clearTimeout(timerId));
212
+ return controller;
213
+ }
214
+ /**
215
+ * Emits `signal` repeatedly with a fixed interval.
216
+ *
217
+ * @param signal
218
+ * @param context
219
+ * @param intervalMs
220
+ * @param leading If true, emits immediately (unless a startDateTime is given and we are before it).
221
+ * @param startDateTime Optional absolute Date when the *first* emission after `leading` should occur.
222
+ * @returns a handle with `clear()` to stop the loop.
223
+ */
224
+ throttle(signal, context, intervalMs = 6e4, leading = false, startDateTime) {
225
+ if (intervalMs <= 0) {
226
+ throw new Error("intervalMs must be a positive number");
227
+ }
228
+ const emit = () => this.emit(signal, context);
229
+ if (leading) {
230
+ const now = Date.now();
231
+ const start = startDateTime?.getTime();
232
+ if (!start || start <= now) {
233
+ emit();
234
+ }
235
+ }
236
+ let firstDelay = intervalMs;
237
+ if (startDateTime) {
238
+ let slot = startDateTime.getTime();
239
+ const now = Date.now();
240
+ while (slot < now) {
241
+ slot += intervalMs;
242
+ }
243
+ firstDelay = slot - now;
244
+ }
245
+ let timer = null;
246
+ let stopped = false;
247
+ const scheduleNext = () => {
248
+ if (stopped) return;
249
+ emit();
250
+ timer = setTimeout(scheduleNext, intervalMs);
251
+ };
252
+ timer = setTimeout(scheduleNext, firstDelay);
253
+ return {
254
+ clear() {
255
+ stopped = true;
256
+ if (timer !== null) clearTimeout(timer);
257
+ }
258
+ };
259
+ }
199
260
  /**
200
261
  * Emits a signal and bubbles to matching wildcards/parents (e.g., 'a.b.action' triggers 'a.b.action', 'a.b.*', 'a.*').
201
262
  * @param signal The signal name.