@develia/commons 0.4.22 → 0.4.24

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.
Files changed (73) hide show
  1. package/dist/cjs/collections/cache-dictionary.js +2 -6
  2. package/dist/cjs/collections/cache-dictionary.js.map +1 -1
  3. package/dist/cjs/debouncer.js +121 -0
  4. package/dist/cjs/debouncer.js.map +1 -0
  5. package/dist/cjs/functions.js +0 -86
  6. package/dist/cjs/functions.js.map +1 -1
  7. package/dist/cjs/index.js +8 -1
  8. package/dist/cjs/index.js.map +1 -1
  9. package/dist/cjs/keyed-debouncer.js +144 -0
  10. package/dist/cjs/keyed-debouncer.js.map +1 -0
  11. package/dist/cjs/result.js +3 -0
  12. package/dist/cjs/result.js.map +1 -0
  13. package/dist/cjs/triggers.js +103 -0
  14. package/dist/cjs/triggers.js.map +1 -0
  15. package/dist/cjs/utilities.js +27 -4
  16. package/dist/cjs/utilities.js.map +1 -1
  17. package/dist/esm/collections/cache-dictionary.js +2 -6
  18. package/dist/esm/collections/cache-dictionary.js.map +1 -1
  19. package/dist/esm/debouncer.js +118 -0
  20. package/dist/esm/debouncer.js.map +1 -0
  21. package/dist/esm/functions.js +0 -85
  22. package/dist/esm/functions.js.map +1 -1
  23. package/dist/esm/index.js +3 -0
  24. package/dist/esm/index.js.map +1 -1
  25. package/dist/esm/keyed-debouncer.js +109 -0
  26. package/dist/esm/keyed-debouncer.js.map +1 -0
  27. package/dist/esm/result.js +2 -0
  28. package/dist/esm/result.js.map +1 -0
  29. package/dist/esm/triggers.js +98 -0
  30. package/dist/esm/triggers.js.map +1 -0
  31. package/dist/esm/utilities.js +27 -4
  32. package/dist/esm/utilities.js.map +1 -1
  33. package/dist/types/collections/cache-dictionary.d.ts.map +1 -1
  34. package/dist/types/debouncer.d.ts +60 -0
  35. package/dist/types/debouncer.d.ts.map +1 -0
  36. package/dist/types/functions.d.ts +5 -35
  37. package/dist/types/functions.d.ts.map +1 -1
  38. package/dist/types/index.d.ts +6 -0
  39. package/dist/types/index.d.ts.map +1 -1
  40. package/dist/types/keyed-debouncer.d.ts +56 -0
  41. package/dist/types/keyed-debouncer.d.ts.map +1 -0
  42. package/dist/types/result.d.ts +11 -0
  43. package/dist/types/result.d.ts.map +1 -0
  44. package/dist/types/triggers.d.ts +64 -0
  45. package/dist/types/triggers.d.ts.map +1 -0
  46. package/dist/types/utilities.d.ts +8 -3
  47. package/dist/types/utilities.d.ts.map +1 -1
  48. package/dist/web/collections/cache-dictionary.js +2 -6
  49. package/dist/web/collections/cache-dictionary.js.map +1 -1
  50. package/dist/web/debouncer.js +118 -0
  51. package/dist/web/debouncer.js.map +1 -0
  52. package/dist/web/functions.js +0 -85
  53. package/dist/web/functions.js.map +1 -1
  54. package/dist/web/index.js +3 -0
  55. package/dist/web/index.js.map +1 -1
  56. package/dist/web/keyed-debouncer.js +109 -0
  57. package/dist/web/keyed-debouncer.js.map +1 -0
  58. package/dist/web/result.js +2 -0
  59. package/dist/web/result.js.map +1 -0
  60. package/dist/web/triggers.js +99 -0
  61. package/dist/web/triggers.js.map +1 -0
  62. package/dist/web/utilities.js +27 -4
  63. package/dist/web/utilities.js.map +1 -1
  64. package/package.json +3 -4
  65. package/src/collections/cache-dictionary.ts +2 -6
  66. package/src/debouncer.ts +135 -0
  67. package/src/functions.ts +5 -114
  68. package/src/index.ts +6 -0
  69. package/src/keyed-debouncer.ts +121 -0
  70. package/src/result.ts +6 -0
  71. package/src/triggers.ts +126 -0
  72. package/src/utilities.ts +30 -4
  73. package/dist/cjs/package.json +0 -3
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Cancellable task. It must honour `signal`: pass it to fetch/SDKs and check
3
+ * `signal.aborted` before producing irreversible side effects.
4
+ */
5
+ export type DebouncedTask = (signal: AbortSignal) => Promise<void>;
6
+ /** Options accepted by {@link Debouncer}. */
7
+ export interface DebouncerOptions {
8
+ /** Default wait, in milliseconds, before a task is executed. */
9
+ delayMs: number;
10
+ /** Called every time a task is scheduled, after any previous task has been cancelled. */
11
+ onSchedule?: () => void;
12
+ /** Called when the wait elapses and the task is about to run. */
13
+ onStart?: () => void;
14
+ /** Called when a task completes without throwing and without being cancelled. */
15
+ onSuccess?: () => void;
16
+ /** Called when a live task is cancelled, either by {@link Debouncer.cancel} or by being replaced. */
17
+ onCancel?: () => void;
18
+ /** Receives task errors. Cancellations never reach this callback. Defaults to `console.error`. */
19
+ onError?: (error: unknown) => void;
20
+ /** Called every time the debouncer goes from busy to idle. */
21
+ onIdle?: () => void;
22
+ }
23
+ /**
24
+ * Keeps at most one live task.
25
+ * Scheduling a new task cancels the previous one, whether it was still
26
+ * waiting or already running.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * const debouncer = new Debouncer({delayMs: 1500});
31
+ *
32
+ * debouncer.schedule(async (signal) => {
33
+ * const reply = await callAI(messages, {signal});
34
+ * if (signal.aborted) return;
35
+ * await sendReply(reply);
36
+ * });
37
+ * ```
38
+ */
39
+ export default class Debouncer {
40
+ #private;
41
+ constructor(options: DebouncerOptions);
42
+ /** Whether a task is waiting or running. */
43
+ get isPending(): boolean;
44
+ /**
45
+ * Schedule `task`, cancelling any previous task.
46
+ *
47
+ * @param task - The task to run once the delay elapses.
48
+ * @param delayMs - Wait for this task only. Defaults to the configured `delayMs`.
49
+ */
50
+ schedule(task: DebouncedTask, delayMs?: number): void;
51
+ /** Cancel the current task, if any. */
52
+ cancel(): void;
53
+ }
54
+ /**
55
+ * Throw a `RangeError` unless `delayMs` is a finite number >= 0.
56
+ *
57
+ * @internal Shared with `KeyedDebouncer`; not part of the public API.
58
+ */
59
+ export declare function assertValidDelay(delayMs: number): void;
60
+ //# sourceMappingURL=debouncer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debouncer.d.ts","sourceRoot":"","sources":["../../src/debouncer.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnE,6CAA6C;AAC7C,MAAM,WAAW,gBAAgB;IAC7B,gEAAgE;IAChE,OAAO,EAAE,MAAM,CAAC;IAChB,yFAAyF;IACzF,UAAU,CAAC,EAAE,MAAM,IAAI,CAAC;IACxB,iEAAiE;IACjE,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,iFAAiF;IACjF,SAAS,CAAC,EAAE,MAAM,IAAI,CAAC;IACvB,qGAAqG;IACrG,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAC;IACtB,kGAAkG;IAClG,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;IACnC,8DAA8D;IAC9D,MAAM,CAAC,EAAE,MAAM,IAAI,CAAC;CACvB;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,OAAO,OAAO,SAAS;;gBAUd,OAAO,EAAE,gBAAgB;IAWrC,4CAA4C;IAC5C,IAAI,SAAS,IAAI,OAAO,CAEvB;IAED;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,EAAE,aAAa,EAAE,OAAO,GAAE,MAAsB,GAAG,IAAI;IAapE,uCAAuC;IACvC,MAAM,IAAI,IAAI;CAkCjB;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAItD"}
@@ -1,11 +1,4 @@
1
1
  type AnyFunction = (this: any, ...args: any[]) => any;
