@tanstack/solid-pacer 0.3.0 → 0.4.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.
@@ -1 +1 @@
1
- {"version":3,"file":"createAsyncRateLimiter.cjs","sources":["../../../src/async-rate-limiter/createAsyncRateLimiter.ts"],"sourcesContent":["import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type { AsyncRateLimiterOptions } from '@tanstack/pacer/async-rate-limiter'\n\nexport interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction>\n extends Omit<\n AsyncRateLimiter<TFn>,\n | 'getSuccessCount'\n | 'getSettleCount'\n | 'getErrorCount'\n | 'getRejectionCount'\n | 'getRemainingInWindow'\n | 'getMsUntilNextWindow'\n > {\n successCount: Accessor<number>\n settleCount: Accessor<number>\n errorCount: Accessor<number>\n rejectionCount: Accessor<number>\n remainingInWindow: Accessor<number>\n msUntilNextWindow: Accessor<number>\n}\n\n/**\n * A low-level Solid hook that creates an `AsyncRateLimiter` instance to limit how many times an async function can execute within a time window.\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 allows an async function to execute up to a specified limit within a time window,\n * then blocks subsequent calls until the window passes. This is useful for respecting API rate limits,\n * managing resource constraints, or controlling bursts of async operations.\n *\n * @example\n * ```tsx\n * // Basic API call rate limiting\n * const { maybeExecute } = createAsyncRateLimiter(\n * async (id: string) => {\n * const data = await api.fetchData(id);\n * return data;\n * },\n * { limit: 5, window: 1000 } // 5 calls per second\n * );\n *\n * // With state management\n * const [data, setData] = createSignal(null);\n * const { maybeExecute } = createAsyncRateLimiter(\n * async (query) => {\n * const result = await searchAPI(query);\n * setData(result);\n * },\n * {\n * limit: 10,\n * window: 60000, // 10 calls per minute\n * onReject: (info) => console.log(`Rate limit exceeded: ${info.nextValidTime - Date.now()}ms until next window`)\n * }\n * );\n * ```\n */\nexport function createAsyncRateLimiter<TFn extends AnyAsyncFunction>(\n fn: TFn,\n initialOptions: AsyncRateLimiterOptions<TFn>,\n): SolidAsyncRateLimiter<TFn> {\n const asyncRateLimiter = new AsyncRateLimiter<TFn>(fn, initialOptions)\n\n const [successCount, setSuccessCount] = createSignal(\n asyncRateLimiter.getSuccessCount(),\n )\n const [rejectionCount, setRejectionCount] = createSignal(\n asyncRateLimiter.getRejectionCount(),\n )\n const [errorCount, setErrorCount] = createSignal(\n asyncRateLimiter.getErrorCount(),\n )\n const [settleCount, setSettleCount] = createSignal(\n asyncRateLimiter.getSettleCount(),\n )\n const [remainingInWindow, setRemainingInWindow] = createSignal(\n asyncRateLimiter.getRemainingInWindow(),\n )\n const [msUntilNextWindow, setMsUntilNextWindow] = createSignal(\n asyncRateLimiter.getMsUntilNextWindow(),\n )\n\n function setOptions(newOptions: Partial<AsyncRateLimiterOptions<TFn>>) {\n asyncRateLimiter.setOptions({\n ...newOptions,\n onSettled: (rateLimiter) => {\n setSuccessCount(rateLimiter.getSuccessCount())\n setSettleCount(rateLimiter.getSettleCount())\n setErrorCount(rateLimiter.getErrorCount())\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onSettled = newOptions.onSettled ?? initialOptions.onSettled\n onSettled?.(rateLimiter)\n },\n onReject: (rateLimiter) => {\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onReject = newOptions.onReject ?? initialOptions.onReject\n onReject?.(rateLimiter)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...bindInstanceMethods(asyncRateLimiter),\n errorCount,\n remainingInWindow,\n msUntilNextWindow,\n rejectionCount,\n setOptions,\n settleCount,\n successCount,\n } as SolidAsyncRateLimiter<TFn>\n}\n"],"names":["asyncRateLimiter","AsyncRateLimiter","createSignal","bindInstanceMethods"],"mappings":";;;;;AA6DgB,SAAA,uBACd,IACA,gBAC4B;AAC5B,QAAMA,qBAAmB,IAAIC,kCAAsB,IAAI,cAAc;AAE/D,QAAA,CAAC,cAAc,eAAe,IAAIC,QAAA;AAAA,IACtCF,mBAAiB,gBAAgB;AAAA,EACnC;AACM,QAAA,CAAC,gBAAgB,iBAAiB,IAAIE,QAAA;AAAA,IAC1CF,mBAAiB,kBAAkB;AAAA,EACrC;AACM,QAAA,CAAC,YAAY,aAAa,IAAIE,QAAA;AAAA,IAClCF,mBAAiB,cAAc;AAAA,EACjC;AACM,QAAA,CAAC,aAAa,cAAc,IAAIE,QAAA;AAAA,IACpCF,mBAAiB,eAAe;AAAA,EAClC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIE,QAAA;AAAA,IAChDF,mBAAiB,qBAAqB;AAAA,EACxC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIE,QAAA;AAAA,IAChDF,mBAAiB,qBAAqB;AAAA,EACxC;AAEA,WAAS,WAAW,YAAmD;AACrEA,uBAAiB,WAAW;AAAA,MAC1B,GAAG;AAAA,MACH,WAAW,CAAC,gBAAgB;AACV,wBAAA,YAAY,iBAAiB;AAC9B,uBAAA,YAAY,gBAAgB;AAC7B,sBAAA,YAAY,eAAe;AACvB,0BAAA,YAAY,mBAAmB;AAC5B,6BAAA,YAAY,sBAAsB;AAClC,6BAAA,YAAY,sBAAsB;AAEjD,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAY;AAAA,MACd;AAAA,MACA,UAAU,CAAC,gBAAgB;AACP,0BAAA,YAAY,mBAAmB;AAC5B,6BAAA,YAAY,sBAAsB;AAClC,6BAAA,YAAY,sBAAsB;AAEjD,cAAA,WAAW,WAAW,YAAY,eAAe;AACvD,6CAAW;AAAA,MAAW;AAAA,IACxB,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAElB,SAAA;AAAA,IACL,GAAGG,MAAAA,oBAAoBH,kBAAgB;AAAA,IACvC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;"}
1
+ {"version":3,"file":"createAsyncRateLimiter.cjs","sources":["../../../src/async-rate-limiter/createAsyncRateLimiter.ts"],"sourcesContent":["import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type { AsyncRateLimiterOptions } from '@tanstack/pacer/async-rate-limiter'\n\nexport interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction>\n extends Omit<\n AsyncRateLimiter<TFn>,\n | 'getSuccessCount'\n | 'getSettleCount'\n | 'getErrorCount'\n | 'getRejectionCount'\n | 'getRemainingInWindow'\n | 'getMsUntilNextWindow'\n > {\n successCount: Accessor<number>\n settleCount: Accessor<number>\n errorCount: Accessor<number>\n rejectionCount: Accessor<number>\n remainingInWindow: Accessor<number>\n msUntilNextWindow: Accessor<number>\n}\n\n/**\n * A low-level Solid hook that creates an `AsyncRateLimiter` instance to limit how many times an async function can execute within a time window.\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 allows an async function to execute up to a specified limit within a time window,\n * then blocks subsequent calls until the window passes. This is useful for respecting API rate limits,\n * managing resource constraints, or controlling bursts of async operations.\n *\n * Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,\n * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call\n * instead of setting the result on a state variable from within the rate-limited function.\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 * @example\n * ```tsx\n * // Basic API call rate limiting with return value\n * const { maybeExecute } = createAsyncRateLimiter(\n * async (id: string) => {\n * const data = await api.fetchData(id);\n * return data; // Return value is preserved\n * },\n * { limit: 5, window: 1000 } // 5 calls per second\n * );\n *\n * // With state management and return value\n * const [data, setData] = createSignal(null);\n * const { maybeExecute } = createAsyncRateLimiter(\n * async (query) => {\n * const result = await searchAPI(query);\n * setData(result);\n * return result; // Return value can be used by the caller\n * },\n * {\n * limit: 10,\n * window: 60000, // 10 calls per minute\n * onReject: (info) => console.log(`Rate limit exceeded: ${info.nextValidTime - Date.now()}ms until next window`)\n * }\n * );\n * ```\n */\nexport function createAsyncRateLimiter<TFn extends AnyAsyncFunction>(\n fn: TFn,\n initialOptions: AsyncRateLimiterOptions<TFn>,\n): SolidAsyncRateLimiter<TFn> {\n const asyncRateLimiter = new AsyncRateLimiter<TFn>(fn, initialOptions)\n\n const [successCount, setSuccessCount] = createSignal(\n asyncRateLimiter.getSuccessCount(),\n )\n const [rejectionCount, setRejectionCount] = createSignal(\n asyncRateLimiter.getRejectionCount(),\n )\n const [errorCount, setErrorCount] = createSignal(\n asyncRateLimiter.getErrorCount(),\n )\n const [settleCount, setSettleCount] = createSignal(\n asyncRateLimiter.getSettleCount(),\n )\n const [remainingInWindow, setRemainingInWindow] = createSignal(\n asyncRateLimiter.getRemainingInWindow(),\n )\n const [msUntilNextWindow, setMsUntilNextWindow] = createSignal(\n asyncRateLimiter.getMsUntilNextWindow(),\n )\n\n function setOptions(newOptions: Partial<AsyncRateLimiterOptions<TFn>>) {\n asyncRateLimiter.setOptions({\n ...newOptions,\n onSettled: (rateLimiter) => {\n setSuccessCount(rateLimiter.getSuccessCount())\n setSettleCount(rateLimiter.getSettleCount())\n setErrorCount(rateLimiter.getErrorCount())\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onSettled = newOptions.onSettled ?? initialOptions.onSettled\n onSettled?.(rateLimiter)\n },\n onReject: (rateLimiter) => {\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onReject = newOptions.onReject ?? initialOptions.onReject\n onReject?.(rateLimiter)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...bindInstanceMethods(asyncRateLimiter),\n errorCount,\n remainingInWindow,\n msUntilNextWindow,\n rejectionCount,\n setOptions,\n settleCount,\n successCount,\n } as SolidAsyncRateLimiter<TFn>\n}\n"],"names":["asyncRateLimiter","AsyncRateLimiter","createSignal","bindInstanceMethods"],"mappings":";;;;;AAwEgB,SAAA,uBACd,IACA,gBAC4B;AAC5B,QAAMA,qBAAmB,IAAIC,kCAAsB,IAAI,cAAc;AAE/D,QAAA,CAAC,cAAc,eAAe,IAAIC,QAAA;AAAA,IACtCF,mBAAiB,gBAAgB;AAAA,EACnC;AACM,QAAA,CAAC,gBAAgB,iBAAiB,IAAIE,QAAA;AAAA,IAC1CF,mBAAiB,kBAAkB;AAAA,EACrC;AACM,QAAA,CAAC,YAAY,aAAa,IAAIE,QAAA;AAAA,IAClCF,mBAAiB,cAAc;AAAA,EACjC;AACM,QAAA,CAAC,aAAa,cAAc,IAAIE,QAAA;AAAA,IACpCF,mBAAiB,eAAe;AAAA,EAClC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIE,QAAA;AAAA,IAChDF,mBAAiB,qBAAqB;AAAA,EACxC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIE,QAAA;AAAA,IAChDF,mBAAiB,qBAAqB;AAAA,EACxC;AAEA,WAAS,WAAW,YAAmD;AACrEA,uBAAiB,WAAW;AAAA,MAC1B,GAAG;AAAA,MACH,WAAW,CAAC,gBAAgB;AACV,wBAAA,YAAY,iBAAiB;AAC9B,uBAAA,YAAY,gBAAgB;AAC7B,sBAAA,YAAY,eAAe;AACvB,0BAAA,YAAY,mBAAmB;AAC5B,6BAAA,YAAY,sBAAsB;AAClC,6BAAA,YAAY,sBAAsB;AAEjD,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAY;AAAA,MACd;AAAA,MACA,UAAU,CAAC,gBAAgB;AACP,0BAAA,YAAY,mBAAmB;AAC5B,6BAAA,YAAY,sBAAsB;AAClC,6BAAA,YAAY,sBAAsB;AAEjD,cAAA,WAAW,WAAW,YAAY,eAAe;AACvD,6CAAW;AAAA,MAAW;AAAA,IACxB,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAElB,SAAA;AAAA,IACL,GAAGG,MAAAA,oBAAoBH,kBAAgB;AAAA,IACvC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;"}
@@ -19,23 +19,34 @@ export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction> extends Omi
19
19
  * then blocks subsequent calls until the window passes. This is useful for respecting API rate limits,
