@tanstack/solid-pacer 0.18.4 → 0.19.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 (141) hide show
  1. package/README.md +1 -1
  2. package/dist/async-batcher/createAsyncBatcher.cjs +32 -2
  3. package/dist/async-batcher/createAsyncBatcher.cjs.map +1 -1
  4. package/dist/async-batcher/createAsyncBatcher.d.cts +28 -2
  5. package/dist/async-batcher/createAsyncBatcher.d.ts +28 -2
  6. package/dist/async-batcher/createAsyncBatcher.js +32 -2
  7. package/dist/async-batcher/createAsyncBatcher.js.map +1 -1
  8. package/dist/async-batcher/index.cjs +1 -0
  9. package/dist/async-batcher/index.d.cts +2 -2
  10. package/dist/async-batcher/index.d.ts +2 -2
  11. package/dist/async-debouncer/createAsyncDebouncer.cjs +26 -3
  12. package/dist/async-debouncer/createAsyncDebouncer.cjs.map +1 -1
  13. package/dist/async-debouncer/createAsyncDebouncer.d.cts +27 -2
  14. package/dist/async-debouncer/createAsyncDebouncer.d.ts +27 -2
  15. package/dist/async-debouncer/createAsyncDebouncer.js +26 -3
  16. package/dist/async-debouncer/createAsyncDebouncer.js.map +1 -1
  17. package/dist/async-debouncer/index.cjs +1 -0
  18. package/dist/async-debouncer/index.d.cts +2 -2
  19. package/dist/async-debouncer/index.d.ts +2 -2
  20. package/dist/async-queuer/createAsyncQueuer.cjs +32 -2
  21. package/dist/async-queuer/createAsyncQueuer.cjs.map +1 -1
  22. package/dist/async-queuer/createAsyncQueuer.d.cts +28 -2
  23. package/dist/async-queuer/createAsyncQueuer.d.ts +28 -2
  24. package/dist/async-queuer/createAsyncQueuer.js +32 -2
  25. package/dist/async-queuer/createAsyncQueuer.js.map +1 -1
  26. package/dist/async-queuer/index.cjs +1 -0
  27. package/dist/async-queuer/index.d.cts +2 -2
  28. package/dist/async-queuer/index.d.ts +2 -2
  29. package/dist/async-rate-limiter/createAsyncRateLimiter.cjs +16 -2
  30. package/dist/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -1
  31. package/dist/async-rate-limiter/createAsyncRateLimiter.d.cts +15 -2
  32. package/dist/async-rate-limiter/createAsyncRateLimiter.d.ts +15 -2
  33. package/dist/async-rate-limiter/createAsyncRateLimiter.js +16 -2
  34. package/dist/async-rate-limiter/createAsyncRateLimiter.js.map +1 -1
  35. package/dist/async-rate-limiter/index.cjs +1 -0
  36. package/dist/async-rate-limiter/index.d.cts +2 -2
  37. package/dist/async-rate-limiter/index.d.ts +2 -2
  38. package/dist/async-throttler/createAsyncThrottler.cjs +31 -2
  39. package/dist/async-throttler/createAsyncThrottler.cjs.map +1 -1
  40. package/dist/async-throttler/createAsyncThrottler.d.cts +27 -2
  41. package/dist/async-throttler/createAsyncThrottler.d.ts +27 -2
  42. package/dist/async-throttler/createAsyncThrottler.js +31 -2
  43. package/dist/async-throttler/createAsyncThrottler.js.map +1 -1
  44. package/dist/async-throttler/index.cjs +1 -0
  45. package/dist/async-throttler/index.d.cts +2 -2
  46. package/dist/async-throttler/index.d.ts +2 -2
  47. package/dist/batcher/createBatcher.cjs +23 -2
  48. package/dist/batcher/createBatcher.cjs.map +1 -1
  49. package/dist/batcher/createBatcher.d.cts +22 -2
  50. package/dist/batcher/createBatcher.d.ts +22 -2
  51. package/dist/batcher/createBatcher.js +23 -2
  52. package/dist/batcher/createBatcher.js.map +1 -1
  53. package/dist/batcher/index.cjs +1 -0
  54. package/dist/batcher/index.d.cts +2 -2
  55. package/dist/batcher/index.d.ts +2 -2
  56. package/dist/debouncer/createDebouncedSignal.cjs.map +1 -1
  57. package/dist/debouncer/createDebouncedSignal.d.cts +3 -3
  58. package/dist/debouncer/createDebouncedSignal.d.ts +3 -3
  59. package/dist/debouncer/createDebouncedSignal.js.map +1 -1
  60. package/dist/debouncer/createDebouncedValue.cjs.map +1 -1
  61. package/dist/debouncer/createDebouncedValue.d.cts +3 -3
  62. package/dist/debouncer/createDebouncedValue.d.ts +3 -3
  63. package/dist/debouncer/createDebouncedValue.js.map +1 -1
  64. package/dist/debouncer/createDebouncer.cjs +17 -3
  65. package/dist/debouncer/createDebouncer.cjs.map +1 -1
  66. package/dist/debouncer/createDebouncer.d.cts +21 -2
  67. package/dist/debouncer/createDebouncer.d.ts +21 -2
  68. package/dist/debouncer/createDebouncer.js +17 -3
  69. package/dist/debouncer/createDebouncer.js.map +1 -1
  70. package/dist/debouncer/index.cjs +1 -0
  71. package/dist/debouncer/index.d.cts +2 -2
  72. package/dist/debouncer/index.d.ts +2 -2
  73. package/dist/index.cjs +1 -0
  74. package/dist/index.d.cts +11 -11
  75. package/dist/index.d.ts +11 -11
  76. package/dist/provider/index.cjs +1 -0
  77. package/dist/queuer/createQueuedSignal.cjs.map +1 -1
  78. package/dist/queuer/createQueuedSignal.d.cts +3 -3
  79. package/dist/queuer/createQueuedSignal.d.ts +3 -3
  80. package/dist/queuer/createQueuedSignal.js.map +1 -1
  81. package/dist/queuer/createQueuer.cjs +23 -2
  82. package/dist/queuer/createQueuer.cjs.map +1 -1
  83. package/dist/queuer/createQueuer.d.cts +22 -2
  84. package/dist/queuer/createQueuer.d.ts +22 -2
  85. package/dist/queuer/createQueuer.js +23 -2
  86. package/dist/queuer/createQueuer.js.map +1 -1
  87. package/dist/queuer/index.cjs +1 -0
  88. package/dist/queuer/index.d.cts +2 -2
  89. package/dist/queuer/index.d.ts +2 -2
  90. package/dist/rate-limiter/createRateLimitedSignal.cjs.map +1 -1
  91. package/dist/rate-limiter/createRateLimitedSignal.d.cts +3 -3
  92. package/dist/rate-limiter/createRateLimitedSignal.d.ts +3 -3
  93. package/dist/rate-limiter/createRateLimitedSignal.js.map +1 -1
  94. package/dist/rate-limiter/createRateLimitedValue.cjs.map +1 -1
  95. package/dist/rate-limiter/createRateLimitedValue.d.cts +3 -3
  96. package/dist/rate-limiter/createRateLimitedValue.d.ts +3 -3
  97. package/dist/rate-limiter/createRateLimitedValue.js.map +1 -1
  98. package/dist/rate-limiter/createRateLimiter.cjs +9 -2
  99. package/dist/rate-limiter/createRateLimiter.cjs.map +1 -1
  100. package/dist/rate-limiter/createRateLimiter.d.cts +9 -2
  101. package/dist/rate-limiter/createRateLimiter.d.ts +9 -2
  102. package/dist/rate-limiter/createRateLimiter.js +9 -2
  103. package/dist/rate-limiter/createRateLimiter.js.map +1 -1
  104. package/dist/rate-limiter/index.cjs +1 -0
  105. package/dist/rate-limiter/index.d.cts +2 -2
  106. package/dist/rate-limiter/index.d.ts +2 -2
  107. package/dist/throttler/createThrottledSignal.cjs.map +1 -1
  108. package/dist/throttler/createThrottledSignal.d.cts +3 -3
  109. package/dist/throttler/createThrottledSignal.d.ts +3 -3
  110. package/dist/throttler/createThrottledSignal.js.map +1 -1
  111. package/dist/throttler/createThrottledValue.cjs.map +1 -1
  112. package/dist/throttler/createThrottledValue.d.cts +3 -3
  113. package/dist/throttler/createThrottledValue.d.ts +3 -3
  114. package/dist/throttler/createThrottledValue.js.map +1 -1
  115. package/dist/throttler/createThrottler.cjs +17 -3
  116. package/dist/throttler/createThrottler.cjs.map +1 -1
  117. package/dist/throttler/createThrottler.d.cts +21 -2
  118. package/dist/throttler/createThrottler.d.ts +21 -2
  119. package/dist/throttler/createThrottler.js +17 -3
  120. package/dist/throttler/createThrottler.js.map +1 -1
  121. package/dist/throttler/index.cjs +1 -0
  122. package/dist/throttler/index.d.cts +2 -2
  123. package/dist/throttler/index.d.ts +2 -2
  124. package/package.json +32 -31
  125. package/src/async-batcher/createAsyncBatcher.ts +44 -3
  126. package/src/async-debouncer/createAsyncDebouncer.ts +37 -4
  127. package/src/async-queuer/createAsyncQueuer.ts +44 -3
  128. package/src/async-rate-limiter/createAsyncRateLimiter.ts +30 -3
  129. package/src/async-throttler/createAsyncThrottler.ts +43 -3
  130. package/src/batcher/createBatcher.ts +38 -3
  131. package/src/debouncer/createDebouncedSignal.ts +3 -6
  132. package/src/debouncer/createDebouncedValue.ts +3 -6
  133. package/src/debouncer/createDebouncer.ts +30 -4
  134. package/src/queuer/createQueuedSignal.ts +3 -3
  135. package/src/queuer/createQueuer.ts +37 -3
  136. package/src/rate-limiter/createRateLimitedSignal.ts +6 -6
  137. package/src/rate-limiter/createRateLimitedValue.ts +6 -6
  138. package/src/rate-limiter/createRateLimiter.ts +22 -3
  139. package/src/throttler/createThrottledSignal.ts +3 -6
  140. package/src/throttler/createThrottledValue.ts +3 -6
  141. package/src/throttler/createThrottler.ts +30 -4