2
- /** Options that control when a debounced function is executed. */
3
- export interface DebounceOptions {
4
- /** Execute on the first call in a debounce window. Defaults to false. */
5
- leading?: boolean;
6
- /** Execute after the debounce window using the latest arguments. Defaults to true. */
7
- trailing?: boolean;
8
- }
9
2
  /** Options that control when a throttled function is executed. */
10
3
  export interface ThrottleOptions {
11
4
  /** Execute immediately on the first call in a throttle window. Defaults to true. */
@@ -14,15 +7,15 @@ export interface ThrottleOptions {
14
7
  trailing?: boolean;
15
8
  }
16
9
  /**
17
- * A delayed function returned by {@link debounce} or {@link throttle}.
10
+ * A delayed function returned by {@link throttle}.
18
11
  *
19
12
  * @example
20
13
  * ```ts
21
- * const saveLater = debounce(save, 250);
22
- * saveLater();
14
+ * const refresh = throttle(loadData, 250);
15
+ * refresh();
23
16
  *
24
- * if (saveLater.pending()) {
25
- * saveLater.flush();
17
+ * if (refresh.pending()) {
18
+ * refresh.flush();
26
19
  * }
27
20
  * ```
28
21
  */
@@ -133,29 +126,6 @@ export declare function once<F extends AnyFunction>(fn: F): F;
133
126
  * ```
134
127
  */
135
128
  export declare function memoize<F extends AnyFunction, K = Parameters<F>[0]>(fn: F, resolver?: (...args: Parameters<F>) => K, cache?: Map<K, ReturnType<F>>): MemoizedFunction<F, K>;
136
- /**
137
- * Delay a function until no calls have been made for the configured wait time.
138
- * The latest arguments and this-context are used for the trailing execution.
139
- *
140
- * @example
141
- * ```ts
142
- * const search = debounce((query: string) => {
143
- * console.log(query);
144
- * }, 300);
145
- *
146
- * search("com");
147
- * search("commons"); // only this value is printed after 300 ms
148
- * ```
149
- *
150
- * @example
151
- * ```ts
152
- * const track = debounce(sendAnalytics, 1000, {leading: true, trailing: false});
153
- *
154
- * track("open"); // executed immediately
155
- * track.cancel(); // cancel the pending debounce window
156
- * ```
157
- */
158
- export declare function debounce<F extends AnyFunction>(fn: F, wait: number, options?: DebounceOptions): DeferredFunction<F>;
159
129
  /**
160
130
  * Limit how often a function can run during a time window.
161
131
  * Calls made while the function is throttled can optionally be replayed once at the end with the latest arguments.
@@ -1 +1 @@
1
- {"version":3,"file":"functions.d.ts","sourceRoot":"","sources":["../../src/functions.ts"],"names":[],"mappings":"AAAA,KAAK,WAAW,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;AAGtD,kEAAkE;AAClE,MAAM,WAAW,eAAe;IAC5B,yEAAyE;IACzE,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,sFAAsF;IACtF,QAAQ,CAAC,EAAE,OA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E,CAAC,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACpG,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC"}
@@ -12,7 +12,13 @@ export { default as Order } from './order.js';
12
12
  export { default as Timer } from './timer.js';
13
13
  export { default as TimeSpan } from './timespan.js';
14
14
  export { default as Stopwatch } from './stopwatch.js';
15
+ export type { Result } from './result.js';
16
+ export { TimeTrigger, CountTrigger } from './triggers.js';
15
17
  export { default as AsyncPoolExecutor } from './async-pool-executor.js';
18
+ export { default as Debouncer } from './debouncer.js';
19
+ export type { DebouncedTask, DebouncerOptions } from './debouncer.js';
20
+ export { default as KeyedDebouncer } from './keyed-debouncer.js';
21
+ export type { KeyedDebouncerOptions } from './keyed-debouncer.js';
16
22
  export { default as Lazy } from './lazy.js';
17
23
  export { default as from } from './collections/from.js';
18
24
  export { EventBus, Event } from './event-bus.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAC,OAAO,IAAI,IAAI,EAAC,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAC,OAAO,IAAI,QAAQ,EAAC,MA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AC,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAG5C,OAAO,EAAC,OAAO,IAAI,KAAK,EAAC,MAAM,YAAY,CAAC;AAG5C,OAAO,EAAC,OAAO,IAAI,oBAAoB,EAAC,MAAM,8BAA8B,CAAC;AAC7E,mBAAmB;AACnB,OAAO,EAAC,UAAU,EAAE,cAAc,EAAE,UAAU,EAAE,YAAY,EAAC,MAAM,oBAAoB,CAAC;AAKxF,mBAAmB;AACnB,cAAc,YAAY,CAAC;AAC3B,mBAAmB;AACnB,cAAc,wBAAwB,CAAC"}
@@ -0,0 +1,56 @@
1
+ import { type DebouncedTask } from "./debouncer.js";
2
+ /** Options accepted by {@link KeyedDebouncer}. Every callback receives the affected key. */
3
+ export interface KeyedDebouncerOptions<K> {
4
+ /** Default wait, in milliseconds, before a task is executed. */
5
+ delayMs: number;
6
+ /** Called every time a task is scheduled for `key`, after any previous task of that key has been cancelled. */
7
+ onSchedule?: (key: K) => void;
8
+ /** Called when the wait elapses and the task of `key` is about to run. */
9
+ onStart?: (key: K) => void;
10
+ /** Called when the task of `key` completes without throwing and without being cancelled. */
11
+ onSuccess?: (key: K) => void;
12
+ /** Called when a live task of `key` is cancelled, either explicitly or by being replaced. */
13
+ onCancel?: (key: K) => void;
14
+ /** Receives task errors. Cancellations never reach this callback. Defaults to `console.error`. */
15
+ onError?: (error: unknown, key: K) => void;
16
+ /** Called when `key` has no more pending work, after it has been removed from the debouncer. */
17
+ onIdle?: (key: K) => void;
18
+ }
19
+ /**
20
+ * An independent {@link Debouncer} per key.
21
+ * Debouncers only exist for keys with pending work: each one is created when
22
+ * its first task is scheduled and removed as soon as it becomes idle.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const debouncer = new KeyedDebouncer<string>({delayMs: 1500});
27
+ *
28
+ * debouncer.schedule(chatId, async (signal) => {
29
+ * const pending = await store.getPending(chatId);
30
+ * const reply = await callAI(pending, {signal});
31
+ * if (signal.aborted) return;
32
+ * await sendReply(chatId, reply);
33
+ * });
34
+ * ```
35
+ */
36
+ export default class KeyedDebouncer<K = string> {
37
+ #private;
38
+ constructor(options: KeyedDebouncerOptions<K>);
39
+ /** Number of keys with a task waiting or running. */
40
+ get size(): number;
41
+ /**
42
+ * Schedule `task` for `key`, cancelling any previous task of that key.
43
+ *
44
+ * @param key - The key whose task is replaced.
45
+ * @param task - The task to run once the delay elapses.
46
+ * @param delayMs - Wait for this task only. Defaults to the configured `delayMs`.
47
+ */
48
+ schedule(key: K, task: DebouncedTask, delayMs?: number): void;
49
+ /** Cancel the task of `key`, if any. */
50
+ cancel(key: K): void;
51
+ /** Cancel every task. Useful when shutting the process down. */
52
+ cancelAll(): void;
53
+ /** Whether a task is waiting or running for `key`. */
54
+ isPending(key: K): boolean;
55
+ }
56
+ //# sourceMappingURL=keyed-debouncer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keyed-debouncer.d.ts","sourceRoot":"","sources":["../../src/keyed-debouncer.ts"],"names":[],"mappings":"AAAA,OAAkB,EAAmB,KAAK,aAAa,EAAC,MAAM,gBAAgB,CAAC;AAG/E,4FAA4F;AAC5F,MAAM,WAAW,qBAAqB,CAAC,CAAC;IACpC,gEAAgE;IAChE,OAAO,EAAE,MAAM,CAAC;IAChB,+GAA+G;IAC/G,UAAU,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,CAAC;IAC9B,0EAA0E;IAC1E,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,CAAC;IAC3B,4FAA4F;IAC5F,SAAS,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,CAAC;IAC7B,6FAA6F;IAC7F,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,CAAC;IAC5B,kGAAkG;IAClG,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,KAAK,IAAI,CAAC;IAC3C,gGAAgG;IAChG,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,CAAC;CAC7B;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,OAAO,OAAO,cAAc,CAAC,CAAC,GAAG,MAAM;;gBAU9B,OAAO,EAAE,qBAAqB,CAAC,CAAC,CAAC;IAW7C,qDAAqD;IACrD,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;;;;;OAMG;IACH,QAAQ,CAAC,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI;IAc7D,wCAAwC;IACxC,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,IAAI;IAIpB,gEAAgE;IAChE,SAAS,IAAI,IAAI;IAOjB,sDAAsD;IACtD,SAAS,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO;CAqB7B"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Represents success and failure outputs narrowed by `success`.
3
+ */
4
+ export type Result<TSuccessOutput, TFailureOutput = undefined> = {
5
+ readonly success: true;
6
+ readonly output: TSuccessOutput;
7
+ } | {
8
+ readonly success: false;
9
+ readonly output?: TFailureOutput;
10
+ };
11
+ //# sourceMappingURL=result.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../../src/result.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,MAAM,CAAC,cAAc,EAAE,cAAc,GAAG,SAAS,IACvD;IAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAA;CAAC,GACzD;IAAC,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,cAAc,CAAA;CAAC,CAAC"}
@@ -0,0 +1,64 @@
1
+ import type { Result } from './result.js';
2
+ import TimeSpan from './timespan.js';
3
+ import type { Supplier } from './types.js';
4
+ /**
5
+ * Runs a callback at most once during a configured time interval.
6
+ */
7
+ export declare class TimeTrigger<T> {
8
+ private callback;
9
+ private interval;
10
+ private lastRunTime;
11
+ constructor(callback: Supplier<T | Promise<T>>, interval: TimeSpan);
12
+ /**
13
+ * Executes the callback when the configured interval has elapsed.
14
+ */
15
+ check(): Promise<Result<T>>;
16
+ /**
17
+ * Executes the callback regardless of the configured interval.
18
+ */
19
+ trigger(): Promise<T>;
20
+ /**
21
+ * Replaces the callback executed by this trigger.
22
+ */
23
+ setCallback(callback: Supplier<T | Promise<T>>): void;
24
+ /**
25
+ * Gets the configured time interval.
26
+ */
27
+ getInterval(): TimeSpan;
28
+ /**
29
+ * Replaces the configured time interval.
30
+ */
31
+ setInterval(interval: TimeSpan): void;
32
+ }
33
+ /**
34
+ * Runs a callback after a configured number of checks.
35
+ */
36
+ export declare class CountTrigger<T> {
37
+ private callback;
38
+ private interval;
39
+ private checkCount;
40
+ constructor(callback: Supplier<T | Promise<T>>, interval: number);
41
+ /**
42
+ * Executes the callback when the configured number of checks is reached.
43
+ */
44
+ check(): Promise<Result<T>>;
45
+ /**
46
+ * Executes the callback regardless of the configured check count.
47
+ */
48
+ trigger(): Promise<T>;
49
+ /**
50
+ * Replaces the callback executed by this trigger.
51
+ */
52
+ setCallback(callback: Supplier<T | Promise<T>>): void;
53
+ /**
54
+ * Gets the configured check interval.
55
+ */
56
+ getInterval(): number;
57
+ /**
58
+ * Replaces the configured check interval.
59
+ *
60
+ * @throws {RangeError} When the interval is less than one.
61
+ */
62
+ setInterval(interval: number): void;
63
+ }
64
+ //# sourceMappingURL=triggers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"triggers.d.ts","sourceRoot":"","sources":["../../src/triggers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AACxC,OAAO,QAAQ,MAAM,eAAe,CAAC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AI;CAO7C"}
@@ -85,10 +85,15 @@ export declare function shallowEqual(a: any, b: any): boolean;
85
85
  /**
86
86
  * Pauses the execution of an asynchronous function for a specified duration.
87
87
  *
88
- * @param {number} ms - The delay duration in milliseconds.
89
- * @return {Promise<void>} A promise that resolves after the specified delay.
88
+ * When `signal` is provided, the returned promise rejects with `signal.reason`
89
+ * as soon as the signal is aborted (or immediately if it is already aborted),
90
+ * and the pending timer is cleared.
91
+ *
92
+ * @param ms - The delay duration in milliseconds.
93
+ * @param signal - Optional signal that cancels the wait.
94
+ * @return A promise that resolves after the specified delay.
90
95
  */
91
- export declare function delay(ms: number): Promise<void>;
96
+ export declare function delay(ms: number, signal?: AbortSignal): Promise<void>;
92
97
  /**
93
98
  * Checks if a value is undefined.
94
99
  *
@@ -1 +1 @@
1
- {"version":3,"file":"utilities.d.ts","sourceRoot":"","sources":["../../src/utilities.ts"],"names":[],"mappings":"AAGA,OAAO,YAAY,MAAM,qBAAqB,CAAC;AAC/C,OAAO,KAAK,EAAC,UAAU,EAAE,WAAW,EAAC,MAAM,YAAY,CAAC;AACxD,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,wBAAgB,IAAI,CAAC,CAAC,SAAS,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,CA0C1D;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAE5C;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,MAAM,CAEpD;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,MAAM,CAEpD;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,OAAO,CAEtD;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI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G;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI,CAGxC;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,WAAW,EAC7C,MAAM,EAAE,CAAC,EACT,MAAM,EAAE,GAAG,EACX,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GACvB,OAAO,CA+BT"}
@@ -39,9 +39,7 @@ export default class CacheDictionary {
39
39
  delete this.expiration[key];
40
40
  }
41
41
  else {
42
- const expiration = new Date();
43
- expiration.setSeconds(expiration.getSeconds() + duration);
44
- this.expiration[key] = expiration;
42
+ this.expiration[key] = this.calculateExpiration(duration);
45
43
  }
46
44
  }
47
45
  /**
@@ -95,9 +93,7 @@ export default class CacheDictionary {
95
93
  if (duration === null) {
96
94
  return undefined;
97
95
  }
98
- const expiration = new Date();
99
- expiration.setSeconds(expiration.getSeconds() + duration);
100
- return expiration;
96
+ return new Date(Date.now() + duration * 1000);
101
97
  }
102
98
  }
103
99
  //# sourceMappingURL=cache-dictionary.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"cache-dictionary.js","sourceRoot":"","sources":["../../../src/collections/cache-dictionary.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,eAAe;IAMhC;;;OAGG;IACH,YAAY,WAAwC,IAAI,EAAE,kBAAiC,IAAI;QAC3F,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QACvC,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;IAED;;;;OAIG;IACI,aAAa,CAAC,GAAW;QAC5B,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAED;;;;OAIG;IACI,a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IAAI;QAC5D,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QACxB,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,mBAAmB,CAAC,QAAQ,CAAC,CAAC;IAC9D,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,GAAW;QACrB,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACvB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAEO,SAAS,CAAC,GAAW;QACzB,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,UAAU,YAAY,IAAI,EAAE,CAAC;YAC7B,OAAO,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC;QACnC,CAAC;QACD,OAAO,KAAK,CAAC;IACjB,CAAC;IAEO,mBAAmB,CAAC,QAAuB;QAC/C,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;YACpB,OAAO,SAAS,CAAC;QACrB,CAAC;QAED,MAAM,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC;QAC9B,UAAU,CAAC,UAAU,CAAC,UAAU,CAAC,UAAU,EAAE,GAAG,QAAQ,CAAC,CAAC;QAC1D,OAAO,UAAU,CAAC;IACtB,CAAC;CACJ","sourcesContent":["/**\n * A dictionary-like structure with caching and expiration capabilities.\n * @template T - The type of the values stored in the cache.\n */\nexport default class CacheDictionary<T> {\n private readonly fallback: ((key: string) => T) | null;\n private readonly cache: { [key: string]: T };\n private readonly defaultDuration: number | null;\n private readonly expiration: { [key: string]: Date | undefined };\n\n /**\n * @param {((key: string) => T) | null} fallback - Optional function to call when a key is not found.\n * @param {number | null} defaultDuration - Default duration in seconds for cache items.\n */\n constructor(fallback: ((key: string) => T) | null = null, defaultDuration: number | null = null) {\n this.fallback = fallback;\n this.cache = Object.create(null);\n this.defaultDuration = defaultDuration;\n this.expiration = Object.create(null);\n }\n\n /**\n * Gets the expiration date for a specific key.\n * @param {string} key - The key.\n * @returns {Date | undefined} The expiration date or undefined.\n */\n public getExpiration(key: string): Date | undefined {\n return this.expiration[key];\n }\n\n /**\n * Sets a specific expiration date for a key.\n * @param {string} key - The key.\n * @param {Date | undefined} expiration - The expiration date.\n */\n public setExpiration(key: string, expiration: Date | undefined): void {\n this.expiration[key] = expiration;\n }\n\n /**\n * Sets a duration in seconds for a key's expiration.\n * @param {string} key - The key.\n * @param {number | null} duration - Duration in seconds. If null, expiration is removed.\n */\n public setDuration(key: string, duration: number | null): void {\n if (duration === null) {\n delete this.expiration[key];\n } else {\n const expiration = new Date();\n expiration.setSeconds(expiration.getSeconds() + duration);\n this.expiration[key] = expiration;\n }\n }\n\n /**\n * Gets a value from the cache. If not found and a fallback exists, it uses the fallback.\n * If the item is expired, it's removed and null is returned.\n * @param {string} key - The key to retrieve.\n * @returns {T | null} The value or null if not found/expired.\n */\n public get(key: string): T | null {\n if (!Object.prototype.hasOwnProperty.call(this.cache, key)) {\n if (this.fallback) {\n this.cache[key] = this.fallback(key);\n this.expiration[key] = this.calculateExpiration(this.defaultDuration);\n } else {\n return null;\n }\n }\n\n if (this.isExpired(key)) {\n this.remove(key);\n return null;\n }\n\n return this.cache[key];\n }\n\n /**\n * Sets a value in the cache with an optional duration.\n * @param {string} key - The key.\n * @param {T} value - The value to store.\n * @param {number | null} duration - Duration in seconds.\n */\n public set(key: string, value: T, duration: number | null = null): void {\n this.cache[key] = value;\n this.expiration[key] = this.calculateExpiration(duration);\n }\n\n /**\n * Removes an item from the cache.\n * @param {string} key - The key to remove.\n */\n public remove(key: string): void {\n delete this.cache[key];\n delete this.expiration[key];\n }\n\n private isExpired(key: string): boolean {\n const expiration = this.expiration[key];\n if (expiration instanceof Date) {\n return expiration < new Date();\n }\n return false;\n }\n\n private calculateExpiration(duration: number | null): Date | undefined {\n if (duration === null) {\n return undefined;\n }\n\n const expiration = new Date();\n expiration.setSeconds(expiration.getSeconds() + duration);\n return expiration;\n }\n}"]}
1
+ {"version":3,"file":"cache-dictionary.js","sourceRoot":"","sources":["../../../src/collections/cache-dictionary.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,eAAe;IAMhC;;;OAGG;IACH,YAAY,WAAwC,IAAI,EAAE,kBAAiC,IAAI;QAC3F,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QACvC,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;IAED;;;;OAIG;IACI,aAAa,CAAC,GAAW;QAC5B,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAED;;;;OAIG;IACI,aAAa,CAAC,GAAW,EAAE,UAA4B;QAC1D,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC;IACtC,CAAC;IAED;;;;OAIG;IACI,WAAW,CAAC,GAAW,EAAE,QAAuB;QACnD,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;YACpB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QAChC,CAAC;aAAM,CAAC;YACJ,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,mBAAmB,CAAC,QAAQ,CAAC,CAAC;QAC9D,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACI,GAAG,CAAC,GAAW;QAClB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,CAAC;YACzD,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;gBAChB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,IAAI,CAA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I,CAAC,CAAC;IAClD,CAAC;CACJ","sourcesContent":["/**\n * A dictionary-like structure with caching and expiration capabilities.\n * @template T - The type of the values stored in the cache.\n */\nexport default class CacheDictionary<T> {\n private readonly fallback: ((key: string) => T) | null;\n private readonly cache: { [key: string]: T };\n private readonly defaultDuration: number | null;\n private readonly expiration: { [key: string]: Date | undefined };\n\n /**\n * @param {((key: string) => T) | null} fallback - Optional function to call when a key is not found.\n * @param {number | null} defaultDuration - Default duration in seconds for cache items.\n */\n constructor(fallback: ((key: string) => T) | null = null, defaultDuration: number | null = null) {\n this.fallback = fallback;\n this.cache = Object.create(null);\n this.defaultDuration = defaultDuration;\n this.expiration = Object.create(null);\n }\n\n /**\n * Gets the expiration date for a specific key.\n * @param {string} key - The key.\n * @returns {Date | undefined} The expiration date or undefined.\n */\n public getExpiration(key: string): Date | undefined {\n return this.expiration[key];\n }\n\n /**\n * Sets a specific expiration date for a key.\n * @param {string} key - The key.\n * @param {Date | undefined} expiration - The expiration date.\n */\n public setExpiration(key: string, expiration: Date | undefined): void {\n this.expiration[key] = expiration;\n }\n\n /**\n * Sets a duration in seconds for a key's expiration.\n * @param {string} key - The key.\n * @param {number | null} duration - Duration in seconds. If null, expiration is removed.\n */\n public setDuration(key: string, duration: number | null): void {\n if (duration === null) {\n delete this.expiration[key];\n } else {\n this.expiration[key] = this.calculateExpiration(duration);\n }\n }\n\n /**\n * Gets a value from the cache. If not found and a fallback exists, it uses the fallback.\n * If the item is expired, it's removed and null is returned.\n * @param {string} key - The key to retrieve.\n * @returns {T | null} The value or null if not found/expired.\n */\n public get(key: string): T | null {\n if (!Object.prototype.hasOwnProperty.call(this.cache, key)) {\n if (this.fallback) {\n this.cache[key] = this.fallback(key);\n this.expiration[key] = this.calculateExpiration(this.defaultDuration);\n } else {\n return null;\n }\n }\n\n if (this.isExpired(key)) {\n this.remove(key);\n return null;\n }\n\n return this.cache[key];\n }\n\n /**\n * Sets a value in the cache with an optional duration.\n * @param {string} key - The key.\n * @param {T} value - The value to store.\n * @param {number | null} duration - Duration in seconds.\n */\n public set(key: string, value: T, duration: number | null = null): void {\n this.cache[key] = value;\n this.expiration[key] = this.calculateExpiration(duration);\n }\n\n /**\n * Removes an item from the cache.\n * @param {string} key - The key to remove.\n */\n public remove(key: string): void {\n delete this.cache[key];\n delete this.expiration[key];\n }\n\n private isExpired(key: string): boolean {\n const expiration = this.expiration[key];\n if (expiration instanceof Date) {\n return expiration < new Date();\n }\n return false;\n }\n\n private calculateExpiration(duration: number | null): Date | undefined {\n if (duration === null) {\n return undefined;\n }\n\n return new Date(Date.now() + duration * 1000);\n }\n}"]}
@@ -0,0 +1,118 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _Debouncer_instances, _Debouncer_controller, _Debouncer_delayMs, _Debouncer_onSchedule, _Debouncer_onStart, _Debouncer_onSuccess, _Debouncer_onCancel, _Debouncer_onError, _Debouncer_onIdle, _Debouncer_run, _Debouncer_becomeIdle;
13
+ import { delay } from "./utilities.js";
14
+ import { noop } from "./functions.js";
15
+ /**
16
+ * Keeps at most one live task.
17
+ * Scheduling a new task cancels the previous one, whether it was still
18
+ * waiting or already running.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * const debouncer = new Debouncer({delayMs: 1500});
23
+ *
24
+ * debouncer.schedule(async (signal) => {
25
+ * const reply = await callAI(messages, {signal});
26
+ * if (signal.aborted) return;
27
+ * await sendReply(reply);
28
+ * });
29
+ * ```
30
+ */
31
+ class Debouncer {
32
+ constructor(options) {
33
+ _Debouncer_instances.add(this);
34
+ _Debouncer_controller.set(this, void 0);
35
+ _Debouncer_delayMs.set(this, void 0);
36
+ _Debouncer_onSchedule.set(this, void 0);
37
+ _Debouncer_onStart.set(this, void 0);
38
+ _Debouncer_onSuccess.set(this, void 0);
39
+ _Debouncer_onCancel.set(this, void 0);
40
+ _Debouncer_onError.set(this, void 0);
41
+ _Debouncer_onIdle.set(this, void 0);
42
+ assertValidDelay(options.delayMs);
43
+ __classPrivateFieldSet(this, _Debouncer_delayMs, options.delayMs, "f");
44
+ __classPrivateFieldSet(this, _Debouncer_onSchedule, options.onSchedule ?? noop, "f");
45
+ __classPrivateFieldSet(this, _Debouncer_onStart, options.onStart ?? noop, "f");
46
+ __classPrivateFieldSet(this, _Debouncer_onSuccess, options.onSuccess ?? noop, "f");
47
+ __classPrivateFieldSet(this, _Debouncer_onCancel, options.onCancel ?? noop, "f");
48
+ __classPrivateFieldSet(this, _Debouncer_onError, options.onError ?? console.error, "f");
49
+ __classPrivateFieldSet(this, _Debouncer_onIdle, options.onIdle ?? noop, "f");
50
+ }
51
+ /** Whether a task is waiting or running. */
52
+ get isPending() {
53
+ return __classPrivateFieldGet(this, _Debouncer_controller, "f") !== undefined;
54
+ }
55
+ /**
56
+ * Schedule `task`, cancelling any previous task.
57
+ *
58
+ * @param task - The task to run once the delay elapses.
59
+ * @param delayMs - Wait for this task only. Defaults to the configured `delayMs`.
60
+ */
61
+ schedule(task, delayMs = __classPrivateFieldGet(this, _Debouncer_delayMs, "f")) {
62
+ assertValidDelay(delayMs);
63
+ if (__classPrivateFieldGet(this, _Debouncer_controller, "f") !== undefined) {
64
+ __classPrivateFieldGet(this, _Debouncer_controller, "f").abort();
65
+ __classPrivateFieldGet(this, _Debouncer_onCancel, "f").call(this);
66
+ }
67
+ const controller = new AbortController();
68
+ __classPrivateFieldSet(this, _Debouncer_controller, controller, "f");
69
+ __classPrivateFieldGet(this, _Debouncer_onSchedule, "f").call(this);
70
+ void __classPrivateFieldGet(this, _Debouncer_instances, "m", _Debouncer_run).call(this, task, delayMs, controller);
71
+ }
72
+ /** Cancel the current task, if any. */
73
+ cancel() {
74
+ if (__classPrivateFieldGet(this, _Debouncer_controller, "f") === undefined) {
75
+ return;
76
+ }
77
+ __classPrivateFieldGet(this, _Debouncer_controller, "f").abort();
78
+ __classPrivateFieldGet(this, _Debouncer_onCancel, "f").call(this);
79
+ __classPrivateFieldGet(this, _Debouncer_instances, "m", _Debouncer_becomeIdle).call(this);
80
+ }
81
+ }
82
+ _Debouncer_controller = new WeakMap(), _Debouncer_delayMs = new WeakMap(), _Debouncer_onSchedule = new WeakMap(), _Debouncer_onStart = new WeakMap(), _Debouncer_onSuccess = new WeakMap(), _Debouncer_onCancel = new WeakMap(), _Debouncer_onError = new WeakMap(), _Debouncer_onIdle = new WeakMap(), _Debouncer_instances = new WeakSet(), _Debouncer_run = async function _Debouncer_run(task, delayMs, controller) {
83
+ const signal = controller.signal;
84
+ try {
85
+ await delay(delayMs, signal);
86
+ __classPrivateFieldGet(this, _Debouncer_onStart, "f").call(this);
87
+ await task(signal);
88
+ if (!signal.aborted) {
89
+ __classPrivateFieldGet(this, _Debouncer_onSuccess, "f").call(this);
90
+ }
91
+ }
92
+ catch (error) {
93
+ if (!signal.aborted) {
94
+ __classPrivateFieldGet(this, _Debouncer_onError, "f").call(this, error);
95
+ }
96
+ }
97
+ finally {
98
+ // A replaced or cancelled task must not touch the current state.
99
+ if (__classPrivateFieldGet(this, _Debouncer_controller, "f") === controller) {
100
+ __classPrivateFieldGet(this, _Debouncer_instances, "m", _Debouncer_becomeIdle).call(this);
101
+ }
102
+ }
103
+ }, _Debouncer_becomeIdle = function _Debouncer_becomeIdle() {
104
+ __classPrivateFieldSet(this, _Debouncer_controller, undefined, "f");
105
+ __classPrivateFieldGet(this, _Debouncer_onIdle, "f").call(this);
106
+ };
107
+ export default Debouncer;
108
+ /**
109
+ * Throw a `RangeError` unless `delayMs` is a finite number >= 0.
110
+ *
111
+ * @internal Shared with `KeyedDebouncer`; not part of the public API.
112
+ */
113
+ export function assertValidDelay(delayMs) {
114
+ if (!Number.isFinite(delayMs) || delayMs < 0) {
115
+ throw new RangeError(`delayMs must be a number >= 0 (received: ${delayMs})`);
116
+ }
117
+ }
118
+ //# sourceMappingURL=debouncer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debouncer.js","sourceRoot":"","sources":["../../src/debouncer.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAC,IAAI,EAAC,MAAM,gBAAgB,CAAC;AA0BpC;;;;;;;;;;;;;;;GAeG;AACH,MAAqB,SAAS;IAU1B,YAAY,OAAyB;;QATrC,wCAAyC;QAChC,qCAAiB;QACjB,wCAAwB;QACxB,qCAAqB;QACrB,uCAAuB;QACvB,sCAAsB;QACtB,qCAAmC;QACnC,oCAAoB;QAGzB,gBAAgB,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAClC,uBAAA,IAAI,sBAAY,OAAO,CAAC,OAAO,MAAA,CAAC;QAChC,uBAAA,IAAI,yBAAe,OAAO,CAAC,UAAU,IAAI,IAAI,MAAA,CAAC;QAC9C,uBAAA,IAAI,sBAAY,OAAO,CAAC,OAAO,IAAI,IAAI,MAAA,CAAC;QACxC,uBAAA,IAAI,wBAAc,OAAO,CAAC,SAAS,IAAI,IAAI,MAAA,CAAC;QAC5C,uBAAA,IAAI,uBAAa,OAAO,CAAC,QAAQ,IAAI,IAAI,MAAA,CAAC;QAC1C,uBAAA,IAAI,sBAAY,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,KAAK,M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M,IAAI,CAAC,MAAM,CAAC,CAAC;QACnB,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YAClB,uBAAA,IAAI,4BAAW,MAAf,IAAI,CAAa,CAAC;QACtB,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YAClB,uBAAA,IAAI,0BAAS,MAAb,IAAI,EAAU,KAAK,CAAC,CAAC;QACzB,CAAC;IACL,CAAC;YAAS,CAAC;QACP,iEAAiE;QACjE,IAAI,uBAAA,IAAI,6BAAY,KAAK,UAAU,EAAE,CAAC;YAClC,uBAAA,IAAI,mDAAY,MAAhB,IAAI,CAAc,CAAC;QACvB,CAAC;IACL,CAAC;AACL,CAAC;IAGG,uBAAA,IAAI,yBAAe,SAAS,MAAA,CAAC;IAC7B,uBAAA,IAAI,yBAAQ,MAAZ,IAAI,CAAU,CAAC;AACnB,CAAC;eA/EgB,SAAS;AAkF9B;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAe;IAC5C,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,OAAO,GAAG,CAAC,EAAE,CAAC;QAC3C,MAAM,IAAI,UAAU,CAAC,4CAA4C,OAAO,GAAG,CAAC,CAAC;IACjF,CAAC;AACL,CAAC","sourcesContent":["import {delay} from \"./utilities.js\";\nimport {noop} from \"./functions.js\";\n\n/**\n * Cancellable task. It must honour `signal`: pass it to fetch/SDKs and check\n * `signal.aborted` before producing irreversible side effects.\n */\nexport type DebouncedTask = (signal: AbortSignal) => Promise<void>;\n\n/** Options accepted by {@link Debouncer}. */\nexport interface DebouncerOptions {\n /** Default wait, in milliseconds, before a task is executed. */\n delayMs: number;\n /** Called every time a task is scheduled, after any previous task has been cancelled. */\n onSchedule?: () => void;\n /** Called when the wait elapses and the task is about to run. */\n onStart?: () => void;\n /** Called when a task completes without throwing and without being cancelled. */\n onSuccess?: () => void;\n /** Called when a live task is cancelled, either by {@link Debouncer.cancel} or by being replaced. */\n onCancel?: () => void;\n /** Receives task errors. Cancellations never reach this callback. Defaults to `console.error`. */\n onError?: (error: unknown) => void;\n /** Called every time the debouncer goes from busy to idle. */\n onIdle?: () => void;\n}\n\n/**\n * Keeps at most one live task.\n * Scheduling a new task cancels the previous one, whether it was still\n * waiting or already running.\n *\n * @example\n * ```ts\n * const debouncer = new Debouncer({delayMs: 1500});\n *\n * debouncer.schedule(async (signal) => {\n * const reply = await callAI(messages, {signal});\n * if (signal.aborted) return;\n * await sendReply(reply);\n * });\n * ```\n */\nexport default class Debouncer {\n #controller: AbortController | undefined;\n readonly #delayMs: number;\n readonly #onSchedule: () => void;\n readonly #onStart: () => void;\n readonly #onSuccess: () => void;\n readonly #onCancel: () => void;\n readonly #onError: (error: unknown) => void;\n readonly #onIdle: () => void;\n\n constructor(options: DebouncerOptions) {\n assertValidDelay(options.delayMs);\n this.#delayMs = options.delayMs;\n this.#onSchedule = options.onSchedule ?? noop;\n this.#onStart = options.onStart ?? noop;\n this.#onSuccess = options.onSuccess ?? noop;\n this.#onCancel = options.onCancel ?? noop;\n this.#onError = options.onError ?? console.error;\n this.#onIdle = options.onIdle ?? noop;\n }\n\n /** Whether a task is waiting or running. */\n get isPending(): boolean {\n return this.#controller !== undefined;\n }\n\n /**\n * Schedule `task`, cancelling any previous task.\n *\n * @param task - The task to run once the delay elapses.\n * @param delayMs - Wait for this task only. Defaults to the configured `delayMs`.\n */\n schedule(task: DebouncedTask, delayMs: number = this.#delayMs): void {\n assertValidDelay(delayMs);\n if (this.#controller !== undefined) {\n this.#controller.abort();\n this.#onCancel();\n }\n\n const controller = new AbortController();\n this.#controller = controller;\n this.#onSchedule();\n void this.#run(task, delayMs, controller);\n }\n\n /** Cancel the current task, if any. */\n cancel(): void {\n if (this.#controller === undefined) {\n return;\n }\n this.#controller.abort();\n this.#onCancel();\n this.#becomeIdle();\n }\n\n async #run(task: DebouncedTask, delayMs: number, controller: AbortController): Promise<void> {\n const signal = controller.signal;\n try {\n await delay(delayMs, signal);\n this.#onStart();\n await task(signal);\n if (!signal.aborted) {\n this.#onSuccess();\n }\n } catch (error) {\n if (!signal.aborted) {\n this.#onError(error);\n }\n } finally {\n // A replaced or cancelled task must not touch the current state.\n if (this.#controller === controller) {\n this.#becomeIdle();\n }\n }\n }\n\n #becomeIdle(): void {\n this.#controller = undefined;\n this.#onIdle();\n }\n}\n\n/**\n * Throw a `RangeError` unless `delayMs` is a finite number >= 0.\n *\n * @internal Shared with `KeyedDebouncer`; not part of the public API.\n */\nexport function assertValidDelay(delayMs: number): void {\n if (!Number.isFinite(delayMs) || delayMs < 0) {\n throw new RangeError(`delayMs must be a number >= 0 (received: ${delayMs})`);\n }\n}\n"]}
@@ -113,91 +113,6 @@ export function memoize(fn, resolver, cache = new Map()) {
113
113
  memoized.has = (...args) => cache.has(resolveKey(...args));
114
114
  return memoized;
115
115
  }
116
- /**
117
- * Delay a function until no calls have been made for the configured wait time.
118
- * The latest arguments and this-context are used for the trailing execution.
119
- *
120
- * @example
121
- * ```ts
122
- * const search = debounce((query: string) => {
123
- * console.log(query);
124
- * }, 300);
125
- *
126
- * search("com");
127
- * search("commons"); // only this value is printed after 300 ms
128
- * ```
129
- *
130
- * @example
131
- * ```ts
132
- * const track = debounce(sendAnalytics, 1000, {leading: true, trailing: false});
133
- *
134
- * track("open"); // executed immediately
135
- * track.cancel(); // cancel the pending debounce window
136
- * ```
137
- */
138
- export function debounce(fn, wait, options = {}) {
139
- const delay = normalizeDelay(wait);
140
- const leading = options.leading ?? false;
141
- const trailing = options.trailing ?? true;
142
- let timer;
143
- let lastArgs;
144
- let lastThis;
145
- let result;
146
- const invoke = () => {
147
- const args = lastArgs;
148
- const context = lastThis;
149
- lastArgs = undefined;
150
- lastThis = undefined;
151
- result = fn.apply(context, args);
152
- return result;
153
- };
154
- const expire = () => {
155
- timer = undefined;
156
- if (trailing && lastArgs) {
157
- invoke();
158
- }
159
- else {
160
- lastArgs = undefined;
161
- lastThis = undefined;
162
- }
163
- };
164
- const start = () => {
165
- timer = setTimeout(expire, delay);
166
- };
167
- const debounced = function (...args) {
168
- const invokeLeading = leading && timer === undefined;
169
- lastArgs = args;
170
- lastThis = this;
171
- if (timer !== undefined) {
172
- clearTimeout(timer);
173
- }
174
- start();
175
- if (invokeLeading) {
176
- return invoke();
177
- }
178
- return result;
179
- };
180
- debounced.cancel = () => {
181
- if (timer !== undefined) {
182
- clearTimeout(timer);
183
- }
184
- timer = undefined;
185
- lastArgs = undefined;
186
- lastThis = undefined;
187
- };
188
- debounced.flush = () => {
189
- if (timer !== undefined) {
190
- clearTimeout(timer);
191
- timer = undefined;
192
- if (lastArgs && trailing) {
193
- return invoke();
194
- }
195
- }
196
- return result;
197
- };
198
- debounced.pending = () => timer !== undefined;
199
- return debounced;
200
- }
201
116
  /**
202
117
  * Limit how often a function can run during a time window.
203
118
  * Calls made while the function is throttled can optionally be replayed once at the end with the latest arguments.