@tanstack/solid-pacer 0.16.1 → 0.18.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.
Files changed (106) hide show
  1. package/README.md +1 -1
  2. package/dist/async-batcher/createAsyncBatcher.cjs +26 -12
  3. package/dist/async-batcher/createAsyncBatcher.cjs.map +1 -1
  4. package/dist/async-batcher/createAsyncBatcher.d.cts +36 -9
  5. package/dist/async-batcher/createAsyncBatcher.d.ts +36 -9
  6. package/dist/async-batcher/createAsyncBatcher.js +22 -8
  7. package/dist/async-batcher/createAsyncBatcher.js.map +1 -1
  8. package/dist/async-batcher/index.cjs +3 -3
  9. package/dist/async-debouncer/createAsyncDebouncer.cjs +26 -12
  10. package/dist/async-debouncer/createAsyncDebouncer.cjs.map +1 -1
  11. package/dist/async-debouncer/createAsyncDebouncer.d.cts +36 -9
  12. package/dist/async-debouncer/createAsyncDebouncer.d.ts +36 -9
  13. package/dist/async-debouncer/createAsyncDebouncer.js +22 -8
  14. package/dist/async-debouncer/createAsyncDebouncer.js.map +1 -1
  15. package/dist/async-debouncer/index.cjs +3 -3
  16. package/dist/async-queuer/createAsyncQueuer.cjs +26 -12
  17. package/dist/async-queuer/createAsyncQueuer.cjs.map +1 -1
  18. package/dist/async-queuer/createAsyncQueuer.d.cts +36 -9
  19. package/dist/async-queuer/createAsyncQueuer.d.ts +36 -9
  20. package/dist/async-queuer/createAsyncQueuer.js +22 -8
  21. package/dist/async-queuer/createAsyncQueuer.js.map +1 -1
  22. package/dist/async-queuer/index.cjs +3 -3
  23. package/dist/async-rate-limiter/createAsyncRateLimiter.cjs +89 -30
  24. package/dist/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -1
  25. package/dist/async-rate-limiter/createAsyncRateLimiter.d.cts +99 -27
  26. package/dist/async-rate-limiter/createAsyncRateLimiter.d.ts +99 -27
  27. package/dist/async-rate-limiter/createAsyncRateLimiter.js +85 -26
  28. package/dist/async-rate-limiter/createAsyncRateLimiter.js.map +1 -1
  29. package/dist/async-rate-limiter/index.cjs +3 -3
  30. package/dist/async-throttler/createAsyncThrottler.cjs +26 -12
  31. package/dist/async-throttler/createAsyncThrottler.cjs.map +1 -1
  32. package/dist/async-throttler/createAsyncThrottler.d.cts +36 -9
  33. package/dist/async-throttler/createAsyncThrottler.d.ts +36 -9
  34. package/dist/async-throttler/createAsyncThrottler.js +22 -8
  35. package/dist/async-throttler/createAsyncThrottler.js.map +1 -1
  36. package/dist/async-throttler/index.cjs +3 -3
  37. package/dist/batcher/createBatcher.cjs +26 -12
  38. package/dist/batcher/createBatcher.cjs.map +1 -1
  39. package/dist/batcher/createBatcher.d.cts +36 -9
  40. package/dist/batcher/createBatcher.d.ts +36 -9
  41. package/dist/batcher/createBatcher.js +22 -8
  42. package/dist/batcher/createBatcher.js.map +1 -1
  43. package/dist/batcher/index.cjs +3 -3
  44. package/dist/debouncer/createDebouncer.cjs +27 -13
  45. package/dist/debouncer/createDebouncer.cjs.map +1 -1
  46. package/dist/debouncer/createDebouncer.d.cts +37 -10
  47. package/dist/debouncer/createDebouncer.d.ts +37 -10
  48. package/dist/debouncer/createDebouncer.js +23 -9
  49. package/dist/debouncer/createDebouncer.js.map +1 -1
  50. package/dist/debouncer/index.cjs +3 -3
  51. package/dist/index.cjs +3 -3
  52. package/dist/queuer/createQueuedSignal.cjs +8 -8
  53. package/dist/queuer/createQueuedSignal.cjs.map +1 -1
  54. package/dist/queuer/createQueuedSignal.d.cts +8 -8
  55. package/dist/queuer/createQueuedSignal.d.ts +8 -8
  56. package/dist/queuer/createQueuedSignal.js +8 -8
  57. package/dist/queuer/createQueuedSignal.js.map +1 -1
  58. package/dist/queuer/createQueuer.cjs +26 -12
  59. package/dist/queuer/createQueuer.cjs.map +1 -1
  60. package/dist/queuer/createQueuer.d.cts +36 -9
  61. package/dist/queuer/createQueuer.d.ts +36 -9
  62. package/dist/queuer/createQueuer.js +22 -8
  63. package/dist/queuer/createQueuer.js.map +1 -1
  64. package/dist/queuer/index.cjs +3 -3
  65. package/dist/rate-limiter/createRateLimiter.cjs +78 -23
  66. package/dist/rate-limiter/createRateLimiter.cjs.map +1 -1
  67. package/dist/rate-limiter/createRateLimiter.d.cts +88 -20
  68. package/dist/rate-limiter/createRateLimiter.d.ts +88 -20
  69. package/dist/rate-limiter/createRateLimiter.js +74 -19
  70. package/dist/rate-limiter/createRateLimiter.js.map +1 -1
  71. package/dist/rate-limiter/index.cjs +3 -3
  72. package/dist/throttler/createThrottledSignal.cjs +1 -1
  73. package/dist/throttler/createThrottledSignal.cjs.map +1 -1
  74. package/dist/throttler/createThrottledSignal.d.cts +1 -1
  75. package/dist/throttler/createThrottledSignal.d.ts +1 -1
  76. package/dist/throttler/createThrottledSignal.js +1 -1
  77. package/dist/throttler/createThrottledSignal.js.map +1 -1
  78. package/dist/throttler/createThrottledValue.cjs +1 -1
  79. package/dist/throttler/createThrottledValue.cjs.map +1 -1
  80. package/dist/throttler/createThrottledValue.d.cts +1 -1
  81. package/dist/throttler/createThrottledValue.d.ts +1 -1
  82. package/dist/throttler/createThrottledValue.js +1 -1
  83. package/dist/throttler/createThrottledValue.js.map +1 -1
  84. package/dist/throttler/createThrottler.cjs +34 -13
  85. package/dist/throttler/createThrottler.cjs.map +1 -1
  86. package/dist/throttler/createThrottler.d.cts +44 -10
  87. package/dist/throttler/createThrottler.d.ts +44 -10
  88. package/dist/throttler/createThrottler.js +30 -9
  89. package/dist/throttler/createThrottler.js.map +1 -1
  90. package/dist/throttler/index.cjs +3 -3
  91. package/dist/types/index.cjs +3 -3
  92. package/dist/utils/index.cjs +3 -3
  93. package/package.json +2 -2
  94. package/src/async-batcher/createAsyncBatcher.ts +51 -10
  95. package/src/async-debouncer/createAsyncDebouncer.ts +51 -10
  96. package/src/async-queuer/createAsyncQueuer.ts +51 -10
  97. package/src/async-rate-limiter/createAsyncRateLimiter.ts +114 -28
  98. package/src/async-throttler/createAsyncThrottler.ts +51 -10
  99. package/src/batcher/createBatcher.ts +51 -10
  100. package/src/debouncer/createDebouncer.ts +52 -11
  101. package/src/queuer/createQueuedSignal.ts +8 -8
  102. package/src/queuer/createQueuer.ts +51 -10
  103. package/src/rate-limiter/createRateLimiter.ts +103 -21
  104. package/src/throttler/createThrottledSignal.ts +1 -1
  105. package/src/throttler/createThrottledValue.ts +1 -1
  106. package/src/throttler/createThrottler.ts +59 -11
