@tanstack/preact-pacer 0.18.0 → 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.
- package/dist/async-batcher/index.cjs +3 -3
- package/dist/async-batcher/index.d.cts +2 -2
- package/dist/async-batcher/index.d.ts +2 -2
- package/dist/async-batcher/useAsyncBatcher.cjs +35 -11
- package/dist/async-batcher/useAsyncBatcher.cjs.map +1 -1
- package/dist/async-batcher/useAsyncBatcher.d.cts +45 -10
- package/dist/async-batcher/useAsyncBatcher.d.ts +45 -10
- package/dist/async-batcher/useAsyncBatcher.js +32 -8
- package/dist/async-batcher/useAsyncBatcher.js.map +1 -1
- package/dist/async-debouncer/index.cjs +3 -3
- package/dist/async-debouncer/index.d.cts +2 -2
- package/dist/async-debouncer/index.d.ts +2 -2
- package/dist/async-debouncer/useAsyncDebouncer.cjs +35 -11
- package/dist/async-debouncer/useAsyncDebouncer.cjs.map +1 -1
- package/dist/async-debouncer/useAsyncDebouncer.d.cts +45 -10
- package/dist/async-debouncer/useAsyncDebouncer.d.ts +45 -10
- package/dist/async-debouncer/useAsyncDebouncer.js +32 -8
- package/dist/async-debouncer/useAsyncDebouncer.js.map +1 -1
- package/dist/async-queuer/index.cjs +3 -3
- package/dist/async-queuer/index.d.cts +2 -2
- package/dist/async-queuer/index.d.ts +2 -2
- package/dist/async-queuer/useAsyncQueuedState.cjs.map +1 -1
- package/dist/async-queuer/useAsyncQueuedState.d.cts +2 -2
- package/dist/async-queuer/useAsyncQueuedState.d.ts +2 -2
- package/dist/async-queuer/useAsyncQueuedState.js.map +1 -1
- package/dist/async-queuer/useAsyncQueuer.cjs +35 -11
- package/dist/async-queuer/useAsyncQueuer.cjs.map +1 -1
- package/dist/async-queuer/useAsyncQueuer.d.cts +45 -10
- package/dist/async-queuer/useAsyncQueuer.d.ts +45 -10
- package/dist/async-queuer/useAsyncQueuer.js +32 -8
- package/dist/async-queuer/useAsyncQueuer.js.map +1 -1
- package/dist/async-rate-limiter/index.cjs +3 -3
- package/dist/async-rate-limiter/index.d.cts +2 -2
- package/dist/async-rate-limiter/index.d.ts +2 -2
- package/dist/async-rate-limiter/useAsyncRateLimiter.cjs +35 -11
- package/dist/async-rate-limiter/useAsyncRateLimiter.cjs.map +1 -1
- package/dist/async-rate-limiter/useAsyncRateLimiter.d.cts +45 -10
- package/dist/async-rate-limiter/useAsyncRateLimiter.d.ts +45 -10
- package/dist/async-rate-limiter/useAsyncRateLimiter.js +32 -8
- package/dist/async-rate-limiter/useAsyncRateLimiter.js.map +1 -1
- package/dist/async-retryer/index.cjs +3 -3
- package/dist/async-throttler/index.cjs +3 -3
- package/dist/async-throttler/index.d.cts +2 -2
- package/dist/async-throttler/index.d.ts +2 -2
- package/dist/async-throttler/useAsyncThrottler.cjs +35 -11
- package/dist/async-throttler/useAsyncThrottler.cjs.map +1 -1
- package/dist/async-throttler/useAsyncThrottler.d.cts +45 -10
- package/dist/async-throttler/useAsyncThrottler.d.ts +45 -10
- package/dist/async-throttler/useAsyncThrottler.js +32 -8
- package/dist/async-throttler/useAsyncThrottler.js.map +1 -1
- package/dist/batcher/index.cjs +3 -3
- package/dist/batcher/useBatcher.cjs +35 -11
- package/dist/batcher/useBatcher.cjs.map +1 -1
- package/dist/batcher/useBatcher.d.cts +42 -7
- package/dist/batcher/useBatcher.d.ts +42 -7
- package/dist/batcher/useBatcher.js +32 -8
- package/dist/batcher/useBatcher.js.map +1 -1
- package/dist/debouncer/index.cjs +3 -3
- package/dist/debouncer/useDebouncer.cjs +35 -11
- package/dist/debouncer/useDebouncer.cjs.map +1 -1
- package/dist/debouncer/useDebouncer.d.cts +42 -7
- package/dist/debouncer/useDebouncer.d.ts +42 -7
- package/dist/debouncer/useDebouncer.js +32 -8
- package/dist/debouncer/useDebouncer.js.map +1 -1
- package/dist/index.cjs +3 -3
- package/dist/index.d.cts +6 -6
- package/dist/index.d.ts +6 -6
- package/dist/provider/PacerProvider.d.cts +1 -1
- package/dist/queuer/index.cjs +3 -3
- package/dist/queuer/useQueuer.cjs +35 -11
- package/dist/queuer/useQueuer.cjs.map +1 -1
- package/dist/queuer/useQueuer.d.cts +42 -7
- package/dist/queuer/useQueuer.d.ts +42 -7
- package/dist/queuer/useQueuer.js +32 -8
- package/dist/queuer/useQueuer.js.map +1 -1
- package/dist/rate-limiter/index.cjs +3 -3
- package/dist/rate-limiter/useRateLimiter.cjs +35 -11
- package/dist/rate-limiter/useRateLimiter.cjs.map +1 -1
- package/dist/rate-limiter/useRateLimiter.d.cts +42 -7
- package/dist/rate-limiter/useRateLimiter.d.ts +42 -7
- package/dist/rate-limiter/useRateLimiter.js +32 -8
- package/dist/rate-limiter/useRateLimiter.js.map +1 -1
- package/dist/throttler/index.cjs +3 -3
- package/dist/throttler/useThrottler.cjs +35 -11
- package/dist/throttler/useThrottler.cjs.map +1 -1
- package/dist/throttler/useThrottler.d.cts +42 -7
- package/dist/throttler/useThrottler.d.ts +42 -7
- package/dist/throttler/useThrottler.js +32 -8
- package/dist/throttler/useThrottler.js.map +1 -1
- package/dist/types/index.cjs +3 -3
- package/dist/utils/index.cjs +3 -3
- package/package.json +1 -1
- package/src/async-batcher/useAsyncBatcher.ts +64 -13
- package/src/async-debouncer/useAsyncDebouncer.ts +64 -13
- package/src/async-queuer/useAsyncQueuedState.ts +2 -2
- package/src/async-queuer/useAsyncQueuer.ts +64 -13
- package/src/async-rate-limiter/useAsyncRateLimiter.ts +64 -13
- package/src/async-throttler/useAsyncThrottler.ts +64 -13
- package/src/batcher/useBatcher.ts +61 -8
- package/src/debouncer/useDebouncer.ts +61 -8
- package/src/queuer/useQueuer.ts +61 -8
- package/src/rate-limiter/useRateLimiter.ts +61 -8
- package/src/throttler/useThrottler.ts +61 -8
|
@@ -1,9 +1,27 @@
|
|
|
1
|
+
import { ComponentChildren } from "preact";
|
|
1
2
|
import { Store } from "@tanstack/preact-store";
|
|
2
3
|
import { Debouncer, DebouncerOptions, DebouncerState } from "@tanstack/pacer/debouncer";
|
|
3
4
|
import { AnyFunction } from "@tanstack/pacer/types";
|
|
4
5
|
|
|
5
6
|
//#region src/debouncer/useDebouncer.d.ts
|
|
6
7
|
interface PreactDebouncer<TFn extends AnyFunction, TSelected = {}> extends Omit<Debouncer<TFn>, 'store'> {
|
|
8
|
+
/**
|
|
9
|
+
* A Preact HOC (Higher Order Component) that allows you to subscribe to the debouncer state.
|
|
10
|
+
*
|
|
11
|
+
* This is useful for opting into state re-renders for specific parts of the debouncer state
|
|
12
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* <debouncer.Subscribe selector={(state) => ({ isPending: state.isPending })}>
|
|
16
|
+
* {({ isPending }) => (
|
|
17
|
+
* <div>{isPending ? 'Loading...' : 'Ready'}</div>
|
|
18
|
+
* )}
|
|
19
|
+
* </debouncer.Subscribe>
|
|
20
|
+
*/
|
|
21
|
+
Subscribe: <TSelected>(props: {
|
|
22
|
+
selector: (state: DebouncerState<TFn>) => TSelected;
|
|
23
|
+
children: ((state: TSelected) => ComponentChildren) | ComponentChildren;
|
|
24
|
+
}) => ComponentChildren;
|
|
7
25
|
/**
|
|
8
26
|
* Reactive state that will be updated and re-rendered when the debouncer state changes
|
|
9
27
|
*
|
|
@@ -34,14 +52,24 @@ interface PreactDebouncer<TFn extends AnyFunction, TSelected = {}> extends Omit<
|
|
|
34
52
|
*
|
|
35
53
|
* ## State Management and Selector
|
|
36
54
|
*
|
|
37
|
-
* The hook uses TanStack Store for reactive state management.
|
|
38
|
-
*
|
|
39
|
-
*
|
|
55
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
56
|
+
* in two ways:
|
|
57
|
+
*
|
|
58
|
+
* **1. Using `debouncer.Subscribe` HOC (Recommended for component tree subscriptions)**
|
|
59
|
+
*
|
|
60
|
+
* Use the `Subscribe` HOC to subscribe to state changes deep in your component tree without
|
|
61
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
62
|
+
* in child components.
|
|
63
|
+
*
|
|
64
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
65
|
+
*
|
|
66
|
+
* The `selector` parameter allows you to specify which state changes will trigger a re-render
|
|
67
|
+
* at the hook level, optimizing performance by preventing unnecessary re-renders when irrelevant
|
|
68
|
+
* state changes occur.
|
|
40
69
|
*
|
|
41
70
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
42
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
43
|
-
* full control over when your component updates.
|
|
44
|
-
* component re-render when the selected state values change.
|
|
71
|
+
* tracking by providing a selector function or using the `Subscribe` HOC. This prevents unnecessary
|
|
72
|
+
* re-renders and gives you full control over when your component updates.
|
|
45
73
|
*
|
|
46
74
|
* Available state properties:
|
|
47
75
|
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
@@ -58,7 +86,14 @@ interface PreactDebouncer<TFn extends AnyFunction, TSelected = {}> extends Omit<
|
|
|
58
86
|
* { wait: 500 }
|
|
59
87
|
* );
|
|
60
88
|
*
|
|
61
|
-
* //
|
|
89
|
+
* // Subscribe to state changes deep in component tree using Subscribe HOC
|
|
90
|
+
* <searchDebouncer.Subscribe selector={(state) => ({ isPending: state.isPending })}>
|
|
91
|
+
* {({ isPending }) => (
|
|
92
|
+
* <div>{isPending ? 'Searching...' : 'Ready'}</div>
|
|
93
|
+
* )}
|
|
94
|
+
* </searchDebouncer.Subscribe>
|
|
95
|
+
*
|
|
96
|
+
* // Opt-in to re-render when isPending changes at hook level (optimized for loading states)
|
|
62
97
|
* const searchDebouncer = useDebouncer(
|
|
63
98
|
* (query: string) => fetchSearchResults(query),
|
|
64
99
|
* { wait: 500 },
|
|
@@ -21,14 +21,24 @@ import { Debouncer } from "@tanstack/pacer/debouncer";
|
|
|
21
21
|
*
|
|
22
22
|
* ## State Management and Selector
|
|
23
23
|
*
|
|
24
|
-
* The hook uses TanStack Store for reactive state management.
|
|
25
|
-
*
|
|
26
|
-
*
|
|
24
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
25
|
+
* in two ways:
|
|
26
|
+
*
|
|
27
|
+
* **1. Using `debouncer.Subscribe` HOC (Recommended for component tree subscriptions)**
|
|
28
|
+
*
|
|
29
|
+
* Use the `Subscribe` HOC to subscribe to state changes deep in your component tree without
|
|
30
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
31
|
+
* in child components.
|
|
32
|
+
*
|
|
33
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
34
|
+
*
|
|
35
|
+
* The `selector` parameter allows you to specify which state changes will trigger a re-render
|
|
36
|
+
* at the hook level, optimizing performance by preventing unnecessary re-renders when irrelevant
|
|
37
|
+
* state changes occur.
|
|
27
38
|
*
|
|
28
39
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
29
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
30
|
-
* full control over when your component updates.
|
|
31
|
-
* component re-render when the selected state values change.
|
|
40
|
+
* tracking by providing a selector function or using the `Subscribe` HOC. This prevents unnecessary
|
|
41
|
+
* re-renders and gives you full control over when your component updates.
|
|
32
42
|
*
|
|
33
43
|
* Available state properties:
|
|
34
44
|
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
@@ -45,7 +55,14 @@ import { Debouncer } from "@tanstack/pacer/debouncer";
|
|
|
45
55
|
* { wait: 500 }
|
|
46
56
|
* );
|
|
47
57
|
*
|
|
48
|
-
* //
|
|
58
|
+
* // Subscribe to state changes deep in component tree using Subscribe HOC
|
|
59
|
+
* <searchDebouncer.Subscribe selector={(state) => ({ isPending: state.isPending })}>
|
|
60
|
+
* {({ isPending }) => (
|
|
61
|
+
* <div>{isPending ? 'Searching...' : 'Ready'}</div>
|
|
62
|
+
* )}
|
|
63
|
+
* </searchDebouncer.Subscribe>
|
|
64
|
+
*
|
|
65
|
+
* // Opt-in to re-render when isPending changes at hook level (optimized for loading states)
|
|
49
66
|
* const searchDebouncer = useDebouncer(
|
|
50
67
|
* (query: string) => fetchSearchResults(query),
|
|
51
68
|
* { wait: 500 },
|
|
@@ -84,7 +101,14 @@ function useDebouncer(fn, options, selector = () => ({})) {
|
|
|
84
101
|
...useDefaultPacerOptions().debouncer,
|
|
85
102
|
...options
|
|
86
103
|
};
|
|
87
|
-
const [debouncer] = useState(() =>
|
|
104
|
+
const [debouncer] = useState(() => {
|
|
105
|
+
const debouncerInstance = new Debouncer(fn, mergedOptions);
|
|
106
|
+
debouncerInstance.Subscribe = function Subscribe(props) {
|
|
107
|
+
const selected = useStore(debouncerInstance.store, props.selector);
|
|
108
|
+
return typeof props.children === "function" ? props.children(selected) : props.children;
|
|
109
|
+
};
|
|
110
|
+
return debouncerInstance;
|
|
111
|
+
});
|
|
88
112
|
debouncer.fn = fn;
|
|
89
113
|
debouncer.setOptions(mergedOptions);
|
|
90
114
|
useEffect(() => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDebouncer.js","names":[],"sources":["../../src/debouncer/useDebouncer.ts"],"sourcesContent":["import { useEffect, useMemo, useState } from 'preact/hooks'\nimport { Debouncer } from '@tanstack/pacer/debouncer'\nimport { useStore } from '@tanstack/preact-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/preact-store'\nimport type {\n DebouncerOptions,\n DebouncerState,\n} from '@tanstack/pacer/debouncer'\nimport type { AnyFunction } from '@tanstack/pacer/types'\n\nexport interface PreactDebouncer<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Debouncer<TFn>, 'store'> {\n /**\n * Reactive state that will be updated and re-rendered when the debouncer state changes\n *\n * Use this instead of `debouncer.store.state`\n */\n readonly state: Readonly<TSelected>\n /**\n * @deprecated Use `debouncer.state` instead of `debouncer.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<DebouncerState<TFn>>>\n}\n\n/**\n * A Preact hook that creates and manages a Debouncer instance.\n *\n * This is a lower-level hook that provides direct access to the Debouncer's functionality without\n * any built-in state management. This allows you to integrate it with any state management solution\n * you prefer (useState, Redux, Zustand, etc.).\n *\n * This hook provides debouncing functionality to limit how often a function can be called,\n * waiting for a specified delay before executing the latest call. This is useful for handling\n * frequent events like window resizing, scroll events, or real-time search inputs.\n *\n * The debouncer will only execute the function after the specified wait time has elapsed\n * since the last call. If the function is called again before the wait time expires, the\n * timer resets and starts waiting again.\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
1
|
+
{"version":3,"file":"useDebouncer.js","names":[],"sources":["../../src/debouncer/useDebouncer.ts"],"sourcesContent":["import { useEffect, useMemo, useState } from 'preact/hooks'\nimport { Debouncer } from '@tanstack/pacer/debouncer'\nimport { useStore } from '@tanstack/preact-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/preact-store'\nimport type {\n DebouncerOptions,\n DebouncerState,\n} from '@tanstack/pacer/debouncer'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type { ComponentChildren } from 'preact'\n\nexport interface PreactDebouncer<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Debouncer<TFn>, 'store'> {\n /**\n * A Preact HOC (Higher Order Component) that allows you to subscribe to the debouncer state.\n *\n * This is useful for opting into state re-renders for specific parts of the debouncer state\n * deep in your component tree without needing to pass a selector to the hook.\n *\n * @example\n * <debouncer.Subscribe selector={(state) => ({ isPending: state.isPending })}>\n * {({ isPending }) => (\n * <div>{isPending ? 'Loading...' : 'Ready'}</div>\n * )}\n * </debouncer.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: DebouncerState<TFn>) => TSelected\n children: ((state: TSelected) => ComponentChildren) | ComponentChildren\n }) => ComponentChildren\n /**\n * Reactive state that will be updated and re-rendered when the debouncer state changes\n *\n * Use this instead of `debouncer.store.state`\n */\n readonly state: Readonly<TSelected>\n /**\n * @deprecated Use `debouncer.state` instead of `debouncer.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<DebouncerState<TFn>>>\n}\n\n/**\n * A Preact hook that creates and manages a Debouncer instance.\n *\n * This is a lower-level hook that provides direct access to the Debouncer's functionality without\n * any built-in state management. This allows you to integrate it with any state management solution\n * you prefer (useState, Redux, Zustand, etc.).\n *\n * This hook provides debouncing functionality to limit how often a function can be called,\n * waiting for a specified delay before executing the latest call. This is useful for handling\n * frequent events like window resizing, scroll events, or real-time search inputs.\n *\n * The debouncer will only execute the function after the specified wait time has elapsed\n * since the last call. If the function is called again before the wait time expires, the\n * timer resets and starts waiting again.\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 `debouncer.Subscribe` HOC (Recommended for component tree subscriptions)**\n *\n * Use the `Subscribe` HOC 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 a re-render\n * at the hook level, optimizing performance by preventing unnecessary re-renders 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` HOC. This prevents unnecessary\n * re-renders and gives you full control over when your component updates.\n *\n * Available state properties:\n * - `canLeadingExecute`: Whether the debouncer can execute on the leading edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const searchDebouncer = useDebouncer(\n * (query: string) => fetchSearchResults(query),\n * { wait: 500 }\n * );\n *\n * // Subscribe to state changes deep in component tree using Subscribe HOC\n * <searchDebouncer.Subscribe selector={(state) => ({ isPending: state.isPending })}>\n * {({ isPending }) => (\n * <div>{isPending ? 'Searching...' : 'Ready'}</div>\n * )}\n * </searchDebouncer.Subscribe>\n *\n * // Opt-in to re-render when isPending changes at hook level (optimized for loading states)\n * const searchDebouncer = useDebouncer(\n * (query: string) => fetchSearchResults(query),\n * { wait: 500 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Opt-in to re-render when executionCount changes (optimized for tracking execution)\n * const searchDebouncer = useDebouncer(\n * (query: string) => fetchSearchResults(query),\n * { wait: 500 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Multiple state properties - re-render when any of these change\n * const searchDebouncer = useDebouncer(\n * (query: string) => fetchSearchResults(query),\n * { wait: 500 },\n * (state) => ({\n * isPending: state.isPending,\n * executionCount: state.executionCount,\n * status: state.status\n * })\n * );\n *\n * // In an event handler\n * const handleChange = (e) => {\n * searchDebouncer.maybeExecute(e.target.value);\n * };\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isPending } = searchDebouncer.state;\n * ```\n */\nexport function useDebouncer<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: DebouncerOptions<TFn>,\n selector: (state: DebouncerState<TFn>) => TSelected = () => ({}) as TSelected,\n): PreactDebouncer<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().debouncer,\n ...options,\n } as DebouncerOptions<TFn>\n\n const [debouncer] = useState(() => {\n const debouncerInstance = new Debouncer(\n fn,\n mergedOptions,\n ) as unknown as PreactDebouncer<TFn, TSelected>\n\n debouncerInstance.Subscribe = function Subscribe<TSelected>(props: {\n selector: (state: DebouncerState<TFn>) => TSelected\n children: ((state: TSelected) => ComponentChildren) | ComponentChildren\n }) {\n const selected = useStore(debouncerInstance.store, props.selector)\n\n return typeof props.children === 'function'\n ? props.children(selected)\n : props.children\n }\n\n return debouncerInstance\n })\n\n debouncer.fn = fn\n debouncer.setOptions(mergedOptions)\n\n useEffect(() => {\n return () => {\n debouncer.cancel()\n }\n }, [debouncer])\n\n const state = useStore(debouncer.store, selector)\n\n return useMemo(\n () =>\n ({\n ...debouncer,\n state,\n }) as PreactDebouncer<TFn, TSelected>, // omit `store` in favor of `state`\n [debouncer, state],\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2IA,SAAgB,aACd,IACA,SACA,kBAA6D,EAAE,GAC9B;CACjC,MAAM,gBAAgB;EACpB,GAAG,wBAAwB,CAAC;EAC5B,GAAG;EACJ;CAED,MAAM,CAAC,aAAa,eAAe;EACjC,MAAM,oBAAoB,IAAI,UAC5B,IACA,cACD;AAED,oBAAkB,YAAY,SAAS,UAAqB,OAGzD;GACD,MAAM,WAAW,SAAS,kBAAkB,OAAO,MAAM,SAAS;AAElE,UAAO,OAAO,MAAM,aAAa,aAC7B,MAAM,SAAS,SAAS,GACxB,MAAM;;AAGZ,SAAO;GACP;AAEF,WAAU,KAAK;AACf,WAAU,WAAW,cAAc;AAEnC,iBAAgB;AACd,eAAa;AACX,aAAU,QAAQ;;IAEnB,CAAC,UAAU,CAAC;CAEf,MAAM,QAAQ,SAAS,UAAU,OAAO,SAAS;AAEjD,QAAO,eAEF;EACC,GAAG;EACH;EACD,GACH,CAAC,WAAW,MAAM,CACnB"}
|
package/dist/index.cjs
CHANGED
|
@@ -57,10 +57,10 @@ exports.useThrottledCallback = require_useThrottledCallback.useThrottledCallback
|
|
|
57
57
|
exports.useThrottledState = require_useThrottledState.useThrottledState;
|
|
58
58
|
exports.useThrottledValue = require_useThrottledValue.useThrottledValue;
|
|
59
59
|
exports.useThrottler = require_useThrottler.useThrottler;
|
|
60
|
-
var
|
|
61
|
-
Object.keys(
|
|
60
|
+
var _tanstack_pacer = require("@tanstack/pacer");
|
|
61
|
+
Object.keys(_tanstack_pacer).forEach(function (k) {
|
|
62
62
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
63
63
|
enumerable: true,
|
|
64
|
-
get: function () { return
|
|
64
|
+
get: function () { return _tanstack_pacer[k]; }
|
|
65
65
|
});
|
|
66
66
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PreactAsyncBatcher, useAsyncBatcher } from "./async-batcher/useAsyncBatcher.cjs";
|
|
2
2
|
import { useAsyncBatchedCallback } from "./async-batcher/useAsyncBatchedCallback.cjs";
|
|
3
|
-
import {
|
|
3
|
+
import { PreactAsyncDebouncer, useAsyncDebouncer } from "./async-debouncer/useAsyncDebouncer.cjs";
|
|
4
4
|
import { useAsyncDebouncedCallback } from "./async-debouncer/useAsyncDebouncedCallback.cjs";
|
|
5
|
-
import {
|
|
5
|
+
import { PreactAsyncQueuer, useAsyncQueuer } from "./async-queuer/useAsyncQueuer.cjs";
|
|
6
6
|
import { useAsyncQueuedState } from "./async-queuer/useAsyncQueuedState.cjs";
|
|
7
|
-
import {
|
|
7
|
+
import { PreactAsyncRateLimiter, useAsyncRateLimiter } from "./async-rate-limiter/useAsyncRateLimiter.cjs";
|
|
8
8
|
import { useAsyncRateLimitedCallback } from "./async-rate-limiter/useAsyncRateLimitedCallback.cjs";
|
|
9
|
-
import {
|
|
9
|
+
import { PreactAsyncThrottler, useAsyncThrottler } from "./async-throttler/useAsyncThrottler.cjs";
|
|
10
10
|
import { useAsyncThrottledCallback } from "./async-throttler/useAsyncThrottledCallback.cjs";
|
|
11
11
|
import { PreactBatcher, useBatcher } from "./batcher/useBatcher.cjs";
|
|
12
12
|
import { useBatchedCallback } from "./batcher/useBatchedCallback.cjs";
|
|
@@ -27,4 +27,4 @@ import { PreactThrottler, useThrottler } from "./throttler/useThrottler.cjs";
|
|
|
27
27
|
import { useThrottledState } from "./throttler/useThrottledState.cjs";
|
|
28
28
|
import { useThrottledValue } from "./throttler/useThrottledValue.cjs";
|
|
29
29
|
export * from "@tanstack/pacer";
|
|
30
|
-
export { PacerProvider, PacerProviderOptions, PacerProviderProps,
|
|
30
|
+
export { PacerProvider, PacerProviderOptions, PacerProviderProps, PreactAsyncBatcher, PreactAsyncDebouncer, PreactAsyncQueuer, PreactAsyncRateLimiter, PreactAsyncThrottler, PreactBatcher, PreactDebouncer, PreactQueuer, PreactRateLimiter, PreactThrottler, useAsyncBatchedCallback, useAsyncBatcher, useAsyncDebouncedCallback, useAsyncDebouncer, useAsyncQueuedState, useAsyncQueuer, useAsyncRateLimitedCallback, useAsyncRateLimiter, useAsyncThrottledCallback, useAsyncThrottler, useBatchedCallback, useBatcher, useDebouncedCallback, useDebouncedState, useDebouncedValue, useDebouncer, useDefaultPacerOptions, usePacerContext, useQueuedState, useQueuedValue, useQueuer, useRateLimitedCallback, useRateLimitedState, useRateLimitedValue, useRateLimiter, useThrottledCallback, useThrottledState, useThrottledValue, useThrottler };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PreactAsyncBatcher, useAsyncBatcher } from "./async-batcher/useAsyncBatcher.js";
|
|
2
2
|
import { useAsyncBatchedCallback } from "./async-batcher/useAsyncBatchedCallback.js";
|
|
3
|
-
import {
|
|
3
|
+
import { PreactAsyncDebouncer, useAsyncDebouncer } from "./async-debouncer/useAsyncDebouncer.js";
|
|
4
4
|
import { useAsyncDebouncedCallback } from "./async-debouncer/useAsyncDebouncedCallback.js";
|
|
5
|
-
import {
|
|
5
|
+
import { PreactAsyncQueuer, useAsyncQueuer } from "./async-queuer/useAsyncQueuer.js";
|
|
6
6
|
import { useAsyncQueuedState } from "./async-queuer/useAsyncQueuedState.js";
|
|
7
|
-
import {
|
|
7
|
+
import { PreactAsyncRateLimiter, useAsyncRateLimiter } from "./async-rate-limiter/useAsyncRateLimiter.js";
|
|
8
8
|
import { useAsyncRateLimitedCallback } from "./async-rate-limiter/useAsyncRateLimitedCallback.js";
|
|
9
|
-
import {
|
|
9
|
+
import { PreactAsyncThrottler, useAsyncThrottler } from "./async-throttler/useAsyncThrottler.js";
|
|
10
10
|
import { useAsyncThrottledCallback } from "./async-throttler/useAsyncThrottledCallback.js";
|
|
11
11
|
import { PreactBatcher, useBatcher } from "./batcher/useBatcher.js";
|
|
12
12
|
import { useBatchedCallback } from "./batcher/useBatchedCallback.js";
|
|
@@ -27,4 +27,4 @@ import { PreactThrottler, useThrottler } from "./throttler/useThrottler.js";
|
|
|
27
27
|
import { useThrottledState } from "./throttler/useThrottledState.js";
|
|
28
28
|
import { useThrottledValue } from "./throttler/useThrottledValue.js";
|
|
29
29
|
export * from "@tanstack/pacer";
|
|
30
|
-
export { PacerProvider, PacerProviderOptions, PacerProviderProps,
|
|
30
|
+
export { PacerProvider, PacerProviderOptions, PacerProviderProps, PreactAsyncBatcher, PreactAsyncDebouncer, PreactAsyncQueuer, PreactAsyncRateLimiter, PreactAsyncThrottler, PreactBatcher, PreactDebouncer, PreactQueuer, PreactRateLimiter, PreactThrottler, useAsyncBatchedCallback, useAsyncBatcher, useAsyncDebouncedCallback, useAsyncDebouncer, useAsyncQueuedState, useAsyncQueuer, useAsyncRateLimitedCallback, useAsyncRateLimiter, useAsyncThrottledCallback, useAsyncThrottler, useBatchedCallback, useBatcher, useDebouncedCallback, useDebouncedState, useDebouncedValue, useDebouncer, useDefaultPacerOptions, usePacerContext, useQueuedState, useQueuedValue, useQueuer, useRateLimitedCallback, useRateLimitedState, useRateLimitedValue, useRateLimiter, useThrottledCallback, useThrottledState, useThrottledValue, useThrottler };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { AnyAsyncFunction, AnyFunction, AsyncBatcherOptions, AsyncDebouncerOptions, AsyncQueuerOptions, AsyncRateLimiterOptions, AsyncThrottlerOptions, BatcherOptions, DebouncerOptions, QueuerOptions, RateLimiterOptions, ThrottlerOptions } from "@tanstack/pacer";
|
|
2
1
|
import * as preact0 from "preact";
|
|
3
2
|
import { ComponentChildren } from "preact";
|
|
3
|
+
import { AnyAsyncFunction, AnyFunction, AsyncBatcherOptions, AsyncDebouncerOptions, AsyncQueuerOptions, AsyncRateLimiterOptions, AsyncThrottlerOptions, BatcherOptions, DebouncerOptions, QueuerOptions, RateLimiterOptions, ThrottlerOptions } from "@tanstack/pacer";
|
|
4
4
|
|
|
5
5
|
//#region src/provider/PacerProvider.d.ts
|
|
6
6
|
interface PacerProviderOptions {
|
package/dist/queuer/index.cjs
CHANGED
|
@@ -5,10 +5,10 @@ const require_useQueuedValue = require('./useQueuedValue.cjs');
|
|
|
5
5
|
exports.useQueuedState = require_useQueuedState.useQueuedState;
|
|
6
6
|
exports.useQueuedValue = require_useQueuedValue.useQueuedValue;
|
|
7
7
|
exports.useQueuer = require_useQueuer.useQueuer;
|
|
8
|
-
var
|
|
9
|
-
Object.keys(
|
|
8
|
+
var _tanstack_pacer_queuer = require("@tanstack/pacer/queuer");
|
|
9
|
+
Object.keys(_tanstack_pacer_queuer).forEach(function (k) {
|
|
10
10
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
11
11
|
enumerable: true,
|
|
12
|
-
get: function () { return
|
|
12
|
+
get: function () { return _tanstack_pacer_queuer[k]; }
|
|
13
13
|
});
|
|
14
14
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const require_PacerProvider = require('../provider/PacerProvider.cjs');
|
|
2
2
|
let preact_hooks = require("preact/hooks");
|
|
3
|
-
let
|
|
4
|
-
let
|
|
3
|
+
let _tanstack_preact_store = require("@tanstack/preact-store");
|
|
4
|
+
let _tanstack_pacer_queuer = require("@tanstack/pacer/queuer");
|
|
5
5
|
|
|
6
6
|
//#region src/queuer/useQueuer.ts
|
|
7
7
|
/**
|
|
@@ -23,14 +23,24 @@ let __tanstack_pacer_queuer = require("@tanstack/pacer/queuer");
|
|
|
23
23
|
*
|
|
24
24
|
* ## State Management and Selector
|
|
25
25
|
*
|
|
26
|
-
* The hook uses TanStack Store for reactive state management.
|
|
27
|
-
*
|
|
28
|
-
*
|
|
26
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
27
|
+
* in two ways:
|
|
28
|
+
*
|
|
29
|
+
* **1. Using `queuer.Subscribe` HOC (Recommended for component tree subscriptions)**
|
|
30
|
+
*
|
|
31
|
+
* Use the `Subscribe` HOC to subscribe to state changes deep in your component tree without
|
|
32
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
33
|
+
* in child components.
|
|
34
|
+
*
|
|
35
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
36
|
+
*
|
|
37
|
+
* The `selector` parameter allows you to specify which state changes will trigger a re-render
|
|
38
|
+
* at the hook level, optimizing performance by preventing unnecessary re-renders when irrelevant
|
|
39
|
+
* state changes occur.
|
|
29
40
|
*
|
|
30
41
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
31
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
32
|
-
* full control over when your component updates.
|
|
33
|
-
* component re-render when the selected state values change.
|
|
42
|
+
* tracking by providing a selector function or using the `Subscribe` HOC. This prevents unnecessary
|
|
43
|
+
* re-renders and gives you full control over when your component updates.
|
|
34
44
|
*
|
|
35
45
|
* Available state properties:
|
|
36
46
|
* - `executionCount`: Number of items that have been processed by the queuer
|
|
@@ -54,7 +64,14 @@ let __tanstack_pacer_queuer = require("@tanstack/pacer/queuer");
|
|
|
54
64
|
* { started: true, wait: 1000 }
|
|
55
65
|
* );
|
|
56
66
|
*
|
|
57
|
-
* //
|
|
67
|
+
* // Subscribe to state changes deep in component tree using Subscribe HOC
|
|
68
|
+
* <queue.Subscribe selector={(state) => ({ size: state.size })}>
|
|
69
|
+
* {({ size }) => (
|
|
70
|
+
* <div>Queue Size: {size}</div>
|
|
71
|
+
* )}
|
|
72
|
+
* </queue.Subscribe>
|
|
73
|
+
*
|
|
74
|
+
* // Opt-in to re-render when queue size changes at hook level (optimized for displaying queue length)
|
|
58
75
|
* const queue = useQueuer(
|
|
59
76
|
* (item) => console.log('Processing:', item),
|
|
60
77
|
* { started: true, wait: 1000 },
|
|
@@ -118,10 +135,17 @@ function useQueuer(fn, options = {}, selector = () => ({})) {
|
|
|
118
135
|
...require_PacerProvider.useDefaultPacerOptions().queuer,
|
|
119
136
|
...options
|
|
120
137
|
};
|
|
121
|
-
const [queuer] = (0, preact_hooks.useState)(() =>
|
|
138
|
+
const [queuer] = (0, preact_hooks.useState)(() => {
|
|
139
|
+
const queuerInstance = new _tanstack_pacer_queuer.Queuer(fn, mergedOptions);
|
|
140
|
+
queuerInstance.Subscribe = function Subscribe(props) {
|
|
141
|
+
const selected = (0, _tanstack_preact_store.useStore)(queuerInstance.store, props.selector);
|
|
142
|
+
return typeof props.children === "function" ? props.children(selected) : props.children;
|
|
143
|
+
};
|
|
144
|
+
return queuerInstance;
|
|
145
|
+
});
|
|
122
146
|
queuer.fn = fn;
|
|
123
147
|
queuer.setOptions(mergedOptions);
|
|
124
|
-
const state = (0,
|
|
148
|
+
const state = (0, _tanstack_preact_store.useStore)(queuer.store, selector);
|
|
125
149
|
return (0, preact_hooks.useMemo)(() => ({
|
|
126
150
|
...queuer,
|
|
127
151
|
state
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQueuer.cjs","names":["useDefaultPacerOptions","Queuer"],"sources":["../../src/queuer/useQueuer.ts"],"sourcesContent":["import { useMemo, useState } from 'preact/hooks'\nimport { Queuer } from '@tanstack/pacer/queuer'\nimport { useStore } from '@tanstack/preact-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/preact-store'\nimport type { QueuerOptions, QueuerState } from '@tanstack/pacer/queuer'\n\nexport interface PreactQueuer<TValue, TSelected = {}> extends Omit<\n Queuer<TValue>,\n 'store'\n> {\n /**\n * Reactive state that will be updated and re-rendered when the queuer state changes\n *\n * Use this instead of `queuer.store.state`\n */\n readonly state: Readonly<TSelected>\n /**\n * @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.\n * The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<QueuerState<TValue>>>\n}\n\n/**\n * A Preact hook that creates and manages a Queuer instance.\n *\n * This is a lower-level hook that provides direct access to the Queuer's functionality without\n * any built-in state management. This allows you to integrate it with any state management solution\n * you prefer (useState, Redux, Zustand, etc.) by utilizing the onItemsChange callback.\n *\n * For a hook with built-in state management, see useQueuedState.\n *\n * The Queuer extends the base Queue to add processing capabilities. Items are processed\n * synchronously in order, with optional delays between processing each item. The queuer includes\n * an internal tick mechanism that can be started and stopped, making it useful as a scheduler.\n * When started, it will process one item per tick, with an optional wait time between ticks.\n *\n * By default uses FIFO (First In First Out) behavior, but can be configured for LIFO\n * (Last In First Out) by specifying 'front' position when adding items.\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
1
|
+
{"version":3,"file":"useQueuer.cjs","names":["useDefaultPacerOptions","Queuer"],"sources":["../../src/queuer/useQueuer.ts"],"sourcesContent":["import { useMemo, useState } from 'preact/hooks'\nimport { Queuer } from '@tanstack/pacer/queuer'\nimport { useStore } from '@tanstack/preact-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/preact-store'\nimport type { QueuerOptions, QueuerState } from '@tanstack/pacer/queuer'\nimport type { ComponentChildren } from 'preact'\n\nexport interface PreactQueuer<TValue, TSelected = {}> extends Omit<\n Queuer<TValue>,\n 'store'\n> {\n /**\n * A Preact HOC (Higher Order Component) that allows you to subscribe to the queuer state.\n *\n * This is useful for opting into state re-renders for specific parts of the queuer state\n * deep in your component tree without needing to pass a selector to the hook.\n *\n * @example\n * <queuer.Subscribe selector={(state) => ({ size: state.size })}>\n * {({ size }) => (\n * <div>Queue Size: {size}</div>\n * )}\n * </queuer.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: QueuerState<TValue>) => TSelected\n children: ((state: TSelected) => ComponentChildren) | ComponentChildren\n }) => ComponentChildren\n /**\n * Reactive state that will be updated and re-rendered when the queuer state changes\n *\n * Use this instead of `queuer.store.state`\n */\n readonly state: Readonly<TSelected>\n /**\n * @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.\n * The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<QueuerState<TValue>>>\n}\n\n/**\n * A Preact hook that creates and manages a Queuer instance.\n *\n * This is a lower-level hook that provides direct access to the Queuer's functionality without\n * any built-in state management. This allows you to integrate it with any state management solution\n * you prefer (useState, Redux, Zustand, etc.) by utilizing the onItemsChange callback.\n *\n * For a hook with built-in state management, see useQueuedState.\n *\n * The Queuer extends the base Queue to add processing capabilities. Items are processed\n * synchronously in order, with optional delays between processing each item. The queuer includes\n * an internal tick mechanism that can be started and stopped, making it useful as a scheduler.\n * When started, it will process one item per tick, with an optional wait time between ticks.\n *\n * By default uses FIFO (First In First Out) behavior, but can be configured for LIFO\n * (Last In First Out) by specifying 'front' position when adding items.\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. You can subscribe to state changes\n * in two ways:\n *\n * **1. Using `queuer.Subscribe` HOC (Recommended for component tree subscriptions)**\n *\n * Use the `Subscribe` HOC 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 a re-render\n * at the hook level, optimizing performance by preventing unnecessary re-renders 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` HOC. This prevents unnecessary\n * re-renders and gives you full control over when your component updates.\n *\n * Available state properties:\n * - `executionCount`: Number of items that have been processed by the queuer\n * - `expirationCount`: Number of items that have been removed due to expiration\n * - `isEmpty`: Whether the queuer has no items to process\n * - `isFull`: Whether the queuer has reached its maximum capacity\n * - `isIdle`: Whether the queuer is not currently processing any items\n * - `isRunning`: Whether the queuer is active and will process items automatically\n * - `items`: Array of items currently waiting to be processed\n * - `itemTimestamps`: Timestamps when items were added for expiration tracking\n * - `pendingTick`: Whether the queuer has a pending timeout for processing the next item\n * - `rejectionCount`: Number of items that have been rejected from being added\n * - `size`: Number of items currently in the queue\n * - `status`: Current processing status ('idle' | 'running' | 'stopped')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const queue = useQueuer(\n * (item) => console.log('Processing:', item),\n * { started: true, wait: 1000 }\n * );\n *\n * // Subscribe to state changes deep in component tree using Subscribe HOC\n * <queue.Subscribe selector={(state) => ({ size: state.size })}>\n * {({ size }) => (\n * <div>Queue Size: {size}</div>\n * )}\n * </queue.Subscribe>\n *\n * // Opt-in to re-render when queue size changes at hook level (optimized for displaying queue length)\n * const queue = useQueuer(\n * (item) => console.log('Processing:', item),\n * { started: true, wait: 1000 },\n * (state) => ({\n * size: state.size,\n * isEmpty: state.isEmpty,\n * isFull: state.isFull\n * })\n * );\n *\n * // Opt-in to re-render when processing state changes (optimized for loading indicators)\n * const queue = useQueuer(\n * (item) => console.log('Processing:', item),\n * { started: true, wait: 1000 },\n * (state) => ({\n * isRunning: state.isRunning,\n * isIdle: state.isIdle,\n * status: state.status,\n * pendingTick: state.pendingTick\n * })\n * );\n *\n * // Opt-in to re-render when execution metrics change (optimized for stats display)\n * const queue = useQueuer(\n * (item) => console.log('Processing:', item),\n * { started: true, wait: 1000 },\n * (state) => ({\n * executionCount: state.executionCount,\n * expirationCount: state.expirationCount,\n * rejectionCount: state.rejectionCount\n * })\n * );\n *\n * // Example with custom state management and scheduling\n * const [items, setItems] = useState([]);\n *\n * const queue = useQueuer(\n * (item) => console.log('Processing:', item),\n * {\n * started: true, // Start processing immediately\n * wait: 1000, // Process one item every second\n * onItemsChange: (queue) => setItems(queue.peekAllItems()),\n * getPriority: (item) => item.priority // Process higher priority items first\n * }\n * );\n *\n * // Add items to process - they'll be handled automatically\n * queue.addItem('task1');\n * queue.addItem('task2');\n *\n * // Control the scheduler\n * queue.stop(); // Pause processing\n * queue.start(); // Resume processing\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { size, isRunning, executionCount } = queue.state;\n * ```\n */\nexport function useQueuer<TValue, TSelected = {}>(\n fn: (item: TValue) => void,\n options: QueuerOptions<TValue> = {},\n selector: (state: QueuerState<TValue>) => TSelected = () => ({}) as TSelected,\n): PreactQueuer<TValue, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().queuer,\n ...options,\n } as QueuerOptions<TValue>\n\n const [queuer] = useState(() => {\n const queuerInstance = new Queuer<TValue>(\n fn,\n mergedOptions,\n ) as unknown as PreactQueuer<TValue, TSelected>\n\n queuerInstance.Subscribe = function Subscribe<TSelected>(props: {\n selector: (state: QueuerState<TValue>) => TSelected\n children: ((state: TSelected) => ComponentChildren) | ComponentChildren\n }) {\n const selected = useStore(queuerInstance.store, props.selector)\n\n return typeof props.children === 'function'\n ? props.children(selected)\n : props.children\n }\n\n return queuerInstance\n })\n\n queuer.fn = fn\n queuer.setOptions(mergedOptions)\n\n const state = useStore(queuer.store, selector)\n\n return useMemo(\n () =>\n ({\n ...queuer,\n state,\n }) as PreactQueuer<TValue, TSelected>, // omit `store` in favor of `state`\n [queuer, state],\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyKA,SAAgB,UACd,IACA,UAAiC,EAAE,EACnC,kBAA6D,EAAE,GAC9B;CACjC,MAAM,gBAAgB;EACpB,GAAGA,8CAAwB,CAAC;EAC5B,GAAG;EACJ;CAED,MAAM,CAAC,2CAAyB;EAC9B,MAAM,iBAAiB,IAAIC,8BACzB,IACA,cACD;AAED,iBAAe,YAAY,SAAS,UAAqB,OAGtD;GACD,MAAM,gDAAoB,eAAe,OAAO,MAAM,SAAS;AAE/D,UAAO,OAAO,MAAM,aAAa,aAC7B,MAAM,SAAS,SAAS,GACxB,MAAM;;AAGZ,SAAO;GACP;AAEF,QAAO,KAAK;AACZ,QAAO,WAAW,cAAc;CAEhC,MAAM,6CAAiB,OAAO,OAAO,SAAS;AAE9C,yCAEK;EACC,GAAG;EACH;EACD,GACH,CAAC,QAAQ,MAAM,CAChB"}
|
|
@@ -1,8 +1,26 @@
|
|
|
1
1
|
import { Store } from "@tanstack/preact-store";
|
|
2
|
+
import { ComponentChildren } from "preact";
|
|
2
3
|
import { Queuer, QueuerOptions, QueuerState } from "@tanstack/pacer/queuer";
|
|
3
4
|
|
|
4
5
|
//#region src/queuer/useQueuer.d.ts
|
|
5
6
|
interface PreactQueuer<TValue, TSelected = {}> extends Omit<Queuer<TValue>, 'store'> {
|
|
7
|
+
/**
|
|
8
|
+
* A Preact HOC (Higher Order Component) that allows you to subscribe to the queuer state.
|
|
9
|
+
*
|
|
10
|
+
* This is useful for opting into state re-renders for specific parts of the queuer state
|
|
11
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* <queuer.Subscribe selector={(state) => ({ size: state.size })}>
|
|
15
|
+
* {({ size }) => (
|
|
16
|
+
* <div>Queue Size: {size}</div>
|
|
17
|
+
* )}
|
|
18
|
+
* </queuer.Subscribe>
|
|
19
|
+
*/
|
|
20
|
+
Subscribe: <TSelected>(props: {
|
|
21
|
+
selector: (state: QueuerState<TValue>) => TSelected;
|
|
22
|
+
children: ((state: TSelected) => ComponentChildren) | ComponentChildren;
|
|
23
|
+
}) => ComponentChildren;
|
|
6
24
|
/**
|
|
7
25
|
* Reactive state that will be updated and re-rendered when the queuer state changes
|
|
8
26
|
*
|
|
@@ -35,14 +53,24 @@ interface PreactQueuer<TValue, TSelected = {}> extends Omit<Queuer<TValue>, 'sto
|
|
|
35
53
|
*
|
|
36
54
|
* ## State Management and Selector
|
|
37
55
|
*
|
|
38
|
-
* The hook uses TanStack Store for reactive state management.
|
|
39
|
-
*
|
|
40
|
-
*
|
|
56
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
57
|
+
* in two ways:
|
|
58
|
+
*
|
|
59
|
+
* **1. Using `queuer.Subscribe` HOC (Recommended for component tree subscriptions)**
|
|
60
|
+
*
|
|
61
|
+
* Use the `Subscribe` HOC to subscribe to state changes deep in your component tree without
|
|
62
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
63
|
+
* in child components.
|
|
64
|
+
*
|
|
65
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
66
|
+
*
|
|
67
|
+
* The `selector` parameter allows you to specify which state changes will trigger a re-render
|
|
68
|
+
* at the hook level, optimizing performance by preventing unnecessary re-renders when irrelevant
|
|
69
|
+
* state changes occur.
|
|
41
70
|
*
|
|
42
71
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
43
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
44
|
-
* full control over when your component updates.
|
|
45
|
-
* component re-render when the selected state values change.
|
|
72
|
+
* tracking by providing a selector function or using the `Subscribe` HOC. This prevents unnecessary
|
|
73
|
+
* re-renders and gives you full control over when your component updates.
|
|
46
74
|
*
|
|
47
75
|
* Available state properties:
|
|
48
76
|
* - `executionCount`: Number of items that have been processed by the queuer
|
|
@@ -66,7 +94,14 @@ interface PreactQueuer<TValue, TSelected = {}> extends Omit<Queuer<TValue>, 'sto
|
|
|
66
94
|
* { started: true, wait: 1000 }
|
|
67
95
|
* );
|
|
68
96
|
*
|
|
69
|
-
* //
|
|
97
|
+
* // Subscribe to state changes deep in component tree using Subscribe HOC
|
|
98
|
+
* <queue.Subscribe selector={(state) => ({ size: state.size })}>
|
|
99
|
+
* {({ size }) => (
|
|
100
|
+
* <div>Queue Size: {size}</div>
|
|
101
|
+
* )}
|
|
102
|
+
* </queue.Subscribe>
|
|
103
|
+
*
|
|
104
|
+
* // Opt-in to re-render when queue size changes at hook level (optimized for displaying queue length)
|
|
70
105
|
* const queue = useQueuer(
|
|
71
106
|
* (item) => console.log('Processing:', item),
|
|
72
107
|
* { started: true, wait: 1000 },
|
|
@@ -1,8 +1,26 @@
|
|
|
1
|
+
import { ComponentChildren } from "preact";
|
|
1
2
|
import { Store } from "@tanstack/preact-store";
|
|
2
3
|
import { Queuer, QueuerOptions, QueuerState } from "@tanstack/pacer/queuer";
|
|
3
4
|
|
|
4
5
|
//#region src/queuer/useQueuer.d.ts
|
|
5
6
|
interface PreactQueuer<TValue, TSelected = {}> extends Omit<Queuer<TValue>, 'store'> {
|
|
7
|
+
/**
|
|
8
|
+
* A Preact HOC (Higher Order Component) that allows you to subscribe to the queuer state.
|
|
9
|
+
*
|
|
10
|
+
* This is useful for opting into state re-renders for specific parts of the queuer state
|
|
11
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* <queuer.Subscribe selector={(state) => ({ size: state.size })}>
|
|
15
|
+
* {({ size }) => (
|
|
16
|
+
* <div>Queue Size: {size}</div>
|
|
17
|
+
* )}
|
|
18
|
+
* </queuer.Subscribe>
|
|
19
|
+
*/
|
|
20
|
+
Subscribe: <TSelected>(props: {
|
|
21
|
+
selector: (state: QueuerState<TValue>) => TSelected;
|
|
22
|
+
children: ((state: TSelected) => ComponentChildren) | ComponentChildren;
|
|
23
|
+
}) => ComponentChildren;
|
|
6
24
|
/**
|
|
7
25
|
* Reactive state that will be updated and re-rendered when the queuer state changes
|
|
8
26
|
*
|
|
@@ -35,14 +53,24 @@ interface PreactQueuer<TValue, TSelected = {}> extends Omit<Queuer<TValue>, 'sto
|
|
|
35
53
|
*
|
|
36
54
|
* ## State Management and Selector
|
|
37
55
|
*
|
|
38
|
-
* The hook uses TanStack Store for reactive state management.
|
|
39
|
-
*
|
|
40
|
-
*
|
|
56
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
57
|
+
* in two ways:
|
|
58
|
+
*
|
|
59
|
+
* **1. Using `queuer.Subscribe` HOC (Recommended for component tree subscriptions)**
|
|
60
|
+
*
|
|
61
|
+
* Use the `Subscribe` HOC to subscribe to state changes deep in your component tree without
|
|
62
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
63
|
+
* in child components.
|
|
64
|
+
*
|
|
65
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
66
|
+
*
|
|
67
|
+
* The `selector` parameter allows you to specify which state changes will trigger a re-render
|
|
68
|
+
* at the hook level, optimizing performance by preventing unnecessary re-renders when irrelevant
|
|
69
|
+
* state changes occur.
|
|
41
70
|
*
|
|
42
71
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
43
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
44
|
-
* full control over when your component updates.
|
|
45
|
-
* component re-render when the selected state values change.
|
|
72
|
+
* tracking by providing a selector function or using the `Subscribe` HOC. This prevents unnecessary
|
|
73
|
+
* re-renders and gives you full control over when your component updates.
|
|
46
74
|
*
|
|
47
75
|
* Available state properties:
|
|
48
76
|
* - `executionCount`: Number of items that have been processed by the queuer
|
|
@@ -66,7 +94,14 @@ interface PreactQueuer<TValue, TSelected = {}> extends Omit<Queuer<TValue>, 'sto
|
|
|
66
94
|
* { started: true, wait: 1000 }
|
|
67
95
|
* );
|
|
68
96
|
*
|
|
69
|
-
* //
|
|
97
|
+
* // Subscribe to state changes deep in component tree using Subscribe HOC
|
|
98
|
+
* <queue.Subscribe selector={(state) => ({ size: state.size })}>
|
|
99
|
+
* {({ size }) => (
|
|
100
|
+
* <div>Queue Size: {size}</div>
|
|
101
|
+
* )}
|
|
102
|
+
* </queue.Subscribe>
|
|
103
|
+
*
|
|
104
|
+
* // Opt-in to re-render when queue size changes at hook level (optimized for displaying queue length)
|
|
70
105
|
* const queue = useQueuer(
|
|
71
106
|
* (item) => console.log('Processing:', item),
|
|
72
107
|
* { started: true, wait: 1000 },
|
package/dist/queuer/useQueuer.js
CHANGED
|
@@ -23,14 +23,24 @@ import { Queuer } from "@tanstack/pacer/queuer";
|
|
|
23
23
|
*
|
|
24
24
|
* ## State Management and Selector
|
|
25
25
|
*
|
|
26
|
-
* The hook uses TanStack Store for reactive state management.
|
|
27
|
-
*
|
|
28
|
-
*
|
|
26
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
27
|
+
* in two ways:
|
|
28
|
+
*
|
|
29
|
+
* **1. Using `queuer.Subscribe` HOC (Recommended for component tree subscriptions)**
|
|
30
|
+
*
|
|
31
|
+
* Use the `Subscribe` HOC to subscribe to state changes deep in your component tree without
|
|
32
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
33
|
+
* in child components.
|
|
34
|
+
*
|
|
35
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
36
|
+
*
|
|
37
|
+
* The `selector` parameter allows you to specify which state changes will trigger a re-render
|
|
38
|
+
* at the hook level, optimizing performance by preventing unnecessary re-renders when irrelevant
|
|
39
|
+
* state changes occur.
|
|
29
40
|
*
|
|
30
41
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
31
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
32
|
-
* full control over when your component updates.
|
|
33
|
-
* component re-render when the selected state values change.
|
|
42
|
+
* tracking by providing a selector function or using the `Subscribe` HOC. This prevents unnecessary
|
|
43
|
+
* re-renders and gives you full control over when your component updates.
|
|
34
44
|
*
|
|
35
45
|
* Available state properties:
|
|
36
46
|
* - `executionCount`: Number of items that have been processed by the queuer
|
|
@@ -54,7 +64,14 @@ import { Queuer } from "@tanstack/pacer/queuer";
|
|
|
54
64
|
* { started: true, wait: 1000 }
|
|
55
65
|
* );
|
|
56
66
|
*
|
|
57
|
-
* //
|
|
67
|
+
* // Subscribe to state changes deep in component tree using Subscribe HOC
|
|
68
|
+
* <queue.Subscribe selector={(state) => ({ size: state.size })}>
|
|
69
|
+
* {({ size }) => (
|
|
70
|
+
* <div>Queue Size: {size}</div>
|
|
71
|
+
* )}
|
|
72
|
+
* </queue.Subscribe>
|
|
73
|
+
*
|
|
74
|
+
* // Opt-in to re-render when queue size changes at hook level (optimized for displaying queue length)
|
|
58
75
|
* const queue = useQueuer(
|
|
59
76
|
* (item) => console.log('Processing:', item),
|
|
60
77
|
* { started: true, wait: 1000 },
|
|
@@ -118,7 +135,14 @@ function useQueuer(fn, options = {}, selector = () => ({})) {
|
|
|
118
135
|
...useDefaultPacerOptions().queuer,
|
|
119
136
|
...options
|
|
120
137
|
};
|
|
121
|
-
const [queuer] = useState(() =>
|
|
138
|
+
const [queuer] = useState(() => {
|
|
139
|
+
const queuerInstance = new Queuer(fn, mergedOptions);
|
|
140
|
+
queuerInstance.Subscribe = function Subscribe(props) {
|
|
141
|
+
const selected = useStore(queuerInstance.store, props.selector);
|
|
142
|
+
return typeof props.children === "function" ? props.children(selected) : props.children;
|
|
143
|
+
};
|
|
144
|
+
return queuerInstance;
|
|
145
|
+
});
|
|
122
146
|
queuer.fn = fn;
|
|
123
147
|
queuer.setOptions(mergedOptions);
|
|
124
148
|
const state = useStore(queuer.store, selector);
|