20
20
  * managing resource constraints, or controlling bursts of async operations.
21
21
  *
22
+ * Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,
23
+ * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call
24
+ * instead of setting the result on a state variable from within the rate-limited function.
25
+ *
26
+ * The rate limiter supports two types of windows:
27
+ * - 'fixed': A strict window that resets after the window period. All executions within the window count
28
+ * towards the limit, and the window resets completely after the period.
29
+ * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more
30
+ * consistent rate of execution over time.
31
+ *
22
32
  * @example
23
33
  * ```tsx
24
- * // Basic API call rate limiting
34
+ * // Basic API call rate limiting with return value
25
35
  * const { maybeExecute } = createAsyncRateLimiter(
26
36
  * async (id: string) => {
27
37
  * const data = await api.fetchData(id);
28
- * return data;
38
+ * return data; // Return value is preserved
29
39
  * },
30
40
  * { limit: 5, window: 1000 } // 5 calls per second
31
41
  * );
32
42
  *
33
- * // With state management
43
+ * // With state management and return value
34
44
  * const [data, setData] = createSignal(null);
35
45
  * const { maybeExecute } = createAsyncRateLimiter(
36
46
  * async (query) => {
37
47
  * const result = await searchAPI(query);
38
48
  * setData(result);
49
+ * return result; // Return value can be used by the caller
39
50
  * },
40
51
  * {
41
52
  * limit: 10,
@@ -1 +1 @@
1
- {"version":3,"file":"createRateLimitedSignal.cjs","sources":["../../../src/rate-limiter/createRateLimitedSignal.ts"],"sourcesContent":["import { createSignal } from 'solid-js'\nimport { createRateLimiter } from './createRateLimiter'\nimport type { SolidRateLimiter } from './createRateLimiter'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\n/**\n * A Solid hook that creates a rate-limited state value that enforces a hard limit on state updates within a time window.\n * This hook combines Solid's createSignal with rate limiting functionality to provide controlled state updates.\n *\n * Rate limiting is a simple \"hard limit\" approach - it allows all updates until the limit is reached, then blocks\n * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out\n * or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.\n *\n * For smoother update patterns, consider:\n * - createThrottledSignal: When you want consistent spacing between updates (e.g. UI changes)\n * - createDebouncedSignal: When you want to collapse rapid updates into a single update (e.g. search input)\n *\n * Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.\n *\n * The hook returns a tuple containing:\n * - The rate-limited state value accessor\n * - A rate-limited setter function that respects the configured limits\n * - The rateLimiter instance for additional control\n *\n * For more direct control over rate limiting without state management,\n * consider using the lower-level createRateLimiter hook instead.\n *\n * @example\n * ```tsx\n * // Basic rate limiting - update state at most 5 times per minute\n * const [value, setValue, rateLimiter] = createRateLimitedSignal(0, {\n * limit: 5,\n * window: 60000\n * });\n *\n * // With rejection callback\n * const [value, setValue] = createRateLimitedSignal(0, {\n * limit: 3,\n * window: 5000,\n * onReject: (rateLimiter) => {\n * alert(`Rate limit reached. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);\n * }\n * });\n *\n * // Access rateLimiter state via signals\n * const handleSubmit = () => {\n * const remaining = rateLimiter.remainingInWindow();\n * if (remaining > 0) {\n * setValue(newValue);\n * } else {\n * showRateLimitWarning();\n * }\n * };\n * ```\n */\nexport function createRateLimitedSignal<TValue>(\n value: TValue,\n initialOptions: RateLimiterOptions<Setter<TValue>>,\n): [Accessor<TValue>, Setter<TValue>, SolidRateLimiter<Setter<TValue>>] {\n const [rateLimitedValue, setRateLimitedValue] = createSignal<TValue>(value)\n\n const rateLimiter = createRateLimiter(setRateLimitedValue, initialOptions)\n\n return [\n rateLimitedValue,\n rateLimiter.maybeExecute.bind(rateLimiter) as Setter<TValue>,\n rateLimiter,\n ]\n}\n"],"names":["createSignal","createRateLimiter"],"mappings":";;;;AAwDgB,SAAA,wBACd,OACA,gBACsE;AACtE,QAAM,CAAC,kBAAkB,mBAAmB,IAAIA,QAAAA,aAAqB,KAAK;AAEpE,QAAA,cAAcC,kBAAAA,kBAAkB,qBAAqB,cAAc;AAElE,SAAA;AAAA,IACL;AAAA,IACA,YAAY,aAAa,KAAK,WAAW;AAAA,IACzC;AAAA,EACF;AACF;;"}
1
+ {"version":3,"file":"createRateLimitedSignal.cjs","sources":["../../../src/rate-limiter/createRateLimitedSignal.ts"],"sourcesContent":["import { createSignal } from 'solid-js'\nimport { createRateLimiter } from './createRateLimiter'\nimport type { SolidRateLimiter } from './createRateLimiter'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\n/**\n * A Solid hook that creates a rate-limited state value that enforces a hard limit on state updates within a time window.\n * This hook combines Solid's createSignal with rate limiting functionality to provide controlled state updates.\n *\n * Rate limiting is a simple \"hard limit\" approach - it allows all updates until the limit is reached, then blocks\n * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out\n * or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All updates within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows updates as old ones expire. This provides a more\n * consistent rate of updates over time.\n *\n * For smoother update patterns, consider:\n * - createThrottledSignal: When you want consistent spacing between updates (e.g. UI changes)\n * - createDebouncedSignal: When you want to collapse rapid updates into a single update (e.g. search input)\n *\n * Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.\n *\n * The hook returns a tuple containing:\n * - The rate-limited state value accessor\n * - A rate-limited setter function that respects the configured limits\n * - The rateLimiter instance for additional control\n *\n * For more direct control over rate limiting without state management,\n * consider using the lower-level createRateLimiter hook instead.\n *\n * @example\n * ```tsx\n * // Basic rate limiting - update state at most 5 times per minute with a sliding window\n * const [value, setValue, rateLimiter] = createRateLimitedSignal(0, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding'\n * });\n *\n * // With rejection callback and fixed window\n * const [value, setValue] = createRateLimitedSignal(0, {\n * limit: 3,\n * window: 5000,\n * windowType: 'fixed',\n * onReject: (rateLimiter) => {\n * alert(`Rate limit reached. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);\n * }\n * });\n *\n * // Access rateLimiter state via signals\n * const handleSubmit = () => {\n * const remaining = rateLimiter.remainingInWindow();\n * if (remaining > 0) {\n * setValue(newValue);\n * } else {\n * showRateLimitWarning();\n * }\n * };\n * ```\n */\nexport function createRateLimitedSignal<TValue>(\n value: TValue,\n initialOptions: RateLimiterOptions<Setter<TValue>>,\n): [Accessor<TValue>, Setter<TValue>, SolidRateLimiter<Setter<TValue>>] {\n const [rateLimitedValue, setRateLimitedValue] = createSignal<TValue>(value)\n\n const rateLimiter = createRateLimiter(setRateLimitedValue, initialOptions)\n\n return [\n rateLimitedValue,\n rateLimiter.maybeExecute.bind(rateLimiter) as Setter<TValue>,\n rateLimiter,\n ]\n}\n"],"names":["createSignal","createRateLimiter"],"mappings":";;;;AAgEgB,SAAA,wBACd,OACA,gBACsE;AACtE,QAAM,CAAC,kBAAkB,mBAAmB,IAAIA,QAAAA,aAAqB,KAAK;AAEpE,QAAA,cAAcC,kBAAAA,kBAAkB,qBAAqB,cAAc;AAElE,SAAA;AAAA,IACL;AAAA,IACA,YAAY,aAAa,KAAK,WAAW;AAAA,IACzC;AAAA,EACF;AACF;;"}
@@ -9,6 +9,12 @@ import { RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
9
9
  * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out
10
10
  * or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.
11
11
  *
12
+ * The rate limiter supports two types of windows:
13
+ * - 'fixed': A strict window that resets after the window period. All updates within the window count
14
+ * towards the limit, and the window resets completely after the period.
15
+ * - 'sliding': A rolling window that allows updates as old ones expire. This provides a more
16
+ * consistent rate of updates over time.
17
+ *
12
18
  * For smoother update patterns, consider:
13
19
  * - createThrottledSignal: When you want consistent spacing between updates (e.g. UI changes)
14
20
  * - createDebouncedSignal: When you want to collapse rapid updates into a single update (e.g. search input)
@@ -25,16 +31,18 @@ import { RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
25
31
  *
26
32
  * @example
27
33
  * ```tsx
28
- * // Basic rate limiting - update state at most 5 times per minute
34
+ * // Basic rate limiting - update state at most 5 times per minute with a sliding window
29
35
  * const [value, setValue, rateLimiter] = createRateLimitedSignal(0, {
30
36
  * limit: 5,
31
- * window: 60000
37
+ * window: 60000,
38
+ * windowType: 'sliding'
32
39
  * });
33
40
  *
34
- * // With rejection callback
41
+ * // With rejection callback and fixed window
35
42
  * const [value, setValue] = createRateLimitedSignal(0, {
36
43
  * limit: 3,
37
44
  * window: 5000,
45
+ * windowType: 'fixed',
38
46
  * onReject: (rateLimiter) => {
39
47
  * alert(`Rate limit reached. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
40
48
  * }
@@ -1 +1 @@
1
- {"version":3,"file":"createRateLimitedValue.cjs","sources":["../../../src/rate-limiter/createRateLimitedValue.ts"],"sourcesContent":["import { createEffect } from 'solid-js'\nimport { createRateLimitedSignal } from './createRateLimitedSignal'\nimport type { SolidRateLimiter } from './createRateLimiter'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\n/**\n * A high-level Solid hook that creates a rate-limited version of a value that updates at most a certain number of times within a time window.\n * This hook uses Solid's createSignal internally to manage the rate-limited state.\n *\n * Rate limiting is a simple \"hard limit\" approach - it allows all updates until the limit is reached, then blocks\n * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out\n * or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.\n *\n * For smoother update patterns, consider:\n * - createThrottledValue: When you want consistent spacing between updates (e.g. UI changes)\n * - createDebouncedValue: When you want to collapse rapid updates into a single update (e.g. search input)\n *\n * Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.\n *\n * The hook returns a tuple containing:\n * - An accessor function that provides the rate-limited value\n * - The rate limiter instance with control methods\n *\n * For more direct control over rate limiting behavior without Solid state management,\n * consider using the lower-level createRateLimiter hook instead.\n *\n * @example\n * ```tsx\n * // Basic rate limiting - update at most 5 times per minute\n * const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {\n * limit: 5,\n * window: 60000\n * });\n *\n * // Use the rate-limited value\n * console.log(rateLimitedValue()); // Access the current rate-limited value\n *\n * // Control the rate limiter\n * rateLimiter.reset(); // Reset the rate limit window\n * ```\n */\nexport function createRateLimitedValue<TValue>(\n value: Accessor<TValue>,\n initialOptions: RateLimiterOptions<Setter<TValue>>,\n): [Accessor<TValue>, SolidRateLimiter<Setter<TValue>>] {\n const [rateLimitedValue, setRateLimitedValue, rateLimiter] =\n createRateLimitedSignal(value(), initialOptions)\n\n createEffect(() => {\n setRateLimitedValue(value() as any)\n })\n\n return [rateLimitedValue, rateLimiter]\n}\n"],"names":["createRateLimitedSignal","createEffect"],"mappings":";;;;AA0CgB,SAAA,uBACd,OACA,gBACsD;AAChD,QAAA,CAAC,kBAAkB,qBAAqB,WAAW,IACvDA,gDAAwB,SAAS,cAAc;AAEjDC,UAAAA,aAAa,MAAM;AACjB,wBAAoB,OAAc;AAAA,EAAA,CACnC;AAEM,SAAA,CAAC,kBAAkB,WAAW;AACvC;;"}
1
+ {"version":3,"file":"createRateLimitedValue.cjs","sources":["../../../src/rate-limiter/createRateLimitedValue.ts"],"sourcesContent":["import { createEffect } from 'solid-js'\nimport { createRateLimitedSignal } from './createRateLimitedSignal'\nimport type { SolidRateLimiter } from './createRateLimiter'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\n/**\n * A high-level Solid hook that creates a rate-limited version of a value that updates at most a certain number of times within a time window.\n * This hook uses Solid's createSignal internally to manage the rate-limited state.\n *\n * Rate limiting is a simple \"hard limit\" approach - it allows all updates until the limit is reached, then blocks\n * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out\n * or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All updates within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows updates as old ones expire. This provides a more\n * consistent rate of updates over time.\n *\n * For smoother update patterns, consider:\n * - createThrottledValue: When you want consistent spacing between updates (e.g. UI changes)\n * - createDebouncedValue: When you want to collapse rapid updates into a single update (e.g. search input)\n *\n * Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.\n *\n * The hook returns a tuple containing:\n * - An accessor function that provides the rate-limited value\n * - The rate limiter instance with control methods\n *\n * For more direct control over rate limiting behavior without Solid state management,\n * consider using the lower-level createRateLimiter hook instead.\n *\n * @example\n * ```tsx\n * // Basic rate limiting - update at most 5 times per minute with a sliding window\n * const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding'\n * });\n *\n * // Use the rate-limited value\n * console.log(rateLimitedValue()); // Access the current rate-limited value\n *\n * // Control the rate limiter\n * rateLimiter.reset(); // Reset the rate limit window\n * ```\n */\nexport function createRateLimitedValue<TValue>(\n value: Accessor<TValue>,\n initialOptions: RateLimiterOptions<Setter<TValue>>,\n): [Accessor<TValue>, SolidRateLimiter<Setter<TValue>>] {\n const [rateLimitedValue, setRateLimitedValue, rateLimiter] =\n createRateLimitedSignal(value(), initialOptions)\n\n createEffect(() => {\n setRateLimitedValue(value() as any)\n })\n\n return [rateLimitedValue, rateLimiter]\n}\n"],"names":["createRateLimitedSignal","createEffect"],"mappings":";;;;AAiDgB,SAAA,uBACd,OACA,gBACsD;AAChD,QAAA,CAAC,kBAAkB,qBAAqB,WAAW,IACvDA,gDAAwB,SAAS,cAAc;AAEjDC,UAAAA,aAAa,MAAM;AACjB,wBAAoB,OAAc;AAAA,EAAA,CACnC;AAEM,SAAA,CAAC,kBAAkB,WAAW;AACvC;;"}
@@ -9,6 +9,12 @@ import { RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
9
9
  * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out
10
10
  * or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.
11
11
  *
12
+ * The rate limiter supports two types of windows:
13
+ * - 'fixed': A strict window that resets after the window period. All updates within the window count
14
+ * towards the limit, and the window resets completely after the period.
15
+ * - 'sliding': A rolling window that allows updates as old ones expire. This provides a more
16
+ * consistent rate of updates over time.
17
+ *
12
18
  * For smoother update patterns, consider:
13
19
  * - createThrottledValue: When you want consistent spacing between updates (e.g. UI changes)
14
20
  * - createDebouncedValue: When you want to collapse rapid updates into a single update (e.g. search input)
@@ -24,10 +30,11 @@ import { RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
24
30
  *
25
31
  * @example
26
32
  * ```tsx
27
- * // Basic rate limiting - update at most 5 times per minute
33
+ * // Basic rate limiting - update at most 5 times per minute with a sliding window
28
34
  * const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {
29
35
  * limit: 5,
30
- * window: 60000
36
+ * window: 60000,
37
+ * windowType: 'sliding'
31
38
  * });
32
39
  *
33
40
  * // Use the rate-limited value
@@ -1 +1 @@
1
- {"version":3,"file":"createRateLimiter.cjs","sources":["../../../src/rate-limiter/createRateLimiter.ts"],"sourcesContent":["import { RateLimiter } from '@tanstack/pacer/rate-limiter'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\nexport interface SolidRateLimiter<TFn extends AnyFunction>\n extends Omit<\n RateLimiter<TFn>,\n | 'getExecutionCount'\n | 'getMsUntilNextWindow'\n | 'getRejectionCount'\n | 'getRemainingInWindow'\n > {\n executionCount: Accessor<number>\n msUntilNextWindow: Accessor<number>\n rejectionCount: Accessor<number>\n remainingInWindow: Accessor<number>\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 * 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 * @example\n * ```tsx\n * // Basic rate limiting - max 5 calls per minute\n * const rateLimiter = createRateLimiter(apiCall, {\n * limit: 5,\n * window: 60000,\n * });\n *\n * // Monitor rate limit status\n * const handleClick = () => {\n * if (rateLimiter.remainingInWindow() > 0) {\n * rateLimiter.maybeExecute(data);\n * } else {\n * showRateLimitWarning();\n * }\n * };\n *\n * // Access rate limiter state via signals\n * console.log('Executions:', rateLimiter.executionCount());\n * console.log('Rejections:', rateLimiter.rejectionCount());\n * console.log('Remaining:', rateLimiter.remainingInWindow());\n * console.log('Next window in:', rateLimiter.msUntilNextWindow());\n * ```\n */\nexport function createRateLimiter<TFn extends AnyFunction>(\n fn: TFn,\n initialOptions: RateLimiterOptions<TFn>,\n): SolidRateLimiter<TFn> {\n const rateLimiter = bindInstanceMethods(\n new RateLimiter<TFn>(fn, initialOptions),\n )\n\n const [executionCount, setExecutionCount] = createSignal(\n rateLimiter.getExecutionCount(),\n )\n const [rejectionCount, setRejectionCount] = createSignal(\n rateLimiter.getRejectionCount(),\n )\n const [remainingInWindow, setRemainingInWindow] = createSignal(\n rateLimiter.getRemainingInWindow(),\n )\n const [msUntilNextWindow, setMsUntilNextWindow] = createSignal(\n rateLimiter.getMsUntilNextWindow(),\n )\n\n function setOptions(newOptions: Partial<RateLimiterOptions<TFn>>) {\n rateLimiter.setOptions({\n ...newOptions,\n onExecute: (rateLimiter) => {\n setExecutionCount(rateLimiter.getExecutionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onExecute = newOptions.onExecute ?? initialOptions.onExecute\n onExecute?.(rateLimiter)\n },\n onReject: (rateLimiter) => {\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onReject = newOptions.onReject ?? initialOptions.onReject\n onReject?.(rateLimiter)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...rateLimiter,\n executionCount,\n rejectionCount,\n remainingInWindow,\n msUntilNextWindow,\n setOptions,\n } as SolidRateLimiter<TFn>\n}\n"],"names":["rateLimiter","bindInstanceMethods","RateLimiter","createSignal"],"mappings":";;;;;AA4DgB,SAAA,kBACd,IACA,gBACuB;AACvB,QAAMA,gBAAcC,MAAA;AAAA,IAClB,IAAIC,YAAAA,YAAiB,IAAI,cAAc;AAAA,EACzC;AAEM,QAAA,CAAC,gBAAgB,iBAAiB,IAAIC,QAAA;AAAA,IAC1CH,cAAY,kBAAkB;AAAA,EAChC;AACM,QAAA,CAAC,gBAAgB,iBAAiB,IAAIG,QAAA;AAAA,IAC1CH,cAAY,kBAAkB;AAAA,EAChC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIG,QAAA;AAAA,IAChDH,cAAY,qBAAqB;AAAA,EACnC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIG,QAAA;AAAA,IAChDH,cAAY,qBAAqB;AAAA,EACnC;AAEA,WAAS,WAAW,YAA8C;AAChEA,kBAAY,WAAW;AAAA,MACrB,GAAG;AAAA,MACH,WAAW,CAACA,iBAAgB;AACRA,0BAAAA,aAAY,mBAAmB;AAC5BA,6BAAAA,aAAY,sBAAsB;AAClCA,6BAAAA,aAAY,sBAAsB;AAEjD,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAYA;AAAAA,MACd;AAAA,MACA,UAAU,CAACA,iBAAgB;AACPA,0BAAAA,aAAY,mBAAmB;AAC5BA,6BAAAA,aAAY,sBAAsB;AAClCA,6BAAAA,aAAY,sBAAsB;AAEjD,cAAA,WAAW,WAAW,YAAY,eAAe;AACvD,6CAAWA;AAAAA,MAAW;AAAA,IACxB,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAElB,SAAA;AAAA,IACL,GAAGA;AAAAA,IACH;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;"}
1
+ {"version":3,"file":"createRateLimiter.cjs","sources":["../../../src/rate-limiter/createRateLimiter.ts"],"sourcesContent":["import { RateLimiter } from '@tanstack/pacer/rate-limiter'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\nexport interface SolidRateLimiter<TFn extends AnyFunction>\n extends Omit<\n RateLimiter<TFn>,\n | 'getExecutionCount'\n | 'getMsUntilNextWindow'\n | 'getRejectionCount'\n | 'getRemainingInWindow'\n > {\n executionCount: Accessor<number>\n msUntilNextWindow: Accessor<number>\n rejectionCount: Accessor<number>\n remainingInWindow: Accessor<number>\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 * @example\n * ```tsx\n * // Basic rate limiting - max 5 calls per minute with a sliding window\n * const rateLimiter = createRateLimiter(apiCall, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding'\n * });\n *\n * // Monitor rate limit status\n * const handleClick = () => {\n * if (rateLimiter.remainingInWindow() > 0) {\n * rateLimiter.maybeExecute(data);\n * } else {\n * showRateLimitWarning();\n * }\n * };\n *\n * // Access rate limiter state via signals\n * console.log('Executions:', rateLimiter.executionCount());\n * console.log('Rejections:', rateLimiter.rejectionCount());\n * console.log('Remaining:', rateLimiter.remainingInWindow());\n * console.log('Next window in:', rateLimiter.msUntilNextWindow());\n * ```\n */\nexport function createRateLimiter<TFn extends AnyFunction>(\n fn: TFn,\n initialOptions: RateLimiterOptions<TFn>,\n): SolidRateLimiter<TFn> {\n const rateLimiter = bindInstanceMethods(\n new RateLimiter<TFn>(fn, initialOptions),\n )\n\n const [executionCount, setExecutionCount] = createSignal(\n rateLimiter.getExecutionCount(),\n )\n const [rejectionCount, setRejectionCount] = createSignal(\n rateLimiter.getRejectionCount(),\n )\n const [remainingInWindow, setRemainingInWindow] = createSignal(\n rateLimiter.getRemainingInWindow(),\n )\n const [msUntilNextWindow, setMsUntilNextWindow] = createSignal(\n rateLimiter.getMsUntilNextWindow(),\n )\n\n function setOptions(newOptions: Partial<RateLimiterOptions<TFn>>) {\n rateLimiter.setOptions({\n ...newOptions,\n onExecute: (rateLimiter) => {\n setExecutionCount(rateLimiter.getExecutionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onExecute = newOptions.onExecute ?? initialOptions.onExecute\n onExecute?.(rateLimiter)\n },\n onReject: (rateLimiter) => {\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onReject = newOptions.onReject ?? initialOptions.onReject\n onReject?.(rateLimiter)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...rateLimiter,\n executionCount,\n rejectionCount,\n remainingInWindow,\n msUntilNextWindow,\n setOptions,\n } as SolidRateLimiter<TFn>\n}\n"],"names":["rateLimiter","bindInstanceMethods","RateLimiter","createSignal"],"mappings":";;;;;AAmEgB,SAAA,kBACd,IACA,gBACuB;AACvB,QAAMA,gBAAcC,MAAA;AAAA,IAClB,IAAIC,YAAAA,YAAiB,IAAI,cAAc;AAAA,EACzC;AAEM,QAAA,CAAC,gBAAgB,iBAAiB,IAAIC,QAAA;AAAA,IAC1CH,cAAY,kBAAkB;AAAA,EAChC;AACM,QAAA,CAAC,gBAAgB,iBAAiB,IAAIG,QAAA;AAAA,IAC1CH,cAAY,kBAAkB;AAAA,EAChC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIG,QAAA;AAAA,IAChDH,cAAY,qBAAqB;AAAA,EACnC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIG,QAAA;AAAA,IAChDH,cAAY,qBAAqB;AAAA,EACnC;AAEA,WAAS,WAAW,YAA8C;AAChEA,kBAAY,WAAW;AAAA,MACrB,GAAG;AAAA,MACH,WAAW,CAACA,iBAAgB;AACRA,0BAAAA,aAAY,mBAAmB;AAC5BA,6BAAAA,aAAY,sBAAsB;AAClCA,6BAAAA,aAAY,sBAAsB;AAEjD,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAYA;AAAAA,MACd;AAAA,MACA,UAAU,CAACA,iBAAgB;AACPA,0BAAAA,aAAY,mBAAmB;AAC5BA,6BAAAA,aAAY,sBAAsB;AAClCA,6BAAAA,aAAY,sBAAsB;AAEjD,cAAA,WAAW,WAAW,YAAY,eAAe;AACvD,6CAAWA;AAAAA,MAAW;AAAA,IACxB,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAElB,SAAA;AAAA,IACL,GAAGA;AAAAA,IACH;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;"}
@@ -17,6 +17,12 @@ export interface SolidRateLimiter<TFn extends AnyFunction> extends Omit<RateLimi
17
17
  * a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,
18
18
  * it does not attempt to space out or collapse executions intelligently.
19
19
  *
20
+ * The rate limiter supports two types of windows:
21
+ * - 'fixed': A strict window that resets after the window period. All executions within the window count
22
+ * towards the limit, and the window resets completely after the period.
23
+ * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more
24
+ * consistent rate of execution over time.
25
+ *
20
26
  * For smoother execution patterns:
21
27
  * - Use throttling when you want consistent spacing between executions (e.g. UI updates)
22
28
  * - Use debouncing when you want to collapse rapid-fire events (e.g. search input)
@@ -24,10 +30,11 @@ export interface SolidRateLimiter<TFn extends AnyFunction> extends Omit<RateLimi
24
30
  *
25
31
  * @example
26
32
  * ```tsx
27
- * // Basic rate limiting - max 5 calls per minute
33
+ * // Basic rate limiting - max 5 calls per minute with a sliding window
28
34
  * const rateLimiter = createRateLimiter(apiCall, {
29
35
  * limit: 5,
30
36
  * window: 60000,
37
+ * windowType: 'sliding'
31
38
  * });
32
39
  *
33
40
  * // Monitor rate limit status
@@ -19,23 +19,34 @@ export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction> extends Omi
19
19
  * then blocks subsequent calls until the window passes. This is useful for respecting API rate limits,
20
20
  * managing resource constraints, or controlling bursts of async operations.
21
21
  *
22
+ * Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,
23
+ * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call
24
+ * instead of setting the result on a state variable from within the rate-limited function.
25
+ *
26
+ * The rate limiter supports two types of windows:
27
+ * - 'fixed': A strict window that resets after the window period. All executions within the window count
28
+ * towards the limit, and the window resets completely after the period.
29
+ * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more
30
+ * consistent rate of execution over time.
31
+ *
22
32
  * @example
23
33
  * ```tsx
24
- * // Basic API call rate limiting
34
+ * // Basic API call rate limiting with return value
25
35
  * const { maybeExecute } = createAsyncRateLimiter(
26
36
  * async (id: string) => {
27
37
  * const data = await api.fetchData(id);
28
- * return data;
38
+ * return data; // Return value is preserved
29
39
  * },
30
40
  * { limit: 5, window: 1000 } // 5 calls per second
31
41
  * );
32
42
  *
33
- * // With state management
43
+ * // With state management and return value
34
44
  * const [data, setData] = createSignal(null);
35
45
  * const { maybeExecute } = createAsyncRateLimiter(
36
46
  * async (query) => {
37
47
  * const result = await searchAPI(query);
38
48
  * setData(result);
49
+ * return result; // Return value can be used by the caller
39
50
  * },
40
51
  * {
41
52
  * limit: 10,
@@ -1 +1 @@
1
- {"version":3,"file":"createAsyncRateLimiter.js","sources":["../../../src/async-rate-limiter/createAsyncRateLimiter.ts"],"sourcesContent":["import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type { AsyncRateLimiterOptions } from '@tanstack/pacer/async-rate-limiter'\n\nexport interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction>\n extends Omit<\n AsyncRateLimiter<TFn>,\n | 'getSuccessCount'\n | 'getSettleCount'\n | 'getErrorCount'\n | 'getRejectionCount'\n | 'getRemainingInWindow'\n | 'getMsUntilNextWindow'\n > {\n successCount: Accessor<number>\n settleCount: Accessor<number>\n errorCount: Accessor<number>\n rejectionCount: Accessor<number>\n remainingInWindow: Accessor<number>\n msUntilNextWindow: Accessor<number>\n}\n\n/**\n * A low-level Solid hook that creates an `AsyncRateLimiter` instance to limit how many times an async function can execute within a time window.\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 allows an async function to execute up to a specified limit within a time window,\n * then blocks subsequent calls until the window passes. This is useful for respecting API rate limits,\n * managing resource constraints, or controlling bursts of async operations.\n *\n * @example\n * ```tsx\n * // Basic API call rate limiting\n * const { maybeExecute } = createAsyncRateLimiter(\n * async (id: string) => {\n * const data = await api.fetchData(id);\n * return data;\n * },\n * { limit: 5, window: 1000 } // 5 calls per second\n * );\n *\n * // With state management\n * const [data, setData] = createSignal(null);\n * const { maybeExecute } = createAsyncRateLimiter(\n * async (query) => {\n * const result = await searchAPI(query);\n * setData(result);\n * },\n * {\n * limit: 10,\n * window: 60000, // 10 calls per minute\n * onReject: (info) => console.log(`Rate limit exceeded: ${info.nextValidTime - Date.now()}ms until next window`)\n * }\n * );\n * ```\n */\nexport function createAsyncRateLimiter<TFn extends AnyAsyncFunction>(\n fn: TFn,\n initialOptions: AsyncRateLimiterOptions<TFn>,\n): SolidAsyncRateLimiter<TFn> {\n const asyncRateLimiter = new AsyncRateLimiter<TFn>(fn, initialOptions)\n\n const [successCount, setSuccessCount] = createSignal(\n asyncRateLimiter.getSuccessCount(),\n )\n const [rejectionCount, setRejectionCount] = createSignal(\n asyncRateLimiter.getRejectionCount(),\n )\n const [errorCount, setErrorCount] = createSignal(\n asyncRateLimiter.getErrorCount(),\n )\n const [settleCount, setSettleCount] = createSignal(\n asyncRateLimiter.getSettleCount(),\n )\n const [remainingInWindow, setRemainingInWindow] = createSignal(\n asyncRateLimiter.getRemainingInWindow(),\n )\n const [msUntilNextWindow, setMsUntilNextWindow] = createSignal(\n asyncRateLimiter.getMsUntilNextWindow(),\n )\n\n function setOptions(newOptions: Partial<AsyncRateLimiterOptions<TFn>>) {\n asyncRateLimiter.setOptions({\n ...newOptions,\n onSettled: (rateLimiter) => {\n setSuccessCount(rateLimiter.getSuccessCount())\n setSettleCount(rateLimiter.getSettleCount())\n setErrorCount(rateLimiter.getErrorCount())\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onSettled = newOptions.onSettled ?? initialOptions.onSettled\n onSettled?.(rateLimiter)\n },\n onReject: (rateLimiter) => {\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onReject = newOptions.onReject ?? initialOptions.onReject\n onReject?.(rateLimiter)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...bindInstanceMethods(asyncRateLimiter),\n errorCount,\n remainingInWindow,\n msUntilNextWindow,\n rejectionCount,\n setOptions,\n settleCount,\n successCount,\n } as SolidAsyncRateLimiter<TFn>\n}\n"],"names":[],"mappings":";;;AA6DgB,SAAA,uBACd,IACA,gBAC4B;AAC5B,QAAM,mBAAmB,IAAI,iBAAsB,IAAI,cAAc;AAE/D,QAAA,CAAC,cAAc,eAAe,IAAI;AAAA,IACtC,iBAAiB,gBAAgB;AAAA,EACnC;AACM,QAAA,CAAC,gBAAgB,iBAAiB,IAAI;AAAA,IAC1C,iBAAiB,kBAAkB;AAAA,EACrC;AACM,QAAA,CAAC,YAAY,aAAa,IAAI;AAAA,IAClC,iBAAiB,cAAc;AAAA,EACjC;AACM,QAAA,CAAC,aAAa,cAAc,IAAI;AAAA,IACpC,iBAAiB,eAAe;AAAA,EAClC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAI;AAAA,IAChD,iBAAiB,qBAAqB;AAAA,EACxC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAI;AAAA,IAChD,iBAAiB,qBAAqB;AAAA,EACxC;AAEA,WAAS,WAAW,YAAmD;AACrE,qBAAiB,WAAW;AAAA,MAC1B,GAAG;AAAA,MACH,WAAW,CAAC,gBAAgB;AACV,wBAAA,YAAY,iBAAiB;AAC9B,uBAAA,YAAY,gBAAgB;AAC7B,sBAAA,YAAY,eAAe;AACvB,0BAAA,YAAY,mBAAmB;AAC5B,6BAAA,YAAY,sBAAsB;AAClC,6BAAA,YAAY,sBAAsB;AAEjD,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAY;AAAA,MACd;AAAA,MACA,UAAU,CAAC,gBAAgB;AACP,0BAAA,YAAY,mBAAmB;AAC5B,6BAAA,YAAY,sBAAsB;AAClC,6BAAA,YAAY,sBAAsB;AAEjD,cAAA,WAAW,WAAW,YAAY,eAAe;AACvD,6CAAW;AAAA,MAAW;AAAA,IACxB,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAElB,SAAA;AAAA,IACL,GAAG,oBAAoB,gBAAgB;AAAA,IACvC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;"}
1
+ {"version":3,"file":"createAsyncRateLimiter.js","sources":["../../../src/async-rate-limiter/createAsyncRateLimiter.ts"],"sourcesContent":["import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type { AsyncRateLimiterOptions } from '@tanstack/pacer/async-rate-limiter'\n\nexport interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction>\n extends Omit<\n AsyncRateLimiter<TFn>,\n | 'getSuccessCount'\n | 'getSettleCount'\n | 'getErrorCount'\n | 'getRejectionCount'\n | 'getRemainingInWindow'\n | 'getMsUntilNextWindow'\n > {\n successCount: Accessor<number>\n settleCount: Accessor<number>\n errorCount: Accessor<number>\n rejectionCount: Accessor<number>\n remainingInWindow: Accessor<number>\n msUntilNextWindow: Accessor<number>\n}\n\n/**\n * A low-level Solid hook that creates an `AsyncRateLimiter` instance to limit how many times an async function can execute within a time window.\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 allows an async function to execute up to a specified limit within a time window,\n * then blocks subsequent calls until the window passes. This is useful for respecting API rate limits,\n * managing resource constraints, or controlling bursts of async operations.\n *\n * Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,\n * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call\n * instead of setting the result on a state variable from within the rate-limited function.\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 * @example\n * ```tsx\n * // Basic API call rate limiting with return value\n * const { maybeExecute } = createAsyncRateLimiter(\n * async (id: string) => {\n * const data = await api.fetchData(id);\n * return data; // Return value is preserved\n * },\n * { limit: 5, window: 1000 } // 5 calls per second\n * );\n *\n * // With state management and return value\n * const [data, setData] = createSignal(null);\n * const { maybeExecute } = createAsyncRateLimiter(\n * async (query) => {\n * const result = await searchAPI(query);\n * setData(result);\n * return result; // Return value can be used by the caller\n * },\n * {\n * limit: 10,\n * window: 60000, // 10 calls per minute\n * onReject: (info) => console.log(`Rate limit exceeded: ${info.nextValidTime - Date.now()}ms until next window`)\n * }\n * );\n * ```\n */\nexport function createAsyncRateLimiter<TFn extends AnyAsyncFunction>(\n fn: TFn,\n initialOptions: AsyncRateLimiterOptions<TFn>,\n): SolidAsyncRateLimiter<TFn> {\n const asyncRateLimiter = new AsyncRateLimiter<TFn>(fn, initialOptions)\n\n const [successCount, setSuccessCount] = createSignal(\n asyncRateLimiter.getSuccessCount(),\n )\n const [rejectionCount, setRejectionCount] = createSignal(\n asyncRateLimiter.getRejectionCount(),\n )\n const [errorCount, setErrorCount] = createSignal(\n asyncRateLimiter.getErrorCount(),\n )\n const [settleCount, setSettleCount] = createSignal(\n asyncRateLimiter.getSettleCount(),\n )\n const [remainingInWindow, setRemainingInWindow] = createSignal(\n asyncRateLimiter.getRemainingInWindow(),\n )\n const [msUntilNextWindow, setMsUntilNextWindow] = createSignal(\n asyncRateLimiter.getMsUntilNextWindow(),\n )\n\n function setOptions(newOptions: Partial<AsyncRateLimiterOptions<TFn>>) {\n asyncRateLimiter.setOptions({\n ...newOptions,\n onSettled: (rateLimiter) => {\n setSuccessCount(rateLimiter.getSuccessCount())\n setSettleCount(rateLimiter.getSettleCount())\n setErrorCount(rateLimiter.getErrorCount())\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onSettled = newOptions.onSettled ?? initialOptions.onSettled\n onSettled?.(rateLimiter)\n },\n onReject: (rateLimiter) => {\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onReject = newOptions.onReject ?? initialOptions.onReject\n onReject?.(rateLimiter)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...bindInstanceMethods(asyncRateLimiter),\n errorCount,\n remainingInWindow,\n msUntilNextWindow,\n rejectionCount,\n setOptions,\n settleCount,\n successCount,\n } as SolidAsyncRateLimiter<TFn>\n}\n"],"names":[],"mappings":";;;AAwEgB,SAAA,uBACd,IACA,gBAC4B;AAC5B,QAAM,mBAAmB,IAAI,iBAAsB,IAAI,cAAc;AAE/D,QAAA,CAAC,cAAc,eAAe,IAAI;AAAA,IACtC,iBAAiB,gBAAgB;AAAA,EACnC;AACM,QAAA,CAAC,gBAAgB,iBAAiB,IAAI;AAAA,IAC1C,iBAAiB,kBAAkB;AAAA,EACrC;AACM,QAAA,CAAC,YAAY,aAAa,IAAI;AAAA,IAClC,iBAAiB,cAAc;AAAA,EACjC;AACM,QAAA,CAAC,aAAa,cAAc,IAAI;AAAA,IACpC,iBAAiB,eAAe;AAAA,EAClC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAI;AAAA,IAChD,iBAAiB,qBAAqB;AAAA,EACxC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAI;AAAA,IAChD,iBAAiB,qBAAqB;AAAA,EACxC;AAEA,WAAS,WAAW,YAAmD;AACrE,qBAAiB,WAAW;AAAA,MAC1B,GAAG;AAAA,MACH,WAAW,CAAC,gBAAgB;AACV,wBAAA,YAAY,iBAAiB;AAC9B,uBAAA,YAAY,gBAAgB;AAC7B,sBAAA,YAAY,eAAe;AACvB,0BAAA,YAAY,mBAAmB;AAC5B,6BAAA,YAAY,sBAAsB;AAClC,6BAAA,YAAY,sBAAsB;AAEjD,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAY;AAAA,MACd;AAAA,MACA,UAAU,CAAC,gBAAgB;AACP,0BAAA,YAAY,mBAAmB;AAC5B,6BAAA,YAAY,sBAAsB;AAClC,6BAAA,YAAY,sBAAsB;AAEjD,cAAA,WAAW,WAAW,YAAY,eAAe;AACvD,6CAAW;AAAA,MAAW;AAAA,IACxB,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAElB,SAAA;AAAA,IACL,GAAG,oBAAoB,gBAAgB;AAAA,IACvC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;"}
@@ -9,6 +9,12 @@ import { RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
9
9
  * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out
10
10
  * or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.
11
11
  *
12
+ * The rate limiter supports two types of windows:
13
+ * - 'fixed': A strict window that resets after the window period. All updates within the window count
14
+ * towards the limit, and the window resets completely after the period.
15
+ * - 'sliding': A rolling window that allows updates as old ones expire. This provides a more
16
+ * consistent rate of updates over time.
17
+ *
12
18
  * For smoother update patterns, consider:
13
19
  * - createThrottledSignal: When you want consistent spacing between updates (e.g. UI changes)
14
20
  * - createDebouncedSignal: When you want to collapse rapid updates into a single update (e.g. search input)
@@ -25,16 +31,18 @@ import { RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
25
31
  *
26
32
  * @example
27
33
  * ```tsx
28
- * // Basic rate limiting - update state at most 5 times per minute
34
+ * // Basic rate limiting - update state at most 5 times per minute with a sliding window
29
35
  * const [value, setValue, rateLimiter] = createRateLimitedSignal(0, {
30
36
  * limit: 5,
31
- * window: 60000
37
+ * window: 60000,
38
+ * windowType: 'sliding'
32
39
  * });
33
40
  *
34
- * // With rejection callback
41
+ * // With rejection callback and fixed window
35
42
  * const [value, setValue] = createRateLimitedSignal(0, {
36
43
  * limit: 3,
37
44
  * window: 5000,
45
+ * windowType: 'fixed',
38
46
  * onReject: (rateLimiter) => {
39
47
  * alert(`Rate limit reached. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
40
48
  * }
@@ -1 +1 @@
1
- {"version":3,"file":"createRateLimitedSignal.js","sources":["../../../src/rate-limiter/createRateLimitedSignal.ts"],"sourcesContent":["import { createSignal } from 'solid-js'\nimport { createRateLimiter } from './createRateLimiter'\nimport type { SolidRateLimiter } from './createRateLimiter'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\n/**\n * A Solid hook that creates a rate-limited state value that enforces a hard limit on state updates within a time window.\n * This hook combines Solid's createSignal with rate limiting functionality to provide controlled state updates.\n *\n * Rate limiting is a simple \"hard limit\" approach - it allows all updates until the limit is reached, then blocks\n * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out\n * or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.\n *\n * For smoother update patterns, consider:\n * - createThrottledSignal: When you want consistent spacing between updates (e.g. UI changes)\n * - createDebouncedSignal: When you want to collapse rapid updates into a single update (e.g. search input)\n *\n * Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.\n *\n * The hook returns a tuple containing:\n * - The rate-limited state value accessor\n * - A rate-limited setter function that respects the configured limits\n * - The rateLimiter instance for additional control\n *\n * For more direct control over rate limiting without state management,\n * consider using the lower-level createRateLimiter hook instead.\n *\n * @example\n * ```tsx\n * // Basic rate limiting - update state at most 5 times per minute\n * const [value, setValue, rateLimiter] = createRateLimitedSignal(0, {\n * limit: 5,\n * window: 60000\n * });\n *\n * // With rejection callback\n * const [value, setValue] = createRateLimitedSignal(0, {\n * limit: 3,\n * window: 5000,\n * onReject: (rateLimiter) => {\n * alert(`Rate limit reached. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);\n * }\n * });\n *\n * // Access rateLimiter state via signals\n * const handleSubmit = () => {\n * const remaining = rateLimiter.remainingInWindow();\n * if (remaining > 0) {\n * setValue(newValue);\n * } else {\n * showRateLimitWarning();\n * }\n * };\n * ```\n */\nexport function createRateLimitedSignal<TValue>(\n value: TValue,\n initialOptions: RateLimiterOptions<Setter<TValue>>,\n): [Accessor<TValue>, Setter<TValue>, SolidRateLimiter<Setter<TValue>>] {\n const [rateLimitedValue, setRateLimitedValue] = createSignal<TValue>(value)\n\n const rateLimiter = createRateLimiter(setRateLimitedValue, initialOptions)\n\n return [\n rateLimitedValue,\n rateLimiter.maybeExecute.bind(rateLimiter) as Setter<TValue>,\n rateLimiter,\n ]\n}\n"],"names":[],"mappings":";;AAwDgB,SAAA,wBACd,OACA,gBACsE;AACtE,QAAM,CAAC,kBAAkB,mBAAmB,IAAI,aAAqB,KAAK;AAEpE,QAAA,cAAc,kBAAkB,qBAAqB,cAAc;AAElE,SAAA;AAAA,IACL;AAAA,IACA,YAAY,aAAa,KAAK,WAAW;AAAA,IACzC;AAAA,EACF;AACF;"}
1
+ {"version":3,"file":"createRateLimitedSignal.js","sources":["../../../src/rate-limiter/createRateLimitedSignal.ts"],"sourcesContent":["import { createSignal } from 'solid-js'\nimport { createRateLimiter } from './createRateLimiter'\nimport type { SolidRateLimiter } from './createRateLimiter'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\n/**\n * A Solid hook that creates a rate-limited state value that enforces a hard limit on state updates within a time window.\n * This hook combines Solid's createSignal with rate limiting functionality to provide controlled state updates.\n *\n * Rate limiting is a simple \"hard limit\" approach - it allows all updates until the limit is reached, then blocks\n * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out\n * or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All updates within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows updates as old ones expire. This provides a more\n * consistent rate of updates over time.\n *\n * For smoother update patterns, consider:\n * - createThrottledSignal: When you want consistent spacing between updates (e.g. UI changes)\n * - createDebouncedSignal: When you want to collapse rapid updates into a single update (e.g. search input)\n *\n * Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.\n *\n * The hook returns a tuple containing:\n * - The rate-limited state value accessor\n * - A rate-limited setter function that respects the configured limits\n * - The rateLimiter instance for additional control\n *\n * For more direct control over rate limiting without state management,\n * consider using the lower-level createRateLimiter hook instead.\n *\n * @example\n * ```tsx\n * // Basic rate limiting - update state at most 5 times per minute with a sliding window\n * const [value, setValue, rateLimiter] = createRateLimitedSignal(0, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding'\n * });\n *\n * // With rejection callback and fixed window\n * const [value, setValue] = createRateLimitedSignal(0, {\n * limit: 3,\n * window: 5000,\n * windowType: 'fixed',\n * onReject: (rateLimiter) => {\n * alert(`Rate limit reached. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);\n * }\n * });\n *\n * // Access rateLimiter state via signals\n * const handleSubmit = () => {\n * const remaining = rateLimiter.remainingInWindow();\n * if (remaining > 0) {\n * setValue(newValue);\n * } else {\n * showRateLimitWarning();\n * }\n * };\n * ```\n */\nexport function createRateLimitedSignal<TValue>(\n value: TValue,\n initialOptions: RateLimiterOptions<Setter<TValue>>,\n): [Accessor<TValue>, Setter<TValue>, SolidRateLimiter<Setter<TValue>>] {\n const [rateLimitedValue, setRateLimitedValue] = createSignal<TValue>(value)\n\n const rateLimiter = createRateLimiter(setRateLimitedValue, initialOptions)\n\n return [\n rateLimitedValue,\n rateLimiter.maybeExecute.bind(rateLimiter) as Setter<TValue>,\n rateLimiter,\n ]\n}\n"],"names":[],"mappings":";;AAgEgB,SAAA,wBACd,OACA,gBACsE;AACtE,QAAM,CAAC,kBAAkB,mBAAmB,IAAI,aAAqB,KAAK;AAEpE,QAAA,cAAc,kBAAkB,qBAAqB,cAAc;AAElE,SAAA;AAAA,IACL;AAAA,IACA,YAAY,aAAa,KAAK,WAAW;AAAA,IACzC;AAAA,EACF;AACF;"}
@@ -9,6 +9,12 @@ import { RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
9
9
  * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out
10
10
  * or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.
11
11
  *
12
+ * The rate limiter supports two types of windows:
13
+ * - 'fixed': A strict window that resets after the window period. All updates within the window count
14
+ * towards the limit, and the window resets completely after the period.
15
+ * - 'sliding': A rolling window that allows updates as old ones expire. This provides a more
16
+ * consistent rate of updates over time.
17
+ *
12
18
  * For smoother update patterns, consider:
13
19
  * - createThrottledValue: When you want consistent spacing between updates (e.g. UI changes)
14
20
  * - createDebouncedValue: When you want to collapse rapid updates into a single update (e.g. search input)
@@ -24,10 +30,11 @@ import { RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
24
30
  *
25
31
  * @example
26
32
  * ```tsx
27
- * // Basic rate limiting - update at most 5 times per minute
33
+ * // Basic rate limiting - update at most 5 times per minute with a sliding window
28
34
  * const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {
29
35
  * limit: 5,
30
- * window: 60000
36
+ * window: 60000,
37
+ * windowType: 'sliding'
31
38
  * });
32
39
  *
33
40
  * // Use the rate-limited value
@@ -1 +1 @@
1
- {"version":3,"file":"createRateLimitedValue.js","sources":["../../../src/rate-limiter/createRateLimitedValue.ts"],"sourcesContent":["import { createEffect } from 'solid-js'\nimport { createRateLimitedSignal } from './createRateLimitedSignal'\nimport type { SolidRateLimiter } from './createRateLimiter'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\n/**\n * A high-level Solid hook that creates a rate-limited version of a value that updates at most a certain number of times within a time window.\n * This hook uses Solid's createSignal internally to manage the rate-limited state.\n *\n * Rate limiting is a simple \"hard limit\" approach - it allows all updates until the limit is reached, then blocks\n * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out\n * or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.\n *\n * For smoother update patterns, consider:\n * - createThrottledValue: When you want consistent spacing between updates (e.g. UI changes)\n * - createDebouncedValue: When you want to collapse rapid updates into a single update (e.g. search input)\n *\n * Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.\n *\n * The hook returns a tuple containing:\n * - An accessor function that provides the rate-limited value\n * - The rate limiter instance with control methods\n *\n * For more direct control over rate limiting behavior without Solid state management,\n * consider using the lower-level createRateLimiter hook instead.\n *\n * @example\n * ```tsx\n * // Basic rate limiting - update at most 5 times per minute\n * const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {\n * limit: 5,\n * window: 60000\n * });\n *\n * // Use the rate-limited value\n * console.log(rateLimitedValue()); // Access the current rate-limited value\n *\n * // Control the rate limiter\n * rateLimiter.reset(); // Reset the rate limit window\n * ```\n */\nexport function createRateLimitedValue<TValue>(\n value: Accessor<TValue>,\n initialOptions: RateLimiterOptions<Setter<TValue>>,\n): [Accessor<TValue>, SolidRateLimiter<Setter<TValue>>] {\n const [rateLimitedValue, setRateLimitedValue, rateLimiter] =\n createRateLimitedSignal(value(), initialOptions)\n\n createEffect(() => {\n setRateLimitedValue(value() as any)\n })\n\n return [rateLimitedValue, rateLimiter]\n}\n"],"names":[],"mappings":";;AA0CgB,SAAA,uBACd,OACA,gBACsD;AAChD,QAAA,CAAC,kBAAkB,qBAAqB,WAAW,IACvD,wBAAwB,SAAS,cAAc;AAEjD,eAAa,MAAM;AACjB,wBAAoB,OAAc;AAAA,EAAA,CACnC;AAEM,SAAA,CAAC,kBAAkB,WAAW;AACvC;"}
1
+ {"version":3,"file":"createRateLimitedValue.js","sources":["../../../src/rate-limiter/createRateLimitedValue.ts"],"sourcesContent":["import { createEffect } from 'solid-js'\nimport { createRateLimitedSignal } from './createRateLimitedSignal'\nimport type { SolidRateLimiter } from './createRateLimiter'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\n/**\n * A high-level Solid hook that creates a rate-limited version of a value that updates at most a certain number of times within a time window.\n * This hook uses Solid's createSignal internally to manage the rate-limited state.\n *\n * Rate limiting is a simple \"hard limit\" approach - it allows all updates until the limit is reached, then blocks\n * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out\n * or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All updates within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows updates as old ones expire. This provides a more\n * consistent rate of updates over time.\n *\n * For smoother update patterns, consider:\n * - createThrottledValue: When you want consistent spacing between updates (e.g. UI changes)\n * - createDebouncedValue: When you want to collapse rapid updates into a single update (e.g. search input)\n *\n * Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.\n *\n * The hook returns a tuple containing:\n * - An accessor function that provides the rate-limited value\n * - The rate limiter instance with control methods\n *\n * For more direct control over rate limiting behavior without Solid state management,\n * consider using the lower-level createRateLimiter hook instead.\n *\n * @example\n * ```tsx\n * // Basic rate limiting - update at most 5 times per minute with a sliding window\n * const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding'\n * });\n *\n * // Use the rate-limited value\n * console.log(rateLimitedValue()); // Access the current rate-limited value\n *\n * // Control the rate limiter\n * rateLimiter.reset(); // Reset the rate limit window\n * ```\n */\nexport function createRateLimitedValue<TValue>(\n value: Accessor<TValue>,\n initialOptions: RateLimiterOptions<Setter<TValue>>,\n): [Accessor<TValue>, SolidRateLimiter<Setter<TValue>>] {\n const [rateLimitedValue, setRateLimitedValue, rateLimiter] =\n createRateLimitedSignal(value(), initialOptions)\n\n createEffect(() => {\n setRateLimitedValue(value() as any)\n })\n\n return [rateLimitedValue, rateLimiter]\n}\n"],"names":[],"mappings":";;AAiDgB,SAAA,uBACd,OACA,gBACsD;AAChD,QAAA,CAAC,kBAAkB,qBAAqB,WAAW,IACvD,wBAAwB,SAAS,cAAc;AAEjD,eAAa,MAAM;AACjB,wBAAoB,OAAc;AAAA,EAAA,CACnC;AAEM,SAAA,CAAC,kBAAkB,WAAW;AACvC;"}
@@ -17,6 +17,12 @@ export interface SolidRateLimiter<TFn extends AnyFunction> extends Omit<RateLimi
17
17
  * a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,
18
18
  * it does not attempt to space out or collapse executions intelligently.
19
19
  *
20
+ * The rate limiter supports two types of windows:
21
+ * - 'fixed': A strict window that resets after the window period. All executions within the window count
22
+ * towards the limit, and the window resets completely after the period.
23
+ * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more
24
+ * consistent rate of execution over time.
25
+ *
20
26
  * For smoother execution patterns:
21
27
  * - Use throttling when you want consistent spacing between executions (e.g. UI updates)
22
28
  * - Use debouncing when you want to collapse rapid-fire events (e.g. search input)
@@ -24,10 +30,11 @@ export interface SolidRateLimiter<TFn extends AnyFunction> extends Omit<RateLimi
24
30
  *
25
31
  * @example
26
32
  * ```tsx
27
- * // Basic rate limiting - max 5 calls per minute
33
+ * // Basic rate limiting - max 5 calls per minute with a sliding window
28
34
  * const rateLimiter = createRateLimiter(apiCall, {
29
35
  * limit: 5,
30
36
  * window: 60000,
37
+ * windowType: 'sliding'
31
38
  * });
32
39
  *
33
40
  * // Monitor rate limit status
@@ -1 +1 @@
1
- {"version":3,"file":"createRateLimiter.js","sources":["../../../src/rate-limiter/createRateLimiter.ts"],"sourcesContent":["import { RateLimiter } from '@tanstack/pacer/rate-limiter'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\nexport interface SolidRateLimiter<TFn extends AnyFunction>\n extends Omit<\n RateLimiter<TFn>,\n | 'getExecutionCount'\n | 'getMsUntilNextWindow'\n | 'getRejectionCount'\n | 'getRemainingInWindow'\n > {\n executionCount: Accessor<number>\n msUntilNextWindow: Accessor<number>\n rejectionCount: Accessor<number>\n remainingInWindow: Accessor<number>\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 * 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 * @example\n * ```tsx\n * // Basic rate limiting - max 5 calls per minute\n * const rateLimiter = createRateLimiter(apiCall, {\n * limit: 5,\n * window: 60000,\n * });\n *\n * // Monitor rate limit status\n * const handleClick = () => {\n * if (rateLimiter.remainingInWindow() > 0) {\n * rateLimiter.maybeExecute(data);\n * } else {\n * showRateLimitWarning();\n * }\n * };\n *\n * // Access rate limiter state via signals\n * console.log('Executions:', rateLimiter.executionCount());\n * console.log('Rejections:', rateLimiter.rejectionCount());\n * console.log('Remaining:', rateLimiter.remainingInWindow());\n * console.log('Next window in:', rateLimiter.msUntilNextWindow());\n * ```\n */\nexport function createRateLimiter<TFn extends AnyFunction>(\n fn: TFn,\n initialOptions: RateLimiterOptions<TFn>,\n): SolidRateLimiter<TFn> {\n const rateLimiter = bindInstanceMethods(\n new RateLimiter<TFn>(fn, initialOptions),\n )\n\n const [executionCount, setExecutionCount] = createSignal(\n rateLimiter.getExecutionCount(),\n )\n const [rejectionCount, setRejectionCount] = createSignal(\n rateLimiter.getRejectionCount(),\n )\n const [remainingInWindow, setRemainingInWindow] = createSignal(\n rateLimiter.getRemainingInWindow(),\n )\n const [msUntilNextWindow, setMsUntilNextWindow] = createSignal(\n rateLimiter.getMsUntilNextWindow(),\n )\n\n function setOptions(newOptions: Partial<RateLimiterOptions<TFn>>) {\n rateLimiter.setOptions({\n ...newOptions,\n onExecute: (rateLimiter) => {\n setExecutionCount(rateLimiter.getExecutionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onExecute = newOptions.onExecute ?? initialOptions.onExecute\n onExecute?.(rateLimiter)\n },\n onReject: (rateLimiter) => {\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onReject = newOptions.onReject ?? initialOptions.onReject\n onReject?.(rateLimiter)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...rateLimiter,\n executionCount,\n rejectionCount,\n remainingInWindow,\n msUntilNextWindow,\n setOptions,\n } as SolidRateLimiter<TFn>\n}\n"],"names":["rateLimiter"],"mappings":";;;AA4DgB,SAAA,kBACd,IACA,gBACuB;AACvB,QAAM,cAAc;AAAA,IAClB,IAAI,YAAiB,IAAI,cAAc;AAAA,EACzC;AAEM,QAAA,CAAC,gBAAgB,iBAAiB,IAAI;AAAA,IAC1C,YAAY,kBAAkB;AAAA,EAChC;AACM,QAAA,CAAC,gBAAgB,iBAAiB,IAAI;AAAA,IAC1C,YAAY,kBAAkB;AAAA,EAChC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAI;AAAA,IAChD,YAAY,qBAAqB;AAAA,EACnC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAI;AAAA,IAChD,YAAY,qBAAqB;AAAA,EACnC;AAEA,WAAS,WAAW,YAA8C;AAChE,gBAAY,WAAW;AAAA,MACrB,GAAG;AAAA,MACH,WAAW,CAACA,iBAAgB;AACRA,0BAAAA,aAAY,mBAAmB;AAC5BA,6BAAAA,aAAY,sBAAsB;AAClCA,6BAAAA,aAAY,sBAAsB;AAEjD,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAYA;AAAAA,MACd;AAAA,MACA,UAAU,CAACA,iBAAgB;AACPA,0BAAAA,aAAY,mBAAmB;AAC5BA,6BAAAA,aAAY,sBAAsB;AAClCA,6BAAAA,aAAY,sBAAsB;AAEjD,cAAA,WAAW,WAAW,YAAY,eAAe;AACvD,6CAAWA;AAAAA,MAAW;AAAA,IACxB,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAElB,SAAA;AAAA,IACL,GAAG;AAAA,IACH;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;"}
1
+ {"version":3,"file":"createRateLimiter.js","sources":["../../../src/rate-limiter/createRateLimiter.ts"],"sourcesContent":["import { RateLimiter } from '@tanstack/pacer/rate-limiter'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\nexport interface SolidRateLimiter<TFn extends AnyFunction>\n extends Omit<\n RateLimiter<TFn>,\n | 'getExecutionCount'\n | 'getMsUntilNextWindow'\n | 'getRejectionCount'\n | 'getRemainingInWindow'\n > {\n executionCount: Accessor<number>\n msUntilNextWindow: Accessor<number>\n rejectionCount: Accessor<number>\n remainingInWindow: Accessor<number>\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 * @example\n * ```tsx\n * // Basic rate limiting - max 5 calls per minute with a sliding window\n * const rateLimiter = createRateLimiter(apiCall, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding'\n * });\n *\n * // Monitor rate limit status\n * const handleClick = () => {\n * if (rateLimiter.remainingInWindow() > 0) {\n * rateLimiter.maybeExecute(data);\n * } else {\n * showRateLimitWarning();\n * }\n * };\n *\n * // Access rate limiter state via signals\n * console.log('Executions:', rateLimiter.executionCount());\n * console.log('Rejections:', rateLimiter.rejectionCount());\n * console.log('Remaining:', rateLimiter.remainingInWindow());\n * console.log('Next window in:', rateLimiter.msUntilNextWindow());\n * ```\n */\nexport function createRateLimiter<TFn extends AnyFunction>(\n fn: TFn,\n initialOptions: RateLimiterOptions<TFn>,\n): SolidRateLimiter<TFn> {\n const rateLimiter = bindInstanceMethods(\n new RateLimiter<TFn>(fn, initialOptions),\n )\n\n const [executionCount, setExecutionCount] = createSignal(\n rateLimiter.getExecutionCount(),\n )\n const [rejectionCount, setRejectionCount] = createSignal(\n rateLimiter.getRejectionCount(),\n )\n const [remainingInWindow, setRemainingInWindow] = createSignal(\n rateLimiter.getRemainingInWindow(),\n )\n const [msUntilNextWindow, setMsUntilNextWindow] = createSignal(\n rateLimiter.getMsUntilNextWindow(),\n )\n\n function setOptions(newOptions: Partial<RateLimiterOptions<TFn>>) {\n rateLimiter.setOptions({\n ...newOptions,\n onExecute: (rateLimiter) => {\n setExecutionCount(rateLimiter.getExecutionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onExecute = newOptions.onExecute ?? initialOptions.onExecute\n onExecute?.(rateLimiter)\n },\n onReject: (rateLimiter) => {\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onReject = newOptions.onReject ?? initialOptions.onReject\n onReject?.(rateLimiter)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...rateLimiter,\n executionCount,\n rejectionCount,\n remainingInWindow,\n msUntilNextWindow,\n setOptions,\n } as SolidRateLimiter<TFn>\n}\n"],"names":["rateLimiter"],"mappings":";;;AAmEgB,SAAA,kBACd,IACA,gBACuB;AACvB,QAAM,cAAc;AAAA,IAClB,IAAI,YAAiB,IAAI,cAAc;AAAA,EACzC;AAEM,QAAA,CAAC,gBAAgB,iBAAiB,IAAI;AAAA,IAC1C,YAAY,kBAAkB;AAAA,EAChC;AACM,QAAA,CAAC,gBAAgB,iBAAiB,IAAI;AAAA,IAC1C,YAAY,kBAAkB;AAAA,EAChC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAI;AAAA,IAChD,YAAY,qBAAqB;AAAA,EACnC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAI;AAAA,IAChD,YAAY,qBAAqB;AAAA,EACnC;AAEA,WAAS,WAAW,YAA8C;AAChE,gBAAY,WAAW;AAAA,MACrB,GAAG;AAAA,MACH,WAAW,CAACA,iBAAgB;AACRA,0BAAAA,aAAY,mBAAmB;AAC5BA,6BAAAA,aAAY,sBAAsB;AAClCA,6BAAAA,aAAY,sBAAsB;AAEjD,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAYA;AAAAA,MACd;AAAA,MACA,UAAU,CAACA,iBAAgB;AACPA,0BAAAA,aAAY,mBAAmB;AAC5BA,6BAAAA,aAAY,sBAAsB;AAClCA,6BAAAA,aAAY,sBAAsB;AAEjD,cAAA,WAAW,WAAW,YAAY,eAAe;AACvD,6CAAWA;AAAAA,MAAW;AAAA,IACxB,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAElB,SAAA;AAAA,IACL,GAAG;AAAA,IACH;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/solid-pacer",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Utilities for debouncing and throttling functions in Solid.",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -142,10 +142,10 @@
142
142
  "src"
143
143
  ],
144
144
  "dependencies": {
145
- "@tanstack/pacer": "0.3.0"
145
+ "@tanstack/pacer": "0.4.0"
146
146
  },
147
147
  "devDependencies": {
148
- "solid-js": "^1.9.5",
148
+ "solid-js": "^1.9.6",
149
149
  "vite-plugin-solid": "^2.11.6"
150
150
  },
151
151
  "peerDependencies": {
@@ -33,23 +33,34 @@ export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction>
33
33
  * then blocks subsequent calls until the window passes. This is useful for respecting API rate limits,
34
34
  * managing resource constraints, or controlling bursts of async operations.
35
35
  *
36
+ * Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,
37
+ * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call
38
+ * instead of setting the result on a state variable from within the rate-limited function.
39
+ *
40
+ * The rate limiter supports two types of windows:
41
+ * - 'fixed': A strict window that resets after the window period. All executions within the window count
42
+ * towards the limit, and the window resets completely after the period.
43
+ * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more
44
+ * consistent rate of execution over time.
45
+ *
36
46
  * @example
37
47
  * ```tsx
38
- * // Basic API call rate limiting
48
+ * // Basic API call rate limiting with return value
39
49
  * const { maybeExecute } = createAsyncRateLimiter(
40
50
  * async (id: string) => {
41
51
  * const data = await api.fetchData(id);
42
- * return data;
52
+ * return data; // Return value is preserved
43
53
  * },
44
54
  * { limit: 5, window: 1000 } // 5 calls per second
45
55
  * );
46
56
  *
47
- * // With state management
57
+ * // With state management and return value
48
58
  * const [data, setData] = createSignal(null);
49
59
  * const { maybeExecute } = createAsyncRateLimiter(
50
60
  * async (query) => {
51
61
  * const result = await searchAPI(query);
52
62
  * setData(result);
63
+ * return result; // Return value can be used by the caller
53
64
  * },
54
65
  * {
55
66
  * limit: 10,
@@ -12,6 +12,12 @@ import type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'
12
12
  * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out
13
13
  * or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.
14
14
  *
15
+ * The rate limiter supports two types of windows:
16
+ * - 'fixed': A strict window that resets after the window period. All updates within the window count
17
+ * towards the limit, and the window resets completely after the period.
18
+ * - 'sliding': A rolling window that allows updates as old ones expire. This provides a more
19
+ * consistent rate of updates over time.
20
+ *
15
21
  * For smoother update patterns, consider:
16
22
  * - createThrottledSignal: When you want consistent spacing between updates (e.g. UI changes)
17
23
  * - createDebouncedSignal: When you want to collapse rapid updates into a single update (e.g. search input)
@@ -28,16 +34,18 @@ import type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'
28
34
  *
29
35
  * @example
30
36
  * ```tsx
31
- * // Basic rate limiting - update state at most 5 times per minute
37
+ * // Basic rate limiting - update state at most 5 times per minute with a sliding window
32
38
  * const [value, setValue, rateLimiter] = createRateLimitedSignal(0, {
33
39
  * limit: 5,
34
- * window: 60000
40
+ * window: 60000,
41
+ * windowType: 'sliding'
35
42
  * });
36
43
  *
37
- * // With rejection callback
44
+ * // With rejection callback and fixed window
38
45
  * const [value, setValue] = createRateLimitedSignal(0, {
39
46
  * limit: 3,
40
47
  * window: 5000,
48
+ * windowType: 'fixed',
41
49
  * onReject: (rateLimiter) => {
42
50
  * alert(`Rate limit reached. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
43
51
  * }
@@ -12,6 +12,12 @@ import type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'
12
12
  * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out
13
13
  * or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.
14
14
  *
15
+ * The rate limiter supports two types of windows:
16
+ * - 'fixed': A strict window that resets after the window period. All updates within the window count
17
+ * towards the limit, and the window resets completely after the period.
18
+ * - 'sliding': A rolling window that allows updates as old ones expire. This provides a more
19
+ * consistent rate of updates over time.
20
+ *
15
21
  * For smoother update patterns, consider:
16
22
  * - createThrottledValue: When you want consistent spacing between updates (e.g. UI changes)
17
23
  * - createDebouncedValue: When you want to collapse rapid updates into a single update (e.g. search input)
@@ -27,10 +33,11 @@ import type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'
27
33
  *
28
34
  * @example
29
35
  * ```tsx
30
- * // Basic rate limiting - update at most 5 times per minute
36
+ * // Basic rate limiting - update at most 5 times per minute with a sliding window
31
37
  * const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {
32
38
  * limit: 5,
33
- * window: 60000
39
+ * window: 60000,
40
+ * windowType: 'sliding'
34
41
  * });
35
42
  *
36
43
  * // Use the rate-limited value
@@ -29,6 +29,12 @@ export interface SolidRateLimiter<TFn extends AnyFunction>
29
29
  * a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,
30
30
  * it does not attempt to space out or collapse executions intelligently.
31
31
  *
32
+ * The rate limiter supports two types of windows:
33
+ * - 'fixed': A strict window that resets after the window period. All executions within the window count
34
+ * towards the limit, and the window resets completely after the period.
35
+ * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more
36
+ * consistent rate of execution over time.
37
+ *
32
38
  * For smoother execution patterns:
33
39
  * - Use throttling when you want consistent spacing between executions (e.g. UI updates)
34
40
  * - Use debouncing when you want to collapse rapid-fire events (e.g. search input)
@@ -36,10 +42,11 @@ export interface SolidRateLimiter<TFn extends AnyFunction>
36
42
  *
37
43
  * @example
38
44
  * ```tsx
39
- * // Basic rate limiting - max 5 calls per minute
45
+ * // Basic rate limiting - max 5 calls per minute with a sliding window
40
46
  * const rateLimiter = createRateLimiter(apiCall, {
41
47
  * limit: 5,
42
48
  * window: 60000,
49
+ * windowType: 'sliding'
43
50
  * });
44
51
  *
45
52
  * // Monitor rate limit status