@@ -4,6 +4,13 @@ import { AnyFunction } from "@tanstack/pacer/types";
4
4
  import { Throttler, ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/throttler";
5
5
 
6
6
  //#region src/throttler/createThrottler.d.ts
7
+ interface SolidThrottlerOptions<TFn extends AnyFunction, TSelected = {}> extends ThrottlerOptions<TFn> {
8
+ /**
9
+ * Optional callback invoked when the owning component unmounts. Receives the throttler instance.
10
+ * When provided, replaces the default cleanup (cancel); use it to call flush(), reset(), cancel(), add logging, etc.
11
+ */
12
+ onUnmount?: (throttler: SolidThrottler<TFn, TSelected>) => void;
13
+ }
7
14
  interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<Throttler<TFn>, 'store'> {
8
15
  /**
9
16
  * A Solid component that allows you to subscribe to the throttler state.
@@ -77,6 +84,18 @@ interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<T
77
84
  * - `nextExecutionTime`: Timestamp of the next allowed execution
78
85
  * - `status`: Current execution status ('disabled' | 'idle' | 'pending')
79
86
  *
87
+ * ## Unmount behavior
88
+ *
89
+ * By default, the primitive cancels any pending execution when the owning component unmounts.
90
+ * Use the `onUnmount` option to customize this. For example, to flush pending work instead:
91
+ *
92
+ * ```tsx
93
+ * const throttler = createThrottler(fn, {
94
+ * wait: 1000,
95
+ * onUnmount: (t) => t.flush()
96
+ * });
97
+ * ```
98
+ *
80
99
  * @example
81
100
  * ```tsx
82
101
  * // Default behavior - no reactive state subscriptions
@@ -123,7 +142,7 @@ interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<T
123
142
  * const { isPending, executionCount } = throttler.state();
124
143
  * ```
125
144
  */
126
- declare function createThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: ThrottlerOptions<TFn>, selector?: (state: ThrottlerState<TFn>) => TSelected): SolidThrottler<TFn, TSelected>;
145
+ declare function createThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: SolidThrottlerOptions<TFn, TSelected>, selector?: (state: ThrottlerState<TFn>) => TSelected): SolidThrottler<TFn, TSelected>;
127
146
  //#endregion