@@ -1,9 +1,26 @@
1
1
  import { Store } from "@tanstack/solid-store";
2
- import { Accessor } from "solid-js";
2
+ import { Accessor, JSX } from "solid-js";
3
3
  import { Queuer, QueuerOptions, QueuerState } from "@tanstack/pacer/queuer";
4
4
 
5
5
  //#region src/queuer/createQueuer.d.ts
6
6
  interface SolidQueuer<TValue, TSelected = {}> extends Omit<Queuer<TValue>, 'store'> {
7
+ /**
8
+ * A Solid component that allows you to subscribe to the queuer state.
9
+ *
10
+ * This is useful for tracking specific parts of the queuer state
11
+ * deep in your component tree without needing to pass a selector to the hook.
12
+ *
13
+ * @example
14
+ * <queuer.Subscribe selector={(state) => ({ size: state.size, isRunning: state.isRunning })}>
15
+ * {(state) => (
16
+ * <div>Queue: {state().size} items, {state().isRunning ? 'Processing' : 'Idle'}</div>
17
+ * )}
18
+ * </queuer.Subscribe>
19
+ */
20
+ Subscribe: <TSelected>(props: {
21
+ selector: (state: QueuerState<TValue>) => TSelected;
22
+ children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element;
23
+ }) => JSX.Element;
7
24
  /**
8
25
  * Reactive state that will be updated when the queuer state changes
9
26
  *
@@ -36,14 +53,24 @@ interface SolidQueuer<TValue, TSelected = {}> extends Omit<Queuer<TValue>, 'stor
36
53
  *
37
54
  * ## State Management and Selector
38
55
  *
39
- * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
40
- * to specify which state changes will trigger a re-render, optimizing performance by preventing
41
- * unnecessary re-renders when irrelevant state changes occur.
56
+ * The hook uses TanStack Store for reactive state management. You can subscribe to state changes
57
+ * in two ways:
58
+ *
59
+ * **1. Using `queuer.Subscribe` component (Recommended for component tree subscriptions)**
60
+ *
61
+ * Use the `Subscribe` component to subscribe to state changes deep in your component tree without
62
+ * needing to pass a selector to the hook. This is ideal when you want to subscribe to state
63
+ * in child components.
64
+ *
65
+ * **2. Using the `selector` parameter (For hook-level subscriptions)**
66
+ *
67
+ * The `selector` parameter allows you to specify which state changes will trigger reactive updates
68
+ * at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
69
+ * state changes occur.
42
70
  *
43
71
  * **By default, there will be no reactive state subscriptions** and you must opt-in to state
44
- * tracking by providing a selector function. This prevents unnecessary re-renders and gives you
45
- * full control over when your component updates. Only when you provide a selector will the
46
- * component re-render when the selected state values change.
72
+ * tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
73
+ * updates and gives you full control over when your component tracks state changes.
47
74
  *
48
75
  * Available state properties:
49
76
  * - `executionCount`: Number of items that have been processed
@@ -66,14 +93,14 @@ interface SolidQueuer<TValue, TSelected = {}> extends Omit<Queuer<TValue>, 'stor
66
93
  * }
67
94
  * );
68
95
  *
69
- * // Opt-in to re-render when items or isRunning changes (optimized for UI updates)
96
+ * // Opt-in to track items or isRunning changes (optimized for UI updates)
70
97
  * const queue = createQueuer(
71
98
  * (item) => console.log('Processing', item),
72
99
  * { started: true, wait: 1000 },
73
100
  * (state) => ({ items: state.items, isRunning: state.isRunning })
74
101
  * );
75
102
  *
76
- * // Opt-in to re-render when execution metrics change (optimized for tracking progress)
103
+ * // Opt-in to track execution metrics changes (optimized for tracking progress)
77
104
  * const queue = createQueuer(
78
105
  * (item) => console.log('Processing', item),
79
106
  * { started: true, wait: 1000 },
@@ -1,9 +1,26 @@
1
- import { Accessor } from "solid-js";
1
+ import { Accessor, JSX } from "solid-js";
2
2
  import { Store } from "@tanstack/solid-store";
3
3
  import { Queuer, QueuerOptions, QueuerState } from "@tanstack/pacer/queuer";
4
4
 
5
5
  //#region src/queuer/createQueuer.d.ts
6
6
  interface SolidQueuer<TValue, TSelected = {}> extends Omit<Queuer<TValue>, 'store'> {
7
+ /**
8
+ * A Solid component that allows you to subscribe to the queuer state.
9
+ *
10
+ * This is useful for tracking specific parts of the queuer state
11
+ * deep in your component tree without needing to pass a selector to the hook.
12
+ *
13
+ * @example
14
+ * <queuer.Subscribe selector={(state) => ({ size: state.size, isRunning: state.isRunning })}>
15
+ * {(state) => (
16
+ * <div>Queue: {state().size} items, {state().isRunning ? 'Processing' : 'Idle'}</div>
17
+ * )}
18
+ * </queuer.Subscribe>
19
+ */
20
+ Subscribe: <TSelected>(props: {
21
+ selector: (state: QueuerState<TValue>) => TSelected;
22
+ children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element;
23
+ }) => JSX.Element;
7
24
  /**
8
25
  * Reactive state that will be updated when the queuer state changes
9
26
  *
@@ -36,14 +53,24 @@ interface SolidQueuer<TValue, TSelected = {}> extends Omit<Queuer<TValue>, 'stor
36
53
  *
37
54
  * ## State Management and Selector
38
55
  *
39
- * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
40
- * to specify which state changes will trigger a re-render, optimizing performance by preventing
41
- * unnecessary re-renders when irrelevant state changes occur.
56
+ * The hook uses TanStack Store for reactive state management. You can subscribe to state changes
57
+ * in two ways:
58
+ *
59
+ * **1. Using `queuer.Subscribe` component (Recommended for component tree subscriptions)**
60
+ *
61
+ * Use the `Subscribe` component to subscribe to state changes deep in your component tree without
62
+ * needing to pass a selector to the hook. This is ideal when you want to subscribe to state
63
+ * in child components.
64
+ *
65
+ * **2. Using the `selector` parameter (For hook-level subscriptions)**
66
+ *
67
+ * The `selector` parameter allows you to specify which state changes will trigger reactive updates
68
+ * at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
69
+ * state changes occur.
42
70
  *
43
71
  * **By default, there will be no reactive state subscriptions** and you must opt-in to state
44
- * tracking by providing a selector function. This prevents unnecessary re-renders and gives you
45
- * full control over when your component updates. Only when you provide a selector will the
46
- * component re-render when the selected state values change.
72
+ * tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
73
+ * updates and gives you full control over when your component tracks state changes.
47
74
  *
48
75
  * Available state properties:
49
76
  * - `executionCount`: Number of items that have been processed
@@ -66,14 +93,14 @@ interface SolidQueuer<TValue, TSelected = {}> extends Omit<Queuer<TValue>, 'stor
66
93
  * }
67
94
  * );
68
95
  *
69
- * // Opt-in to re-render when items or isRunning changes (optimized for UI updates)
96
+ * // Opt-in to track items or isRunning changes (optimized for UI updates)
70
97
  * const queue = createQueuer(
71
98
  * (item) => console.log('Processing', item),
72
99
  * { started: true, wait: 1000 },
73
100
  * (state) => ({ items: state.items, isRunning: state.isRunning })
74
101
  * );
75
102
  *
76
- * // Opt-in to re-render when execution metrics change (optimized for tracking progress)
103
+ * // Opt-in to track execution metrics changes (optimized for tracking progress)
77
104
  * const queue = createQueuer(
78
105
  * (item) => console.log('Processing', item),
79
106
  * { started: true, wait: 1000 },
@@ -22,14 +22,24 @@ import { Queuer } from "@tanstack/pacer/queuer";
22
22
  *
23
23
  * ## State Management and Selector
24
24
  *
25
- * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
26
- * to specify which state changes will trigger a re-render, optimizing performance by preventing
27
- * unnecessary re-renders when irrelevant state changes occur.
25
+ * The hook uses TanStack Store for reactive state management. You can subscribe to state changes
26
+ * in two ways:
27
+ *
28
+ * **1. Using `queuer.Subscribe` component (Recommended for component tree subscriptions)**
29
+ *
30
+ * Use the `Subscribe` component to subscribe to state changes deep in your component tree without
31
+ * needing to pass a selector to the hook. This is ideal when you want to subscribe to state
32
+ * in child components.
33
+ *
34
+ * **2. Using the `selector` parameter (For hook-level subscriptions)**
35
+ *
36
+ * The `selector` parameter allows you to specify which state changes will trigger reactive updates
37
+ * at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
38
+ * state changes occur.
28
39
  *
29
40
  * **By default, there will be no reactive state subscriptions** and you must opt-in to state
30
- * tracking by providing a selector function. This prevents unnecessary re-renders and gives you
31
- * full control over when your component updates. Only when you provide a selector will the
32
- * component re-render when the selected state values change.
41
+ * tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
42
+ * updates and gives you full control over when your component tracks state changes.
33
43
  *
34
44
  * Available state properties:
35
45
  * - `executionCount`: Number of items that have been processed
@@ -52,14 +62,14 @@ import { Queuer } from "@tanstack/pacer/queuer";
52
62
  * }
53
63
  * );
54
64
  *
55
- * // Opt-in to re-render when items or isRunning changes (optimized for UI updates)
65
+ * // Opt-in to track items or isRunning changes (optimized for UI updates)
56
66
  * const queue = createQueuer(
57
67
  * (item) => console.log('Processing', item),
58
68
  * { started: true, wait: 1000 },
59
69
  * (state) => ({ items: state.items, isRunning: state.isRunning })
60
70
  * );
61
71
  *
62
- * // Opt-in to re-render when execution metrics change (optimized for tracking progress)
72
+ * // Opt-in to track execution metrics changes (optimized for tracking progress)
63
73
  * const queue = createQueuer(
64
74
  * (item) => console.log('Processing', item),
65
75
  * { started: true, wait: 1000 },
@@ -86,6 +96,10 @@ function createQueuer(fn, options = {}, selector = () => ({})) {
86
96
  ...useDefaultPacerOptions().queuer,
87
97
  ...options
88
98
  });
99
+ queuer.Subscribe = function Subscribe(props) {
100
+ const selected = useStore(queuer.store, props.selector);
101
+ return typeof props.children === "function" ? props.children(selected) : props.children;
102
+ };
89
103
  const state = useStore(queuer.store, selector);
90
104
  return {
91
105
  ...queuer,
@@ -1 +1 @@
1
- {"version":3,"file":"createQueuer.js","names":[],"sources":["../../src/queuer/createQueuer.ts"],"sourcesContent":["import { Queuer } from '@tanstack/pacer/queuer'\nimport { useStore } from '@tanstack/solid-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { QueuerOptions, QueuerState } from '@tanstack/pacer/queuer'\n\nexport interface SolidQueuer<TValue, TSelected = {}> extends Omit<\n Queuer<TValue>,\n 'store'\n> {\n /**\n * Reactive state that will be updated when the queuer state changes\n *\n * Use this instead of `queuer.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.\n * The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<QueuerState<TValue>>>\n}\n\n/**\n * Creates a Solid-compatible Queuer instance for managing a synchronous queue of items, exposing Solid signals for all stateful properties.\n *\n * Features:\n * - Synchronous processing of items using the provided `fn` function\n * - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior\n * - Priority queueing via `getPriority` or item `priority` property\n * - Item expiration and removal of stale items\n * - Configurable wait time between processing items\n * - Pause/resume processing\n * - Callbacks for queue state changes, execution, rejection, and expiration\n * - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity\n *\n * The queue processes items synchronously in order, with optional delays between each item. When started, it will process one item per tick, with an optional wait time between ticks. You can pause and resume processing with `stop()` and `start()`.\n *\n * By default, the queue uses FIFO behavior, but you can configure LIFO or double-ended queueing by specifying the position when adding or removing items.\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you\n * to specify which state changes will trigger a re-render, optimizing performance by preventing\n * unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available state properties:\n * - `executionCount`: Number of items that have been processed\n * - `isRunning`: Whether the queuer is currently running (not stopped)\n * - `items`: Array of items currently queued for processing\n * - `rejectionCount`: Number of items that were rejected (expired or failed validation)\n *\n * Example usage:\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const queue = createQueuer(\n * (item) => {\n * // process item synchronously\n * console.log('Processing', item);\n * },\n * {\n * started: true, // Start processing immediately\n * wait: 1000, // Process one item every second\n * getPriority: (item) => item.priority // Process higher priority items first\n * }\n * );\n *\n * // Opt-in to re-render when items or isRunning changes (optimized for UI updates)\n * const queue = createQueuer(\n * (item) => console.log('Processing', item),\n * { started: true, wait: 1000 },\n * (state) => ({ items: state.items, isRunning: state.isRunning })\n * );\n *\n * // Opt-in to re-render when execution metrics change (optimized for tracking progress)\n * const queue = createQueuer(\n * (item) => console.log('Processing', item),\n * { started: true, wait: 1000 },\n * (state) => ({\n * executionCount: state.executionCount,\n * rejectionCount: state.rejectionCount\n * })\n * );\n *\n * // Add items to process - they'll be handled automatically\n * queue.addItem('task1');\n * queue.addItem('task2');\n *\n * // Control the scheduler\n * queue.stop(); // Pause processing\n * queue.start(); // Resume processing\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { items, isRunning } = queue.state();\n * ```\n */\nexport function createQueuer<TValue, TSelected = {}>(\n fn: (item: TValue) => void,\n options: QueuerOptions<TValue> = {},\n selector: (state: QueuerState<TValue>) => TSelected = () => ({}) as TSelected,\n): SolidQueuer<TValue, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().queuer,\n ...options,\n } as QueuerOptions<TValue>\n\n const queuer = new Queuer(fn, mergedOptions)\n\n const state = useStore(queuer.store, selector)\n\n return {\n ...queuer,\n state,\n } as SolidQueuer<TValue, TSelected> // omit `store` in favor of `state`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuGA,SAAgB,aACd,IACA,UAAiC,EAAE,EACnC,kBAA6D,EAAE,GAC/B;CAMhC,MAAM,SAAS,IAAI,OAAO,IALJ;EACpB,GAAG,wBAAwB,CAAC;EAC5B,GAAG;EACJ,CAE2C;CAE5C,MAAM,QAAQ,SAAS,OAAO,OAAO,SAAS;AAE9C,QAAO;EACL,GAAG;EACH;EACD"}
1
+ {"version":3,"file":"createQueuer.js","names":[],"sources":["../../src/queuer/createQueuer.ts"],"sourcesContent":["import { Queuer } from '@tanstack/pacer/queuer'\nimport { useStore } from '@tanstack/solid-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor, JSX } from 'solid-js'\nimport type { QueuerOptions, QueuerState } from '@tanstack/pacer/queuer'\n\nexport interface SolidQueuer<TValue, TSelected = {}> extends Omit<\n Queuer<TValue>,\n 'store'\n> {\n /**\n * A Solid component that allows you to subscribe to the queuer state.\n *\n * This is useful for tracking specific parts of the queuer state\n * deep in your component tree without needing to pass a selector to the hook.\n *\n * @example\n * <queuer.Subscribe selector={(state) => ({ size: state.size, isRunning: state.isRunning })}>\n * {(state) => (\n * <div>Queue: {state().size} items, {state().isRunning ? 'Processing' : 'Idle'}</div>\n * )}\n * </queuer.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: QueuerState<TValue>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) => JSX.Element\n /**\n * Reactive state that will be updated when the queuer state changes\n *\n * Use this instead of `queuer.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.\n * The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<QueuerState<TValue>>>\n}\n\n/**\n * Creates a Solid-compatible Queuer instance for managing a synchronous queue of items, exposing Solid signals for all stateful properties.\n *\n * Features:\n * - Synchronous processing of items using the provided `fn` function\n * - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior\n * - Priority queueing via `getPriority` or item `priority` property\n * - Item expiration and removal of stale items\n * - Configurable wait time between processing items\n * - Pause/resume processing\n * - Callbacks for queue state changes, execution, rejection, and expiration\n * - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity\n *\n * The queue processes items synchronously in order, with optional delays between each item. When started, it will process one item per tick, with an optional wait time between ticks. You can pause and resume processing with `stop()` and `start()`.\n *\n * By default, the queue uses FIFO behavior, but you can configure LIFO or double-ended queueing by specifying the position when adding or removing items.\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. You can subscribe to state changes\n * in two ways:\n *\n * **1. Using `queuer.Subscribe` component (Recommended for component tree subscriptions)**\n *\n * Use the `Subscribe` component to subscribe to state changes deep in your component tree without\n * needing to pass a selector to the hook. This is ideal when you want to subscribe to state\n * in child components.\n *\n * **2. Using the `selector` parameter (For hook-level subscriptions)**\n *\n * The `selector` parameter allows you to specify which state changes will trigger reactive updates\n * at the hook level, optimizing performance by preventing unnecessary updates when irrelevant\n * state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary\n * updates and gives you full control over when your component tracks state changes.\n *\n * Available state properties:\n * - `executionCount`: Number of items that have been processed\n * - `isRunning`: Whether the queuer is currently running (not stopped)\n * - `items`: Array of items currently queued for processing\n * - `rejectionCount`: Number of items that were rejected (expired or failed validation)\n *\n * Example usage:\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const queue = createQueuer(\n * (item) => {\n * // process item synchronously\n * console.log('Processing', item);\n * },\n * {\n * started: true, // Start processing immediately\n * wait: 1000, // Process one item every second\n * getPriority: (item) => item.priority // Process higher priority items first\n * }\n * );\n *\n * // Opt-in to track items or isRunning changes (optimized for UI updates)\n * const queue = createQueuer(\n * (item) => console.log('Processing', item),\n * { started: true, wait: 1000 },\n * (state) => ({ items: state.items, isRunning: state.isRunning })\n * );\n *\n * // Opt-in to track execution metrics changes (optimized for tracking progress)\n * const queue = createQueuer(\n * (item) => console.log('Processing', item),\n * { started: true, wait: 1000 },\n * (state) => ({\n * executionCount: state.executionCount,\n * rejectionCount: state.rejectionCount\n * })\n * );\n *\n * // Add items to process - they'll be handled automatically\n * queue.addItem('task1');\n * queue.addItem('task2');\n *\n * // Control the scheduler\n * queue.stop(); // Pause processing\n * queue.start(); // Resume processing\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { items, isRunning } = queue.state();\n * ```\n */\nexport function createQueuer<TValue, TSelected = {}>(\n fn: (item: TValue) => void,\n options: QueuerOptions<TValue> = {},\n selector: (state: QueuerState<TValue>) => TSelected = () => ({}) as TSelected,\n): SolidQueuer<TValue, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().queuer,\n ...options,\n } as QueuerOptions<TValue>\n\n const queuer = new Queuer(fn, mergedOptions) as unknown as SolidQueuer<\n TValue,\n TSelected\n >\n\n queuer.Subscribe = function Subscribe<TSelected>(props: {\n selector: (state: QueuerState<TValue>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) {\n const selected = useStore(queuer.store, props.selector)\n\n return typeof props.children === 'function'\n ? props.children(selected)\n : props.children\n }\n\n const state = useStore(queuer.store, selector)\n\n return {\n ...queuer,\n state,\n } as SolidQueuer<TValue, TSelected> // omit `store` in favor of `state`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkIA,SAAgB,aACd,IACA,UAAiC,EAAE,EACnC,kBAA6D,EAAE,GAC/B;CAMhC,MAAM,SAAS,IAAI,OAAO,IALJ;EACpB,GAAG,wBAAwB,CAAC;EAC5B,GAAG;EACJ,CAE2C;AAK5C,QAAO,YAAY,SAAS,UAAqB,OAG9C;EACD,MAAM,WAAW,SAAS,OAAO,OAAO,MAAM,SAAS;AAEvD,SAAO,OAAO,MAAM,aAAa,aAC7B,MAAM,SAAS,SAAS,GACxB,MAAM;;CAGZ,MAAM,QAAQ,SAAS,OAAO,OAAO,SAAS;AAE9C,QAAO;EACL,GAAG;EACH;EACD"}
@@ -3,10 +3,10 @@ const require_createQueuedSignal = require('./createQueuedSignal.cjs');
3
3
 
4
4
  exports.createQueuedSignal = require_createQueuedSignal.createQueuedSignal;
5
5
  exports.createQueuer = require_createQueuer.createQueuer;
6
- var __tanstack_pacer_queuer = require("@tanstack/pacer/queuer");
7
- Object.keys(__tanstack_pacer_queuer).forEach(function (k) {
6
+ var _tanstack_pacer_queuer = require("@tanstack/pacer/queuer");
7
+ Object.keys(_tanstack_pacer_queuer).forEach(function (k) {
8
8
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
9
9
  enumerable: true,
10
- get: function () { return __tanstack_pacer_queuer[k]; }
10
+ get: function () { return _tanstack_pacer_queuer[k]; }
11
11
  });
12
12
  });
@@ -1,6 +1,6 @@
1
1
  const require_PacerProvider = require('../provider/PacerProvider.cjs');
2
- let __tanstack_solid_store = require("@tanstack/solid-store");
3
- let __tanstack_pacer_rate_limiter = require("@tanstack/pacer/rate-limiter");
2
+ let _tanstack_solid_store = require("@tanstack/solid-store");
3
+ let _tanstack_pacer_rate_limiter = require("@tanstack/pacer/rate-limiter");
4
4
 
5
5
  //#region src/rate-limiter/createRateLimiter.ts
6
6
  /**
@@ -26,14 +26,24 @@ let __tanstack_pacer_rate_limiter = require("@tanstack/pacer/rate-limiter");
26
26
  *
27
27
  * ## State Management and Selector
28
28
  *
29
- * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
30
- * to specify which state changes will trigger a re-render, optimizing performance by preventing
31
- * unnecessary re-renders when irrelevant state changes occur.
29
+ * The hook uses TanStack Store for reactive state management. You can subscribe to state changes
30
+ * in two ways:
31
+ *
32
+ * **1. Using `rateLimiter.Subscribe` component (Recommended for component tree subscriptions)**
33
+ *
34
+ * Use the `Subscribe` component to subscribe to state changes deep in your component tree without
35
+ * needing to pass a selector to the hook. This is ideal when you want to subscribe to state
36
+ * in child components.
37
+ *
38
+ * **2. Using the `selector` parameter (For hook-level subscriptions)**
39
+ *
40
+ * The `selector` parameter allows you to specify which state changes will trigger reactive updates
41
+ * at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
42
+ * state changes occur.
32
43
  *
33
44
  * **By default, there will be no reactive state subscriptions** and you must opt-in to state
34
- * tracking by providing a selector function. This prevents unnecessary re-renders and gives you
35
- * full control over when your component updates. Only when you provide a selector will the
36
- * component re-render when the selected state values change.
45
+ * tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
46
+ * updates and gives you full control over when your component tracks state changes.
37
47
  *
38
48
  * Available state properties:
39
49
  * - `executionCount`: Number of function executions that have been completed
@@ -49,41 +59,86 @@ let __tanstack_pacer_rate_limiter = require("@tanstack/pacer/rate-limiter");
49
59
  * limit: 5,
50
60
  * window: 60000,
51
61
  * windowType: 'sliding',
52
- * onReject: (rateLimiter) => {
53
- * console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
54
- * }
55
62
  * });
56
63
  *
57
- * // Opt-in to re-render when rate limit state changes (optimized for UI feedback)
64
+ * // Subscribe to state changes deep in component tree using Subscribe component
65
+ * <rateLimiter.Subscribe selector={(state) => ({ rejectionCount: state.rejectionCount })}>
66
+ * {(state) => (
67
+ * <div>Rejections: {state().rejectionCount}</div>
68
+ * )}
69
+ * </rateLimiter.Subscribe>
70
+ *
71
+ * // Opt-in to track execution count changes at hook level (optimized for tracking successful executions)
58
72
  * const rateLimiter = createRateLimiter(
59
73
  * apiCall,
60
- * { limit: 5, window: 60000 },
61
- * (state) => ({
62
- * remainingInWindow: state.remainingInWindow,
63
- * rejectionCount: state.rejectionCount
64
- * })
74
+ * {
75
+ * limit: 5,
76
+ * window: 60000,
77
+ * windowType: 'sliding',
78
+ * },
79
+ * (state) => ({ executionCount: state.executionCount })
65
80
  * );
66
81
  *
67
- * // Opt-in to re-render when execution metrics change (optimized for tracking progress)
82
+ * // Opt-in to track rejection count changes (optimized for tracking rate limit violations)
68
83
  * const rateLimiter = createRateLimiter(
69
84
  * apiCall,
70
- * { limit: 5, window: 60000 },
85
+ * {
86
+ * limit: 5,
87
+ * window: 60000,
88
+ * windowType: 'sliding',
89
+ * },
90
+ * (state) => ({ rejectionCount: state.rejectionCount })
91
+ * );
92
+ *
93
+ * // Opt-in to track execution times changes (optimized for window calculations)
94
+ * const rateLimiter = createRateLimiter(
95
+ * apiCall,
96
+ * {
97
+ * limit: 5,
98
+ * window: 60000,
99
+ * windowType: 'sliding',
100
+ * },
101
+ * (state) => ({ executionTimes: state.executionTimes })
102
+ * );
103
+ *
104
+ * // Multiple state properties - track when any of these change
105
+ * const rateLimiter = createRateLimiter(
106
+ * apiCall,
107
+ * {
108
+ * limit: 5,
109
+ * window: 60000,
110
+ * windowType: 'sliding',
111
+ * },
71
112
  * (state) => ({
72
113
  * executionCount: state.executionCount,
73
- * nextWindowTime: state.nextWindowTime
114
+ * rejectionCount: state.rejectionCount
74
115
  * })
75
116
  * );
76
117
  *
118
+ * // Monitor rate limit status
119
+ * const handleClick = () => {
120
+ * const remaining = rateLimiter.getRemainingInWindow();
121
+ * if (remaining > 0) {
122
+ * rateLimiter.maybeExecute(data);
123
+ * } else {
124
+ * showRateLimitWarning();
125
+ * }
126
+ * };
127
+ *
77
128
  * // Access the selected state (will be empty object {} unless selector provided)
78
- * const { remainingInWindow, rejectionCount } = rateLimiter.state();
129
+ * const { executionCount, rejectionCount } = rateLimiter.state();
79
130
  * ```
80
131
  */
81
132
  function createRateLimiter(fn, options, selector = () => ({})) {
82
- const rateLimiter = new __tanstack_pacer_rate_limiter.RateLimiter(fn, {
133
+ const rateLimiter = new _tanstack_pacer_rate_limiter.RateLimiter(fn, {
83
134
  ...require_PacerProvider.useDefaultPacerOptions().rateLimiter,
84
135
  ...options
85
136
  });
86
- const state = (0, __tanstack_solid_store.useStore)(rateLimiter.store, selector);
137
+ rateLimiter.Subscribe = function Subscribe(props) {
138
+ const selected = (0, _tanstack_solid_store.useStore)(rateLimiter.store, props.selector);
139
+ return typeof props.children === "function" ? props.children(selected) : props.children;
140
+ };
141
+ const state = (0, _tanstack_solid_store.useStore)(rateLimiter.store, selector);
87
142
  return {
88
143
  ...rateLimiter,
89
144
  state
@@ -1 +1 @@
1
- {"version":3,"file":"createRateLimiter.cjs","names":["RateLimiter","useDefaultPacerOptions"],"sources":["../../src/rate-limiter/createRateLimiter.ts"],"sourcesContent":["import { RateLimiter } from '@tanstack/pacer/rate-limiter'\nimport { useStore } from '@tanstack/solid-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n RateLimiterOptions,\n RateLimiterState,\n} from '@tanstack/pacer/rate-limiter'\n\nexport interface SolidRateLimiter<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<RateLimiter<TFn>, 'store'> {\n /**\n * Reactive state that will be updated when the rate limiter state changes\n *\n * Use this instead of `rateLimiter.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.\n * The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<RateLimiterState>>\n}\n\n/**\n * A low-level Solid hook that creates a `RateLimiter` instance to enforce rate limits on function execution.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a rate limiter instance that\n * you can integrate with any state management solution (createSignal, etc).\n *\n * Rate limiting is a simple \"hard limit\" approach that allows executions until a maximum count is reached within\n * a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,\n * it does not attempt to space out or collapse executions intelligently.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n * consistent rate of execution over time.\n *\n * For smoother execution patterns:\n * - Use throttling when you want consistent spacing between executions (e.g. UI updates)\n * - Use debouncing when you want to collapse rapid-fire events (e.g. search input)\n * - Use rate limiting only when you need to enforce hard limits (e.g. API rate limits)\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you\n * to specify which state changes will trigger a re-render, optimizing performance by preventing\n * unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available state properties:\n * - `executionCount`: Number of function executions that have been completed\n * - `rejectionCount`: Number of function calls that were rejected due to rate limiting\n * - `remainingInWindow`: Number of executions remaining in the current window\n * - `nextWindowTime`: Timestamp when the next window begins\n * - `currentWindowStart`: Timestamp when the current window started\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const rateLimiter = createRateLimiter(apiCall, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * onReject: (rateLimiter) => {\n * console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);\n * }\n * });\n *\n * // Opt-in to re-render when rate limit state changes (optimized for UI feedback)\n * const rateLimiter = createRateLimiter(\n * apiCall,\n * { limit: 5, window: 60000 },\n * (state) => ({\n * remainingInWindow: state.remainingInWindow,\n * rejectionCount: state.rejectionCount\n * })\n * );\n *\n * // Opt-in to re-render when execution metrics change (optimized for tracking progress)\n * const rateLimiter = createRateLimiter(\n * apiCall,\n * { limit: 5, window: 60000 },\n * (state) => ({\n * executionCount: state.executionCount,\n * nextWindowTime: state.nextWindowTime\n * })\n * );\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { remainingInWindow, rejectionCount } = rateLimiter.state();\n * ```\n */\nexport function createRateLimiter<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: RateLimiterOptions<TFn>,\n selector: (state: RateLimiterState) => TSelected = () => ({}) as TSelected,\n): SolidRateLimiter<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().rateLimiter,\n ...options,\n } as RateLimiterOptions<TFn>\n\n const rateLimiter = new RateLimiter<TFn>(fn, mergedOptions)\n\n const state = useStore(rateLimiter.store, selector)\n\n return {\n ...rateLimiter,\n state,\n } as SolidRateLimiter<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwGA,SAAgB,kBACd,IACA,SACA,kBAA0D,EAAE,GAC1B;CAMlC,MAAM,cAAc,IAAIA,0CAAiB,IALnB;EACpB,GAAGC,8CAAwB,CAAC;EAC5B,GAAG;EACJ,CAE0D;CAE3D,MAAM,6CAAiB,YAAY,OAAO,SAAS;AAEnD,QAAO;EACL,GAAG;EACH;EACD"}
1
+ {"version":3,"file":"createRateLimiter.cjs","names":["RateLimiter","useDefaultPacerOptions"],"sources":["../../src/rate-limiter/createRateLimiter.ts"],"sourcesContent":["import { RateLimiter } from '@tanstack/pacer/rate-limiter'\nimport { useStore } from '@tanstack/solid-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor, JSX } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n RateLimiterOptions,\n RateLimiterState,\n} from '@tanstack/pacer/rate-limiter'\n\nexport interface SolidRateLimiter<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<RateLimiter<TFn>, 'store'> {\n /**\n * A Solid component that allows you to subscribe to the rate limiter state.\n *\n * This is useful for tracking specific parts of the rate limiter state\n * deep in your component tree without needing to pass a selector to the hook.\n *\n * @example\n * <rateLimiter.Subscribe selector={(state) => ({ rejectionCount: state.rejectionCount })}>\n * {(state) => (\n * <div>Rejections: {state().rejectionCount}</div>\n * )}\n * </rateLimiter.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: RateLimiterState) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) => JSX.Element\n /**\n * Reactive state that will be updated when the rate limiter state changes\n *\n * Use this instead of `rateLimiter.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.\n * The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<RateLimiterState>>\n}\n\n/**\n * A low-level Solid hook that creates a `RateLimiter` instance to enforce rate limits on function execution.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a rate limiter instance that\n * you can integrate with any state management solution (createSignal, etc).\n *\n * Rate limiting is a simple \"hard limit\" approach that allows executions until a maximum count is reached within\n * a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,\n * it does not attempt to space out or collapse executions intelligently.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n * consistent rate of execution over time.\n *\n * For smoother execution patterns:\n * - Use throttling when you want consistent spacing between executions (e.g. UI updates)\n * - Use debouncing when you want to collapse rapid-fire events (e.g. search input)\n * - Use rate limiting only when you need to enforce hard limits (e.g. API rate limits)\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. You can subscribe to state changes\n * in two ways:\n *\n * **1. Using `rateLimiter.Subscribe` component (Recommended for component tree subscriptions)**\n *\n * Use the `Subscribe` component to subscribe to state changes deep in your component tree without\n * needing to pass a selector to the hook. This is ideal when you want to subscribe to state\n * in child components.\n *\n * **2. Using the `selector` parameter (For hook-level subscriptions)**\n *\n * The `selector` parameter allows you to specify which state changes will trigger reactive updates\n * at the hook level, optimizing performance by preventing unnecessary updates when irrelevant\n * state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary\n * updates and gives you full control over when your component tracks state changes.\n *\n * Available state properties:\n * - `executionCount`: Number of function executions that have been completed\n * - `rejectionCount`: Number of function calls that were rejected due to rate limiting\n * - `remainingInWindow`: Number of executions remaining in the current window\n * - `nextWindowTime`: Timestamp when the next window begins\n * - `currentWindowStart`: Timestamp when the current window started\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const rateLimiter = createRateLimiter(apiCall, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * });\n *\n * // Subscribe to state changes deep in component tree using Subscribe component\n * <rateLimiter.Subscribe selector={(state) => ({ rejectionCount: state.rejectionCount })}>\n * {(state) => (\n * <div>Rejections: {state().rejectionCount}</div>\n * )}\n * </rateLimiter.Subscribe>\n *\n * // Opt-in to track execution count changes at hook level (optimized for tracking successful executions)\n * const rateLimiter = createRateLimiter(\n * apiCall,\n * {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Opt-in to track rejection count changes (optimized for tracking rate limit violations)\n * const rateLimiter = createRateLimiter(\n * apiCall,\n * {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * },\n * (state) => ({ rejectionCount: state.rejectionCount })\n * );\n *\n * // Opt-in to track execution times changes (optimized for window calculations)\n * const rateLimiter = createRateLimiter(\n * apiCall,\n * {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * },\n * (state) => ({ executionTimes: state.executionTimes })\n * );\n *\n * // Multiple state properties - track when any of these change\n * const rateLimiter = createRateLimiter(\n * apiCall,\n * {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * },\n * (state) => ({\n * executionCount: state.executionCount,\n * rejectionCount: state.rejectionCount\n * })\n * );\n *\n * // Monitor rate limit status\n * const handleClick = () => {\n * const remaining = rateLimiter.getRemainingInWindow();\n * if (remaining > 0) {\n * rateLimiter.maybeExecute(data);\n * } else {\n * showRateLimitWarning();\n * }\n * };\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { executionCount, rejectionCount } = rateLimiter.state();\n * ```\n */\nexport function createRateLimiter<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: RateLimiterOptions<TFn>,\n selector: (state: RateLimiterState) => TSelected = () => ({}) as TSelected,\n): SolidRateLimiter<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().rateLimiter,\n ...options,\n } as RateLimiterOptions<TFn>\n\n const rateLimiter = new RateLimiter<TFn>(\n fn,\n mergedOptions,\n ) as unknown as SolidRateLimiter<TFn, TSelected>\n\n rateLimiter.Subscribe = function Subscribe<TSelected>(props: {\n selector: (state: RateLimiterState) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) {\n const selected = useStore(rateLimiter.store, props.selector)\n\n return typeof props.children === 'function'\n ? props.children(selected)\n : props.children\n }\n\n const state = useStore(rateLimiter.store, selector)\n\n return {\n ...rateLimiter,\n state,\n } as SolidRateLimiter<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4KA,SAAgB,kBACd,IACA,SACA,kBAA0D,EAAE,GAC1B;CAMlC,MAAM,cAAc,IAAIA,yCACtB,IANoB;EACpB,GAAGC,8CAAwB,CAAC;EAC5B,GAAG;EACJ,CAKA;AAED,aAAY,YAAY,SAAS,UAAqB,OAGnD;EACD,MAAM,+CAAoB,YAAY,OAAO,MAAM,SAAS;AAE5D,SAAO,OAAO,MAAM,aAAa,aAC7B,MAAM,SAAS,SAAS,GACxB,MAAM;;CAGZ,MAAM,4CAAiB,YAAY,OAAO,SAAS;AAEnD,QAAO;EACL,GAAG;EACH;EACD"}
@@ -1,10 +1,27 @@
1
1
  import { Store } from "@tanstack/solid-store";
2
- import { Accessor } from "solid-js";
2
+ import { Accessor, JSX } from "solid-js";
3
3
  import { AnyFunction } from "@tanstack/pacer/types";
4
4
  import { RateLimiter, RateLimiterOptions, RateLimiterState } from "@tanstack/pacer/rate-limiter";
5
5
 
6
6
  //#region src/rate-limiter/createRateLimiter.d.ts
7
7
  interface SolidRateLimiter<TFn extends AnyFunction, TSelected = {}> extends Omit<RateLimiter<TFn>, 'store'> {
8
+ /**
9
+ * A Solid component that allows you to subscribe to the rate limiter state.
10
+ *
11
+ * This is useful for tracking specific parts of the rate limiter state
12
+ * deep in your component tree without needing to pass a selector to the hook.
13
+ *
14
+ * @example
15
+ * <rateLimiter.Subscribe selector={(state) => ({ rejectionCount: state.rejectionCount })}>
16
+ * {(state) => (
17
+ * <div>Rejections: {state().rejectionCount}</div>
18
+ * )}
19
+ * </rateLimiter.Subscribe>
20
+ */
21
+ Subscribe: <TSelected>(props: {
22
+ selector: (state: RateLimiterState) => TSelected;
23
+ children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element;
24
+ }) => JSX.Element;
8
25
  /**
9
26
  * Reactive state that will be updated when the rate limiter state changes
10
27
  *
@@ -41,14 +58,24 @@ interface SolidRateLimiter<TFn extends AnyFunction, TSelected = {}> extends Omit
41
58
  *
42
59
  * ## State Management and Selector
43
60
  *
44
- * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
45
- * to specify which state changes will trigger a re-render, optimizing performance by preventing
46
- * unnecessary re-renders when irrelevant state changes occur.
61
+ * The hook uses TanStack Store for reactive state management. You can subscribe to state changes
62
+ * in two ways:
63
+ *
64
+ * **1. Using `rateLimiter.Subscribe` component (Recommended for component tree subscriptions)**
65
+ *
66
+ * Use the `Subscribe` component to subscribe to state changes deep in your component tree without
67
+ * needing to pass a selector to the hook. This is ideal when you want to subscribe to state
68
+ * in child components.
69
+ *
70
+ * **2. Using the `selector` parameter (For hook-level subscriptions)**
71
+ *
72
+ * The `selector` parameter allows you to specify which state changes will trigger reactive updates
73
+ * at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
74
+ * state changes occur.
47
75
  *
48
76
  * **By default, there will be no reactive state subscriptions** and you must opt-in to state
49
- * tracking by providing a selector function. This prevents unnecessary re-renders and gives you
50
- * full control over when your component updates. Only when you provide a selector will the
51
- * component re-render when the selected state values change.
77
+ * tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
78
+ * updates and gives you full control over when your component tracks state changes.
52
79
  *
53
80
  * Available state properties:
54
81
  * - `executionCount`: Number of function executions that have been completed
@@ -64,33 +91,74 @@ interface SolidRateLimiter<TFn extends AnyFunction, TSelected = {}> extends Omit
64
91
  * limit: 5,
65
92
  * window: 60000,
66
93
  * windowType: 'sliding',
67
- * onReject: (rateLimiter) => {
68
- * console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
69
- * }
70
94
  * });
71
95
  *
72
- * // Opt-in to re-render when rate limit state changes (optimized for UI feedback)
96
+ * // Subscribe to state changes deep in component tree using Subscribe component
97
+ * <rateLimiter.Subscribe selector={(state) => ({ rejectionCount: state.rejectionCount })}>
98
+ * {(state) => (
99
+ * <div>Rejections: {state().rejectionCount}</div>
100
+ * )}
101
+ * </rateLimiter.Subscribe>
102
+ *
103
+ * // Opt-in to track execution count changes at hook level (optimized for tracking successful executions)
73
104
  * const rateLimiter = createRateLimiter(
74
105
  * apiCall,
75
- * { limit: 5, window: 60000 },
76
- * (state) => ({
77
- * remainingInWindow: state.remainingInWindow,
78
- * rejectionCount: state.rejectionCount
79
- * })
106
+ * {
107
+ * limit: 5,
108
+ * window: 60000,
109
+ * windowType: 'sliding',
110
+ * },
111
+ * (state) => ({ executionCount: state.executionCount })
112
+ * );
113
+ *
114
+ * // Opt-in to track rejection count changes (optimized for tracking rate limit violations)
115
+ * const rateLimiter = createRateLimiter(
116
+ * apiCall,
117
+ * {
118
+ * limit: 5,
119
+ * window: 60000,
120
+ * windowType: 'sliding',
121
+ * },
122
+ * (state) => ({ rejectionCount: state.rejectionCount })
123
+ * );
124
+ *
125
+ * // Opt-in to track execution times changes (optimized for window calculations)
126
+ * const rateLimiter = createRateLimiter(
127
+ * apiCall,
128
+ * {
129
+ * limit: 5,
130
+ * window: 60000,
131
+ * windowType: 'sliding',
132
+ * },
133
+ * (state) => ({ executionTimes: state.executionTimes })
80
134
  * );
81
135
  *
82
- * // Opt-in to re-render when execution metrics change (optimized for tracking progress)
136
+ * // Multiple state properties - track when any of these change
83
137
  * const rateLimiter = createRateLimiter(
84
138
  * apiCall,
85
- * { limit: 5, window: 60000 },
139
+ * {
140
+ * limit: 5,
141
+ * window: 60000,
142
+ * windowType: 'sliding',
143
+ * },
86
144
  * (state) => ({
87
145
  * executionCount: state.executionCount,
88
- * nextWindowTime: state.nextWindowTime
146
+ * rejectionCount: state.rejectionCount
89
147
  * })
90
148
  * );
91
149
  *
150
+ * // Monitor rate limit status
151
+ * const handleClick = () => {
152
+ * const remaining = rateLimiter.getRemainingInWindow();
153
+ * if (remaining > 0) {
154
+ * rateLimiter.maybeExecute(data);
155
+ * } else {
156
+ * showRateLimitWarning();
157
+ * }
158
+ * };
159
+ *
92
160
  * // Access the selected state (will be empty object {} unless selector provided)
93
- * const { remainingInWindow, rejectionCount } = rateLimiter.state();
161
+ * const { executionCount, rejectionCount } = rateLimiter.state();
94
162
  * ```
95
163
  */
96
164
  declare function createRateLimiter<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: RateLimiterOptions<TFn>, selector?: (state: RateLimiterState) => TSelected): SolidRateLimiter<TFn, TSelected>;