128
- export { SolidThrottler, createThrottler };
147
+ export { SolidThrottler, SolidThrottlerOptions, createThrottler };
129
148
  //# sourceMappingURL=createThrottler.d.cts.map
@@ -4,6 +4,13 @@ import { Throttler, ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/thr
4
4
  import { AnyFunction } from "@tanstack/pacer/types";
5
5
 
6
6
  //#region src/throttler/createThrottler.d.ts
7
+ interface SolidThrottlerOptions<TFn extends AnyFunction, TSelected = {}> extends ThrottlerOptions<TFn> {
8
+ /**
9
+ * Optional callback invoked when the owning component unmounts. Receives the throttler instance.
10
+ * When provided, replaces the default cleanup (cancel); use it to call flush(), reset(), cancel(), add logging, etc.
11
+ */
12
+ onUnmount?: (throttler: SolidThrottler<TFn, TSelected>) => void;
13
+ }
7
14
  interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<Throttler<TFn>, 'store'> {
8
15
  /**
9
16
  * A Solid component that allows you to subscribe to the throttler state.
@@ -77,6 +84,18 @@ interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<T
77
84
  * - `nextExecutionTime`: Timestamp of the next allowed execution
78
85
  * - `status`: Current execution status ('disabled' | 'idle' | 'pending')
79
86
  *
87
+ * ## Unmount behavior
88
+ *
89
+ * By default, the primitive cancels any pending execution when the owning component unmounts.
90
+ * Use the `onUnmount` option to customize this. For example, to flush pending work instead:
91
+ *
92
+ * ```tsx
93
+ * const throttler = createThrottler(fn, {
94
+ * wait: 1000,
95
+ * onUnmount: (t) => t.flush()
96
+ * });
97
+ * ```
98
+ *
80
99
  * @example
81
100
  * ```tsx
82
101
  * // Default behavior - no reactive state subscriptions
@@ -123,7 +142,7 @@ interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<T
123
142
  * const { isPending, executionCount } = throttler.state();
124
143
  * ```
125
144
  */
126
- declare function createThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: ThrottlerOptions<TFn>, selector?: (state: ThrottlerState<TFn>) => TSelected): SolidThrottler<TFn, TSelected>;
145
+ declare function createThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: SolidThrottlerOptions<TFn, TSelected>, selector?: (state: ThrottlerState<TFn>) => TSelected): SolidThrottler<TFn, TSelected>;
127
146
  //#endregion
128
- export { SolidThrottler, createThrottler };
147
+ export { SolidThrottler, SolidThrottlerOptions, createThrottler };
129
148
  //# sourceMappingURL=createThrottler.d.ts.map
@@ -46,6 +46,18 @@ import { Throttler } from "@tanstack/pacer/throttler";
46
46
  * - `nextExecutionTime`: Timestamp of the next allowed execution
47
47
  * - `status`: Current execution status ('disabled' | 'idle' | 'pending')
48
48
  *
49
+ * ## Unmount behavior
50
+ *
51
+ * By default, the primitive cancels any pending execution when the owning component unmounts.
52
+ * Use the `onUnmount` option to customize this. For example, to flush pending work instead:
53
+ *
54
+ * ```tsx
55
+ * const throttler = createThrottler(fn, {
56
+ * wait: 1000,
57
+ * onUnmount: (t) => t.flush()
58
+ * });
59
+ * ```
60
+ *
49
61
  * @example
50
62
  * ```tsx
51
63
  * // Default behavior - no reactive state subscriptions
@@ -93,10 +105,11 @@ import { Throttler } from "@tanstack/pacer/throttler";
93
105
  * ```
94
106
  */
95
107
  function createThrottler(fn, options, selector = () => ({})) {
96
- const asyncThrottler = new Throttler(fn, {
108
+ const mergedOptions = {
97
109
  ...useDefaultPacerOptions().throttler,
98
110
  ...options
99
- });
111
+ };
112
+ const asyncThrottler = new Throttler(fn, mergedOptions);
100
113
  asyncThrottler.Subscribe = function Subscribe(props) {
101
114
  const selected = useStore(asyncThrottler.store, props.selector);
102
115
  return typeof props.children === "function" ? props.children(selected) : props.children;
@@ -104,7 +117,8 @@ function createThrottler(fn, options, selector = () => ({})) {
104
117
  const state = useStore(asyncThrottler.store, selector);
105
118
  createEffect(() => {
106
119
  onCleanup(() => {
107
- asyncThrottler.cancel();
120
+ if (mergedOptions.onUnmount) mergedOptions.onUnmount(asyncThrottler);
121
+ else asyncThrottler.cancel();
108
122
  });
109
123
  });
110
124
  return {
@@ -1 +1 @@
1
- {"version":3,"file":"createThrottler.js","names":[],"sources":["../../src/throttler/createThrottler.ts"],"sourcesContent":["import { Throttler } from '@tanstack/pacer/throttler'\nimport { createEffect, onCleanup } from 'solid-js'\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 ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\nexport interface SolidThrottler<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Throttler<TFn>, 'store'> {\n /**\n * A Solid component that allows you to subscribe to the throttler state.\n *\n * This is useful for tracking specific parts of the throttler state\n * deep in your component tree without needing to pass a selector to the hook.\n *\n * @example\n * <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>\n * {(state) => (\n * <div>{state().isPending ? 'Loading...' : 'Ready'}</div>\n * )}\n * </throttler.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: ThrottlerState<TFn>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) => JSX.Element\n /**\n * Reactive state that will be updated when the throttler state changes\n *\n * Use this instead of `throttler.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `throttler.state` instead of `throttler.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<ThrottlerState<TFn>>>\n}\n\n/**\n * A low-level Solid hook that creates a `Throttler` instance that limits how often the provided function can execute.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that\n * you can integrate with any state management solution (createSignal, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that\n * integrates directly with Solid's createSignal, see createThrottledSignal.\n *\n * Throttling ensures a function executes at most once within a specified time window,\n * regardless of how many times it is called. This is useful for rate-limiting\n * expensive operations or UI updates.\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 `throttler.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 * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last execution\n * - `nextExecutionTime`: Timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const throttler = createThrottler(setValue, { wait: 1000 });\n *\n * // Subscribe to state changes deep in component tree using Subscribe component\n * <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>\n * {(state) => (\n * <div>{state().isPending ? 'Loading...' : 'Ready'}</div>\n * )}\n * </throttler.Subscribe>\n *\n * // Opt-in to track isPending changes at hook level (optimized for loading states)\n * const throttler = createThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Opt-in to track executionCount changes (optimized for tracking execution)\n * const throttler = createThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Multiple state properties - track when any of these change\n * const throttler = createThrottler(\n * setValue,\n * {\n * wait: 2000,\n * leading: true, // Execute immediately on first call\n * trailing: false // Skip trailing edge updates\n * },\n * (state) => ({\n * isPending: state.isPending,\n * executionCount: state.executionCount,\n * lastExecutionTime: state.lastExecutionTime,\n * nextExecutionTime: state.nextExecutionTime\n * })\n * );\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isPending, executionCount } = throttler.state();\n * ```\n */\nexport function createThrottler<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: ThrottlerOptions<TFn>,\n selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,\n): SolidThrottler<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().throttler,\n ...options,\n } as ThrottlerOptions<TFn>\n\n const asyncThrottler = new Throttler<TFn>(\n fn,\n mergedOptions,\n ) as unknown as SolidThrottler<TFn, TSelected>\n\n asyncThrottler.Subscribe = function Subscribe<TSelected>(props: {\n selector: (state: ThrottlerState<TFn>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) {\n const selected = useStore(asyncThrottler.store, props.selector)\n\n return typeof props.children === 'function'\n ? props.children(selected)\n : props.children\n }\n\n const state = useStore(asyncThrottler.store, selector)\n\n createEffect(() => {\n onCleanup(() => {\n asyncThrottler.cancel()\n })\n })\n\n return {\n ...asyncThrottler,\n state,\n } as SolidThrottler<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuIA,SAAgB,gBACd,IACA,SACA,kBAA6D,EAAE,GAC/B;CAMhC,MAAM,iBAAiB,IAAI,UACzB,IANoB;EACpB,GAAG,wBAAwB,CAAC;EAC5B,GAAG;EACJ,CAKA;AAED,gBAAe,YAAY,SAAS,UAAqB,OAGtD;EACD,MAAM,WAAW,SAAS,eAAe,OAAO,MAAM,SAAS;AAE/D,SAAO,OAAO,MAAM,aAAa,aAC7B,MAAM,SAAS,SAAS,GACxB,MAAM;;CAGZ,MAAM,QAAQ,SAAS,eAAe,OAAO,SAAS;AAEtD,oBAAmB;AACjB,kBAAgB;AACd,kBAAe,QAAQ;IACvB;GACF;AAEF,QAAO;EACL,GAAG;EACH;EACD"}
1
+ {"version":3,"file":"createThrottler.js","names":[],"sources":["../../src/throttler/createThrottler.ts"],"sourcesContent":["import { Throttler } from '@tanstack/pacer/throttler'\nimport { createEffect, onCleanup } from 'solid-js'\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 ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\nexport interface SolidThrottlerOptions<\n TFn extends AnyFunction,\n TSelected = {},\n> extends ThrottlerOptions<TFn> {\n /**\n * Optional callback invoked when the owning component unmounts. Receives the throttler instance.\n * When provided, replaces the default cleanup (cancel); use it to call flush(), reset(), cancel(), add logging, etc.\n */\n onUnmount?: (throttler: SolidThrottler<TFn, TSelected>) => void\n}\n\nexport interface SolidThrottler<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Throttler<TFn>, 'store'> {\n /**\n * A Solid component that allows you to subscribe to the throttler state.\n *\n * This is useful for tracking specific parts of the throttler state\n * deep in your component tree without needing to pass a selector to the hook.\n *\n * @example\n * <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>\n * {(state) => (\n * <div>{state().isPending ? 'Loading...' : 'Ready'}</div>\n * )}\n * </throttler.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: ThrottlerState<TFn>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) => JSX.Element\n /**\n * Reactive state that will be updated when the throttler state changes\n *\n * Use this instead of `throttler.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `throttler.state` instead of `throttler.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<ThrottlerState<TFn>>>\n}\n\n/**\n * A low-level Solid hook that creates a `Throttler` instance that limits how often the provided function can execute.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that\n * you can integrate with any state management solution (createSignal, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that\n * integrates directly with Solid's createSignal, see createThrottledSignal.\n *\n * Throttling ensures a function executes at most once within a specified time window,\n * regardless of how many times it is called. This is useful for rate-limiting\n * expensive operations or UI updates.\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 `throttler.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 * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last execution\n * - `nextExecutionTime`: Timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * ## Unmount behavior\n *\n * By default, the primitive cancels any pending execution when the owning component unmounts.\n * Use the `onUnmount` option to customize this. For example, to flush pending work instead:\n *\n * ```tsx\n * const throttler = createThrottler(fn, {\n * wait: 1000,\n * onUnmount: (t) => t.flush()\n * });\n * ```\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const throttler = createThrottler(setValue, { wait: 1000 });\n *\n * // Subscribe to state changes deep in component tree using Subscribe component\n * <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>\n * {(state) => (\n * <div>{state().isPending ? 'Loading...' : 'Ready'}</div>\n * )}\n * </throttler.Subscribe>\n *\n * // Opt-in to track isPending changes at hook level (optimized for loading states)\n * const throttler = createThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Opt-in to track executionCount changes (optimized for tracking execution)\n * const throttler = createThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Multiple state properties - track when any of these change\n * const throttler = createThrottler(\n * setValue,\n * {\n * wait: 2000,\n * leading: true, // Execute immediately on first call\n * trailing: false // Skip trailing edge updates\n * },\n * (state) => ({\n * isPending: state.isPending,\n * executionCount: state.executionCount,\n * lastExecutionTime: state.lastExecutionTime,\n * nextExecutionTime: state.nextExecutionTime\n * })\n * );\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isPending, executionCount } = throttler.state();\n * ```\n */\nexport function createThrottler<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: SolidThrottlerOptions<TFn, TSelected>,\n selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,\n): SolidThrottler<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().throttler,\n ...options,\n } as SolidThrottlerOptions<TFn, TSelected>\n const asyncThrottler = new Throttler<TFn>(\n fn,\n mergedOptions,\n ) as unknown as SolidThrottler<TFn, TSelected>\n\n asyncThrottler.Subscribe = function Subscribe<TSelected>(props: {\n selector: (state: ThrottlerState<TFn>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) {\n const selected = useStore(asyncThrottler.store, props.selector)\n\n return typeof props.children === 'function'\n ? props.children(selected)\n : props.children\n }\n\n const state = useStore(asyncThrottler.store, selector)\n\n createEffect(() => {\n onCleanup(() => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(asyncThrottler)\n } else {\n asyncThrottler.cancel()\n }\n })\n })\n\n return {\n ...asyncThrottler,\n state,\n } as SolidThrottler<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8JA,SAAgB,gBACd,IACA,SACA,kBAA6D,EAAE,GAC/B;CAChC,MAAM,gBAAgB;EACpB,GAAG,wBAAwB,CAAC;EAC5B,GAAG;EACJ;CACD,MAAM,iBAAiB,IAAI,UACzB,IACA,cACD;AAED,gBAAe,YAAY,SAAS,UAAqB,OAGtD;EACD,MAAM,WAAW,SAAS,eAAe,OAAO,MAAM,SAAS;AAE/D,SAAO,OAAO,MAAM,aAAa,aAC7B,MAAM,SAAS,SAAS,GACxB,MAAM;;CAGZ,MAAM,QAAQ,SAAS,eAAe,OAAO,SAAS;AAEtD,oBAAmB;AACjB,kBAAgB;AACd,OAAI,cAAc,UAChB,eAAc,UAAU,eAAe;OAEvC,gBAAe,QAAQ;IAEzB;GACF;AAEF,QAAO;EACL,GAAG;EACH;EACD"}
@@ -1,3 +1,4 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1
2
  const require_createThrottler = require('./createThrottler.cjs');
2
3
  const require_createThrottledSignal = require('./createThrottledSignal.cjs');
3
4
  const require_createThrottledValue = require('./createThrottledValue.cjs');
@@ -1,5 +1,5 @@
1
- import { SolidThrottler, createThrottler } from "./createThrottler.cjs";
1
+ import { SolidThrottler, SolidThrottlerOptions, createThrottler } from "./createThrottler.cjs";
2
2
  import { createThrottledSignal } from "./createThrottledSignal.cjs";
3
3
  import { createThrottledValue } from "./createThrottledValue.cjs";
4
4
  export * from "@tanstack/pacer/throttler";
5
- export { SolidThrottler, createThrottledSignal, createThrottledValue, createThrottler };
5
+ export { SolidThrottler, SolidThrottlerOptions, createThrottledSignal, createThrottledValue, createThrottler };
@@ -1,5 +1,5 @@
1
- import { SolidThrottler, createThrottler } from "./createThrottler.js";
1
+ import { SolidThrottler, SolidThrottlerOptions, createThrottler } from "./createThrottler.js";
2
2
  import { createThrottledSignal } from "./createThrottledSignal.js";
3
3
  import { createThrottledValue } from "./createThrottledValue.js";
4
4
  export * from "@tanstack/pacer/throttler";
5
- export { SolidThrottler, createThrottledSignal, createThrottledValue, createThrottler };
5
+ export { SolidThrottler, SolidThrottlerOptions, createThrottledSignal, createThrottledValue, createThrottler };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/solid-pacer",
3
- "version": "0.18.4",
3
+ "version": "0.19.0",
4
4
  "description": "Utilities for debouncing and throttling functions in Solid.",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -28,60 +28,60 @@
28
28
  "types": "./dist/index.d.cts",
29
29
  "exports": {
30
30
  ".": {
31
- "require": "./dist/index.cjs",
32
- "import": "./dist/index.js"
31
+ "import": "./dist/index.js",
32
+ "require": "./dist/index.cjs"
33
33
  },
34
34
  "./async-batcher": {
35
- "require": "./dist/async-batcher/index.cjs",
36
- "import": "./dist/async-batcher/index.js"
35
+ "import": "./dist/async-batcher/index.js",
36
+ "require": "./dist/async-batcher/index.cjs"
37
37
  },
38
38
  "./async-debouncer": {
39
- "require": "./dist/async-debouncer/index.cjs",
40
- "import": "./dist/async-debouncer/index.js"
39
+ "import": "./dist/async-debouncer/index.js",
40
+ "require": "./dist/async-debouncer/index.cjs"
41
41
  },
42
42
  "./async-queuer": {
43
- "require": "./dist/async-queuer/index.cjs",
44
- "import": "./dist/async-queuer/index.js"
43
+ "import": "./dist/async-queuer/index.js",
44
+ "require": "./dist/async-queuer/index.cjs"
45
45
  },
46
46
  "./async-rate-limiter": {
47
- "require": "./dist/async-rate-limiter/index.cjs",
48
- "import": "./dist/async-rate-limiter/index.js"
47
+ "import": "./dist/async-rate-limiter/index.js",
48
+ "require": "./dist/async-rate-limiter/index.cjs"
49
49
  },
50
50
  "./async-throttler": {
51
- "require": "./dist/async-throttler/index.cjs",
52
- "import": "./dist/async-throttler/index.js"
51
+ "import": "./dist/async-throttler/index.js",
52
+ "require": "./dist/async-throttler/index.cjs"
53
53
  },
54
54
  "./batcher": {
55
- "require": "./dist/batcher/index.cjs",
56
- "import": "./dist/batcher/index.js"
55
+ "import": "./dist/batcher/index.js",
56
+ "require": "./dist/batcher/index.cjs"
57
57
  },
58
58
  "./debouncer": {
59
- "require": "./dist/debouncer/index.cjs",
60
- "import": "./dist/debouncer/index.js"
59
+ "import": "./dist/debouncer/index.js",
60
+ "require": "./dist/debouncer/index.cjs"
61
61
  },
62
62
  "./provider": {
63
- "require": "./dist/provider/index.cjs",
64
- "import": "./dist/provider/index.js"
63
+ "import": "./dist/provider/index.js",
64
+ "require": "./dist/provider/index.cjs"
65
65
  },
66
66
  "./queuer": {
67
- "require": "./dist/queuer/index.cjs",
68
- "import": "./dist/queuer/index.js"
67
+ "import": "./dist/queuer/index.js",
68
+ "require": "./dist/queuer/index.cjs"
69
69
  },
70
70
  "./rate-limiter": {
71
- "require": "./dist/rate-limiter/index.cjs",
72
- "import": "./dist/rate-limiter/index.js"
71
+ "import": "./dist/rate-limiter/index.js",
72
+ "require": "./dist/rate-limiter/index.cjs"
73
73
  },
74
74
  "./throttler": {
75
- "require": "./dist/throttler/index.cjs",
76
- "import": "./dist/throttler/index.js"
75
+ "import": "./dist/throttler/index.js",
76
+ "require": "./dist/throttler/index.cjs"
77
77
  },
78
78
  "./types": {
79
- "require": "./dist/types/index.cjs",
80
- "import": "./dist/types/index.js"
79
+ "import": "./dist/types/index.js",
80
+ "require": "./dist/types/index.cjs"
81
81
  },
82
82
  "./utils": {
83
- "require": "./dist/utils/index.cjs",
84
- "import": "./dist/utils/index.js"
83
+ "import": "./dist/utils/index.js",
84
+ "require": "./dist/utils/index.cjs"
85
85
  },
86
86
  "./package.json": "./package.json"
87
87
  },
@@ -94,8 +94,8 @@
94
94
  "src"
95
95
  ],
96
96
  "dependencies": {
97
- "@tanstack/solid-store": "^0.8.0",
98
- "@tanstack/pacer": "0.18.0"
97
+ "@tanstack/solid-store": "^0.8.1",
98
+ "@tanstack/pacer": "0.19.0"
99
99
  },
100
100
  "devDependencies": {
101
101
  "solid-js": "^1.9.11",
@@ -106,6 +106,7 @@
106
106
  },
107
107
  "scripts": {
108
108
  "clean": "premove ./build ./dist",
109
+ "lint:fix": "eslint ./src --fix",
109
110
  "test:eslint": "eslint ./src",
110
111
  "test:lib": "vitest --passWithNoTests",
111
112
  "test:lib:dev": "pnpm test:lib --watch",
@@ -1,5 +1,6 @@
1
1
  import { AsyncBatcher } from '@tanstack/pacer/async-batcher'
2
2
  import { useStore } from '@tanstack/solid-store'
3
+ import { createEffect, onCleanup } from 'solid-js'
3
4
  import { useDefaultPacerOptions } from '../provider/PacerProvider'
4
5
  import type { Store } from '@tanstack/solid-store'
5
6
  import type { Accessor, JSX } from 'solid-js'
@@ -8,6 +9,17 @@ import type {
8
9
  AsyncBatcherState,
9
10
  } from '@tanstack/pacer/async-batcher'
10
11
 
12
+ export interface SolidAsyncBatcherOptions<
13
+ TValue,
14
+ TSelected = {},
15
+ > extends AsyncBatcherOptions<TValue> {
16
+ /**
17
+ * Optional callback invoked when the owning component unmounts. Receives the batcher instance.
18
+ * When provided, replaces the default cleanup (cancel + abort); use it to call flush(), reset(), cancel(), add logging, etc.
19
+ */
20
+ onUnmount?: (batcher: SolidAsyncBatcher<TValue, TSelected>) => void
21
+ }
22
+
11
23
  export interface SolidAsyncBatcher<TValue, TSelected = {}> extends Omit<
12
24
  AsyncBatcher<TValue>,
13
25
  'store'
@@ -105,6 +117,25 @@ export interface SolidAsyncBatcher<TValue, TSelected = {}> extends Omit<
105
117
  * - `settleCount`: Number of batch executions that have completed (successful or failed)
106
118
  * - `successCount`: Number of successful batch executions
107
119
  *
120
+ * ## Unmount behavior
121
+ *
122
+ * By default, the primitive cancels any pending batch and aborts any in-flight execution when the owning component unmounts.
123
+ * Abort only cancels underlying operations (e.g. fetch) when the abort signal from `getAbortSignal()` is passed to them.
124
+ * Use the `onUnmount` option to customize this. For example, to flush pending work instead:
125
+ *
126
+ * ```tsx
127
+ * const batcher = createAsyncBatcher(fn, {
128
+ * maxSize: 10,
129
+ * wait: 2000,
130
+ * onUnmount: (b) => b.flush()
131
+ * });
132
+ * ```
133
+ *
134
+ * Note: For async utils, `flush()` returns a Promise and runs fire-and-forget in the cleanup.
135
+ * If your batch function updates Solid signals, those updates may run after the component has
136
+ * unmounted, which can cause unexpected reactive updates. Guard your callbacks accordingly when
137
+ * using onUnmount with flush.
138
+ *
108
139
  * Example usage:
109
140
  * ```tsx
110
141
  * // Default behavior - no reactive state subscriptions
@@ -157,15 +188,14 @@ export interface SolidAsyncBatcher<TValue, TSelected = {}> extends Omit<
157
188
  */
158
189
  export function createAsyncBatcher<TValue, TSelected = {}>(
159
190
  fn: (items: Array<TValue>) => Promise<any>,
160
- options: AsyncBatcherOptions<TValue> = {},
191
+ options: SolidAsyncBatcherOptions<TValue, TSelected> = {},
161
192
  selector: (state: AsyncBatcherState<TValue>) => TSelected = () =>
162
193
  ({}) as TSelected,
163
194
  ): SolidAsyncBatcher<TValue, TSelected> {
164
195
  const mergedOptions = {
165
196
  ...useDefaultPacerOptions().asyncBatcher,
166
197
  ...options,
167
- } as AsyncBatcherOptions<TValue>
168
-
198
+ } as SolidAsyncBatcherOptions<TValue, TSelected>
169
199
  const asyncBatcher = new AsyncBatcher<TValue>(
170
200
  fn,
171
201
  mergedOptions,
@@ -184,6 +214,17 @@ export function createAsyncBatcher<TValue, TSelected = {}>(
184
214
 
185
215
  const state = useStore(asyncBatcher.store, selector)
186
216
 
217
+ createEffect(() => {
218
+ onCleanup(() => {
219
+ if (mergedOptions.onUnmount) {
220
+ mergedOptions.onUnmount(asyncBatcher)
221
+ } else {
222
+ asyncBatcher.cancel()
223
+ asyncBatcher.abort()
224
+ }
225
+ })
226
+ })
227
+
187
228
  return {
188
229
  ...asyncBatcher,
189
230
  state,
@@ -10,6 +10,17 @@ import type {
10
10
  } from '@tanstack/pacer/async-debouncer'
11
11
  import type { AnyAsyncFunction } from '@tanstack/pacer/types'
12
12
 
13
+ export interface SolidAsyncDebouncerOptions<
14
+ TFn extends AnyAsyncFunction,
15
+ TSelected = {},
16
+ > extends AsyncDebouncerOptions<TFn> {
17
+ /**
18
+ * Optional callback invoked when the owning component unmounts. Receives the debouncer instance.
19
+ * When provided, replaces the default cleanup (cancel + abort); use it to call flush(), reset(), cancel(), add logging, etc.
20
+ */
21
+ onUnmount?: (debouncer: SolidAsyncDebouncer<TFn, TSelected>) => void
22
+ }
23
+
13
24
  export interface SolidAsyncDebouncer<
14
25
  TFn extends AnyAsyncFunction,
15
26
  TSelected = {},
@@ -101,6 +112,24 @@ export interface SolidAsyncDebouncer<
101
112
  * - `lastResult`: The result from the most recent successful execution
102
113
  * - `status`: Current execution status ('disabled' | 'idle' | 'pending' | 'executing')
103
114
  *
115
+ * ## Unmount behavior
116
+ *
117
+ * By default, the primitive cancels any pending execution and aborts any in-flight execution when the owning component unmounts.
118
+ * Abort only cancels underlying operations (e.g. fetch) when the abort signal from `getAbortSignal()` is passed to them.
119
+ * Use the `onUnmount` option to customize this. For example, to flush pending work instead:
120
+ *
121
+ * ```tsx
122
+ * const debouncer = createAsyncDebouncer(fn, {
123
+ * wait: 500,
124
+ * onUnmount: (d) => d.flush()
125
+ * });
126
+ * ```
127
+ *
128
+ * Note: For async utils, `flush()` returns a Promise and runs fire-and-forget in the cleanup.
129
+ * If your debounced function updates Solid signals, those updates may run after the component has
130
+ * unmounted, which can cause unexpected reactive updates. Guard your callbacks accordingly when
131
+ * using onUnmount with flush.
132
+ *
104
133
  * @example
105
134
  * ```tsx
106
135
  * // Default behavior - no reactive state subscriptions
@@ -148,15 +177,14 @@ export function createAsyncDebouncer<
148
177
  TSelected = {},
149
178
  >(
150
179
  fn: TFn,
151
- options: AsyncDebouncerOptions<TFn>,
180
+ options: SolidAsyncDebouncerOptions<TFn, TSelected>,
152
181
  selector: (state: AsyncDebouncerState<TFn>) => TSelected = () =>
153
182
  ({}) as TSelected,
154
183
  ): SolidAsyncDebouncer<TFn, TSelected> {
155
184
  const mergedOptions = {
156
185
  ...useDefaultPacerOptions().asyncDebouncer,
157
186
  ...options,
158
- } as AsyncDebouncerOptions<TFn>
159
-
187
+ } as SolidAsyncDebouncerOptions<TFn, TSelected>
160
188
  const asyncDebouncer = new AsyncDebouncer<TFn>(
161
189
  fn,
162
190
  mergedOptions,
@@ -177,7 +205,12 @@ export function createAsyncDebouncer<
177
205
 
178
206
  createEffect(() => {
179
207
  onCleanup(() => {
180
- asyncDebouncer.cancel()
208
+ if (mergedOptions.onUnmount) {
209
+ mergedOptions.onUnmount(asyncDebouncer)
210
+ } else {
211
+ asyncDebouncer.cancel()
212
+ asyncDebouncer.abort()
213
+ }
181
214
  })
182
215
  })
183
216
 
@@ -1,5 +1,6 @@
1
1
  import { AsyncQueuer } from '@tanstack/pacer/async-queuer'
2
2
  import { useStore } from '@tanstack/solid-store'
3
+ import { createEffect, onCleanup } from 'solid-js'
3
4
  import { useDefaultPacerOptions } from '../provider/PacerProvider'
4
5
  import type { Store } from '@tanstack/solid-store'
5
6
  import type { Accessor, JSX } from 'solid-js'
@@ -8,6 +9,17 @@ import type {
8
9
  AsyncQueuerState,
9
10
  } from '@tanstack/pacer/async-queuer'
10
11
 
12
+ export interface SolidAsyncQueuerOptions<
13
+ TValue,
14
+ TSelected = {},
15
+ > extends AsyncQueuerOptions<TValue> {
16
+ /**
17
+ * Optional callback invoked when the owning component unmounts. Receives the queuer instance.
18
+ * When provided, replaces the default cleanup (stop + abort); use it to call flush(), flushAsBatch(), stop(), add logging, etc.
19
+ */
20
+ onUnmount?: (queuer: SolidAsyncQueuer<TValue, TSelected>) => void
21
+ }
22
+
11
23
  export interface SolidAsyncQueuer<TValue, TSelected = {}> extends Omit<
12
24
  AsyncQueuer<TValue>,
13
25
  'store'
@@ -96,6 +108,25 @@ export interface SolidAsyncQueuer<TValue, TSelected = {}> extends Omit<
96
108
  * - `settleCount`: Number of items that have completed processing (successful or failed)
97
109
  * - `successCount`: Number of items that were processed successfully
98
110
  *
111
+ * ## Unmount behavior
112
+ *
113
+ * By default, the primitive stops the queuer and aborts any in-flight task executions when the owning component unmounts.
114
+ * Abort only cancels underlying operations (e.g. fetch) when the abort signal from `getAbortSignal()` is passed to them.
115
+ * Use the `onUnmount` option to customize this. For example, to flush pending items instead:
116
+ *
117
+ * ```tsx
118
+ * const queuer = createAsyncQueuer(fn, {
119
+ * concurrency: 2,
120
+ * started: false,
121
+ * onUnmount: (q) => q.flush()
122
+ * });
123
+ * ```
124
+ *
125
+ * Note: For async utils, `flush()` returns a Promise and runs fire-and-forget in the cleanup.
126
+ * If your task function updates Solid signals, those updates may run after the component has
127
+ * unmounted, which can cause unexpected reactive updates. Guard your callbacks accordingly when
128
+ * using onUnmount with flush.
129
+ *
99
130
  * Example usage:
100
131
  * ```tsx
101
132
  * // Default behavior - no reactive state subscriptions
@@ -149,15 +180,14 @@ export interface SolidAsyncQueuer<TValue, TSelected = {}> extends Omit<
149
180
  */
150
181
  export function createAsyncQueuer<TValue, TSelected = {}>(
151
182
  fn: (value: TValue) => Promise<any>,
152
- options: AsyncQueuerOptions<TValue> = {},
183
+ options: SolidAsyncQueuerOptions<TValue, TSelected> = {},
153
184
  selector: (state: AsyncQueuerState<TValue>) => TSelected = () =>
154
185
  ({}) as TSelected,
155
186
  ): SolidAsyncQueuer<TValue, TSelected> {
156
187
  const mergedOptions = {
157
188
  ...useDefaultPacerOptions().asyncQueuer,
158
189
  ...options,
159
- } as AsyncQueuerOptions<TValue>
160
-
190
+ } as SolidAsyncQueuerOptions<TValue, TSelected>
161
191
  const asyncQueuer = new AsyncQueuer<TValue>(
162
192
  fn,
163
193
  mergedOptions,
@@ -176,6 +206,17 @@ export function createAsyncQueuer<TValue, TSelected = {}>(
176
206
 
177
207
  const state = useStore(asyncQueuer.store, selector)
178
208
 
209
+ createEffect(() => {
210
+ onCleanup(() => {
211
+ if (mergedOptions.onUnmount) {
212
+ mergedOptions.onUnmount(asyncQueuer)
213
+ } else {
214
+ asyncQueuer.stop()
215
+ asyncQueuer.abort()
216
+ }
217
+ })
218
+ })
219
+
179
220
  return {
180
221
  ...asyncQueuer,
181
222
  state,
@@ -1,4 +1,5 @@
1
1
  import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'
2
+ import { createEffect, onCleanup } from 'solid-js'
2
3
  import { useStore } from '@tanstack/solid-store'
3
4
  import { useDefaultPacerOptions } from '../provider/PacerProvider'
4
5
  import type { Store } from '@tanstack/solid-store'
@@ -9,6 +10,17 @@ import type {
9
10
  AsyncRateLimiterState,
10
11
  } from '@tanstack/pacer/async-rate-limiter'
11
12
 
13
+ export interface SolidAsyncRateLimiterOptions<
14
+ TFn extends AnyAsyncFunction,
15
+ TSelected = {},
16
+ > extends AsyncRateLimiterOptions<TFn> {
17
+ /**
18
+ * Optional callback invoked when the owning component unmounts. Receives the rate limiter instance.
19
+ * When provided, replaces the default cleanup (abort); use it to call reset(), add logging, etc.
20
+ */
21
+ onUnmount?: (rateLimiter: SolidAsyncRateLimiter<TFn, TSelected>) => void
22
+ }
23
+
12
24
  export interface SolidAsyncRateLimiter<
13
25
  TFn extends AnyAsyncFunction,
14
26
  TSelected = {},
@@ -111,6 +123,12 @@ export interface SolidAsyncRateLimiter<
111
123
  * - `rejectionCount`: Number of function calls that were rejected due to rate limiting
112
124
  * - `remainingInWindow`: Number of executions remaining in the current window
113
125
  *
126
+ * ## Unmount behavior
127
+ *
128
+ * By default, the primitive aborts any in-flight execution when the owning component unmounts.
129
+ * Abort only cancels underlying operations (e.g. fetch) when the abort signal from `getAbortSignal()` is passed to them.
130
+ * Use the `onUnmount` option to customize this.
131
+ *
114
132
  * @example
115
133
  * ```tsx
116
134
  * // Default behavior - no reactive state subscriptions
@@ -204,15 +222,14 @@ export function createAsyncRateLimiter<
204
222
  TSelected = {},
205
223
  >(
206
224
  fn: TFn,
207
- options: AsyncRateLimiterOptions<TFn>,
225
+ options: SolidAsyncRateLimiterOptions<TFn, TSelected>,
208
226
  selector: (state: AsyncRateLimiterState<TFn>) => TSelected = () =>
209
227
  ({}) as TSelected,
210
228
  ): SolidAsyncRateLimiter<TFn, TSelected> {
211
229
  const mergedOptions = {
212
230
  ...useDefaultPacerOptions().asyncRateLimiter,
213
231
  ...options,
214
- } as AsyncRateLimiterOptions<TFn>
215
-
232
+ } as SolidAsyncRateLimiterOptions<TFn, TSelected>
216
233
  const asyncRateLimiter = new AsyncRateLimiter<TFn>(
217
234
  fn,
218
235
  mergedOptions,
@@ -231,6 +248,16 @@ export function createAsyncRateLimiter<
231
248
 
232
249
  const state = useStore(asyncRateLimiter.store, selector)
233
250
 
251
+ createEffect(() => {
252
+ onCleanup(() => {
253
+ if (mergedOptions.onUnmount) {
254
+ mergedOptions.onUnmount(asyncRateLimiter)
255
+ } else {
256
+ asyncRateLimiter.abort()
257
+ }
258
+ })
259
+ })
260
+
234
261
  return {
235
262
  ...asyncRateLimiter,
236
263
  state,