@tanstack/pacer 0.16.3 → 0.16.4
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/README.md +165 -0
- package/dist/async-batcher.cjs +355 -0
- package/dist/async-batcher.cjs.map +1 -0
- package/dist/async-batcher.d.cts +344 -0
- package/dist/async-batcher.d.ts +344 -0
- package/dist/async-batcher.js +353 -0
- package/dist/async-batcher.js.map +1 -0
- package/dist/async-debouncer.cjs +340 -0
- package/dist/async-debouncer.cjs.map +1 -0
- package/dist/async-debouncer.d.cts +300 -0
- package/dist/async-debouncer.d.ts +300 -0
- package/dist/async-debouncer.js +338 -0
- package/dist/async-debouncer.js.map +1 -0
- package/dist/async-queuer.cjs +496 -0
- package/dist/async-queuer.cjs.map +1 -0
- package/dist/async-queuer.d.cts +440 -0
- package/dist/async-queuer.d.ts +440 -0
- package/dist/async-queuer.js +494 -0
- package/dist/async-queuer.js.map +1 -0
- package/dist/async-rate-limiter.cjs +381 -0
- package/dist/async-rate-limiter.cjs.map +1 -0
- package/dist/{cjs/async-rate-limiter.d.cts → async-rate-limiter.d.cts} +190 -185
- package/dist/{esm/async-rate-limiter.d.ts → async-rate-limiter.d.ts} +190 -185
- package/dist/async-rate-limiter.js +379 -0
- package/dist/async-rate-limiter.js.map +1 -0
- package/dist/async-retryer.cjs +378 -0
- package/dist/async-retryer.cjs.map +1 -0
- package/dist/async-retryer.d.cts +312 -0
- package/dist/async-retryer.d.ts +312 -0
- package/dist/async-retryer.js +376 -0
- package/dist/async-retryer.js.map +1 -0
- package/dist/async-throttler.cjs +362 -0
- package/dist/async-throttler.cjs.map +1 -0
- package/dist/async-throttler.d.cts +320 -0
- package/dist/async-throttler.d.ts +320 -0
- package/dist/async-throttler.js +360 -0
- package/dist/async-throttler.js.map +1 -0
- package/dist/batcher.cjs +194 -0
- package/dist/batcher.cjs.map +1 -0
- package/dist/batcher.d.cts +180 -0
- package/dist/batcher.d.ts +180 -0
- package/dist/batcher.js +193 -0
- package/dist/batcher.js.map +1 -0
- package/dist/debouncer.cjs +197 -0
- package/dist/debouncer.cjs.map +1 -0
- package/dist/debouncer.d.cts +167 -0
- package/dist/debouncer.d.ts +167 -0
- package/dist/debouncer.js +195 -0
- package/dist/debouncer.js.map +1 -0
- package/dist/event-client.cjs +20 -0
- package/dist/event-client.cjs.map +1 -0
- package/dist/event-client.d.cts +49 -0
- package/dist/event-client.d.ts +49 -0
- package/dist/event-client.js +19 -0
- package/dist/event-client.js.map +1 -0
- package/dist/index.cjs +49 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/{esm/index.js → index.js} +5 -41
- package/dist/queuer.cjs +393 -0
- package/dist/queuer.cjs.map +1 -0
- package/dist/queuer.d.cts +345 -0
- package/dist/queuer.d.ts +345 -0
- package/dist/queuer.js +391 -0
- package/dist/queuer.js.map +1 -0
- package/dist/rate-limiter.cjs +251 -0
- package/dist/rate-limiter.cjs.map +1 -0
- package/dist/{cjs/rate-limiter.d.cts → rate-limiter.d.cts} +113 -108
- package/dist/{esm/rate-limiter.d.ts → rate-limiter.d.ts} +113 -108
- package/dist/rate-limiter.js +249 -0
- package/dist/rate-limiter.js.map +1 -0
- package/dist/throttler.cjs +209 -0
- package/dist/throttler.cjs.map +1 -0
- package/dist/throttler.d.cts +207 -0
- package/dist/throttler.d.ts +207 -0
- package/dist/throttler.js +207 -0
- package/dist/throttler.js.map +1 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.cts +13 -0
- package/dist/types.d.ts +13 -0
- package/dist/types.js +1 -0
- package/dist/utils.cjs +13 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +8 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.js +11 -0
- package/dist/utils.js.map +1 -0
- package/package.json +38 -122
- package/dist/cjs/async-batcher.cjs +0 -222
- package/dist/cjs/async-batcher.cjs.map +0 -1
- package/dist/cjs/async-batcher.d.cts +0 -340
- package/dist/cjs/async-debouncer.cjs +0 -233
- package/dist/cjs/async-debouncer.cjs.map +0 -1
- package/dist/cjs/async-debouncer.d.cts +0 -295
- package/dist/cjs/async-queuer.cjs +0 -399
- package/dist/cjs/async-queuer.cjs.map +0 -1
- package/dist/cjs/async-queuer.d.cts +0 -435
- package/dist/cjs/async-rate-limiter.cjs +0 -254
- package/dist/cjs/async-rate-limiter.cjs.map +0 -1
- package/dist/cjs/async-retryer.cjs +0 -287
- package/dist/cjs/async-retryer.cjs.map +0 -1
- package/dist/cjs/async-retryer.d.cts +0 -308
- package/dist/cjs/async-throttler.cjs +0 -263
- package/dist/cjs/async-throttler.cjs.map +0 -1
- package/dist/cjs/async-throttler.d.cts +0 -315
- package/dist/cjs/batcher.cjs +0 -132
- package/dist/cjs/batcher.cjs.map +0 -1
- package/dist/cjs/batcher.d.cts +0 -176
- package/dist/cjs/debouncer.cjs +0 -139
- package/dist/cjs/debouncer.cjs.map +0 -1
- package/dist/cjs/debouncer.d.cts +0 -162
- package/dist/cjs/event-client.cjs +0 -20
- package/dist/cjs/event-client.cjs.map +0 -1
- package/dist/cjs/event-client.d.cts +0 -45
- package/dist/cjs/index.cjs +0 -51
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/index.d.cts +0 -15
- package/dist/cjs/queuer.cjs +0 -308
- package/dist/cjs/queuer.cjs.map +0 -1
- package/dist/cjs/queuer.d.cts +0 -340
- package/dist/cjs/rate-limiter.cjs +0 -181
- package/dist/cjs/rate-limiter.cjs.map +0 -1
- package/dist/cjs/throttler.cjs +0 -153
- package/dist/cjs/throttler.cjs.map +0 -1
- package/dist/cjs/throttler.d.cts +0 -202
- package/dist/cjs/types.d.cts +0 -9
- package/dist/cjs/utils.cjs +0 -11
- package/dist/cjs/utils.cjs.map +0 -1
- package/dist/cjs/utils.d.cts +0 -3
- package/dist/esm/async-batcher.d.ts +0 -340
- package/dist/esm/async-batcher.js +0 -222
- package/dist/esm/async-batcher.js.map +0 -1
- package/dist/esm/async-debouncer.d.ts +0 -295
- package/dist/esm/async-debouncer.js +0 -233
- package/dist/esm/async-debouncer.js.map +0 -1
- package/dist/esm/async-queuer.d.ts +0 -435
- package/dist/esm/async-queuer.js +0 -399
- package/dist/esm/async-queuer.js.map +0 -1
- package/dist/esm/async-rate-limiter.js +0 -254
- package/dist/esm/async-rate-limiter.js.map +0 -1
- package/dist/esm/async-retryer.d.ts +0 -308
- package/dist/esm/async-retryer.js +0 -287
- package/dist/esm/async-retryer.js.map +0 -1
- package/dist/esm/async-throttler.d.ts +0 -315
- package/dist/esm/async-throttler.js +0 -263
- package/dist/esm/async-throttler.js.map +0 -1
- package/dist/esm/batcher.d.ts +0 -176
- package/dist/esm/batcher.js +0 -132
- package/dist/esm/batcher.js.map +0 -1
- package/dist/esm/debouncer.d.ts +0 -162
- package/dist/esm/debouncer.js +0 -139
- package/dist/esm/debouncer.js.map +0 -1
- package/dist/esm/event-client.d.ts +0 -45
- package/dist/esm/event-client.js +0 -20
- package/dist/esm/event-client.js.map +0 -1
- package/dist/esm/index.d.ts +0 -15
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/queuer.d.ts +0 -340
- package/dist/esm/queuer.js +0 -308
- package/dist/esm/queuer.js.map +0 -1
- package/dist/esm/rate-limiter.js +0 -181
- package/dist/esm/rate-limiter.js.map +0 -1
- package/dist/esm/throttler.d.ts +0 -202
- package/dist/esm/throttler.js +0 -153
- package/dist/esm/throttler.js.map +0 -1
- package/dist/esm/types.d.ts +0 -9
- package/dist/esm/utils.d.ts +0 -3
- package/dist/esm/utils.js +0 -11
- package/dist/esm/utils.js.map +0 -1
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { parseFunctionOrValue } from "./utils.js";
|
|
2
|
+
import { emitChange, pacerEventClient } from "./event-client.js";
|
|
3
|
+
import { Store } from "@tanstack/store";
|
|
4
|
+
|
|
5
|
+
//#region src/debouncer.ts
|
|
6
|
+
function getDefaultDebouncerState() {
|
|
7
|
+
return {
|
|
8
|
+
canLeadingExecute: true,
|
|
9
|
+
executionCount: 0,
|
|
10
|
+
isPending: false,
|
|
11
|
+
lastArgs: void 0,
|
|
12
|
+
status: "idle",
|
|
13
|
+
maybeExecuteCount: 0
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Utility function for sharing common `DebouncerOptions` options between different `Debouncer` instances.
|
|
18
|
+
*/
|
|
19
|
+
function debouncerOptions(options) {
|
|
20
|
+
return options;
|
|
21
|
+
}
|
|
22
|
+
const defaultOptions = {
|
|
23
|
+
enabled: true,
|
|
24
|
+
leading: false,
|
|
25
|
+
trailing: true,
|
|
26
|
+
wait: 0
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* A class that creates a debounced function.
|
|
30
|
+
*
|
|
31
|
+
* Debouncing ensures that a function is only executed after a certain amount of time has passed
|
|
32
|
+
* since its last invocation. This is useful for handling frequent events like window resizing,
|
|
33
|
+
* scroll events, or input changes where you want to limit the rate of execution.
|
|
34
|
+
* This synchronous version is lighter weight and often all you need - upgrade to AsyncDebouncer when you need promises, retry support, abort/cancel capabilities, or advanced error handling.
|
|
35
|
+
*
|
|
36
|
+
* The debounced function can be configured to execute either at the start of the delay period
|
|
37
|
+
* (leading edge) or at the end (trailing edge, default). Each new call during the wait period
|
|
38
|
+
* will reset the timer.
|
|
39
|
+
*
|
|
40
|
+
* State Management:
|
|
41
|
+
* - Uses TanStack Store for reactive state management
|
|
42
|
+
* - Use `initialState` to provide initial state values when creating the debouncer
|
|
43
|
+
* - Use `onExecute` callback to react to function execution and implement custom logic
|
|
44
|
+
* - The state includes canLeadingExecute, execution count, and isPending status
|
|
45
|
+
* - State can be accessed via `debouncer.store.state` when using the class directly
|
|
46
|
+
* - When using framework adapters (React/Solid), state is accessed from `debouncer.state`
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const debouncer = new Debouncer((value: string) => {
|
|
51
|
+
* saveToDatabase(value);
|
|
52
|
+
* }, { wait: 500 });
|
|
53
|
+
*
|
|
54
|
+
* // Will only save after 500ms of no new input
|
|
55
|
+
* inputElement.addEventListener('input', () => {
|
|
56
|
+
* debouncer.maybeExecute(inputElement.value);
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
var Debouncer = class {
|
|
61
|
+
#timeoutId;
|
|
62
|
+
constructor(fn, initialOptions) {
|
|
63
|
+
this.fn = fn;
|
|
64
|
+
this.store = new Store(getDefaultDebouncerState());
|
|
65
|
+
this.setOptions = (newOptions) => {
|
|
66
|
+
this.options = {
|
|
67
|
+
...this.options,
|
|
68
|
+
...newOptions
|
|
69
|
+
};
|
|
70
|
+
if (!this.#getEnabled()) this.cancel();
|
|
71
|
+
};
|
|
72
|
+
this.maybeExecute = (...args) => {
|
|
73
|
+
if (!this.#getEnabled()) return void 0;
|
|
74
|
+
this.#setState({ maybeExecuteCount: this.store.state.maybeExecuteCount + 1 });
|
|
75
|
+
let _didLeadingExecute = false;
|
|
76
|
+
if (this.options.leading && this.store.state.canLeadingExecute) {
|
|
77
|
+
this.#setState({ canLeadingExecute: false });
|
|
78
|
+
_didLeadingExecute = true;
|
|
79
|
+
this.#execute(...args);
|
|
80
|
+
}
|
|
81
|
+
if (this.options.trailing) this.#setState({
|
|
82
|
+
isPending: true,
|
|
83
|
+
lastArgs: args
|
|
84
|
+
});
|
|
85
|
+
if (this.#timeoutId) clearTimeout(this.#timeoutId);
|
|
86
|
+
this.#timeoutId = setTimeout(() => {
|
|
87
|
+
this.#setState({ canLeadingExecute: true });
|
|
88
|
+
if (this.options.trailing && !_didLeadingExecute) this.#execute(...args);
|
|
89
|
+
}, this.#getWait());
|
|
90
|
+
};
|
|
91
|
+
this.flush = () => {
|
|
92
|
+
if (this.store.state.isPending && this.store.state.lastArgs) {
|
|
93
|
+
this.#clearTimeout();
|
|
94
|
+
this.#execute(...this.store.state.lastArgs);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
this.cancel = () => {
|
|
98
|
+
this.#clearTimeout();
|
|
99
|
+
this.#setState({
|
|
100
|
+
canLeadingExecute: true,
|
|
101
|
+
isPending: false
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
this.reset = () => {
|
|
105
|
+
this.#setState(getDefaultDebouncerState());
|
|
106
|
+
};
|
|
107
|
+
this.key = initialOptions.key;
|
|
108
|
+
this.options = {
|
|
109
|
+
...defaultOptions,
|
|
110
|
+
...initialOptions
|
|
111
|
+
};
|
|
112
|
+
this.#setState(this.options.initialState ?? {});
|
|
113
|
+
if (this.key) pacerEventClient.on("d-Debouncer", (event) => {
|
|
114
|
+
if (event.payload.key !== this.key) return;
|
|
115
|
+
this.#setState(event.payload.store.state);
|
|
116
|
+
this.setOptions(event.payload.options);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
#setState = (newState) => {
|
|
120
|
+
this.store.setState((state) => {
|
|
121
|
+
const combinedState = {
|
|
122
|
+
...state,
|
|
123
|
+
...newState
|
|
124
|
+
};
|
|
125
|
+
const { isPending } = combinedState;
|
|
126
|
+
return {
|
|
127
|
+
...combinedState,
|
|
128
|
+
status: !this.#getEnabled() ? "disabled" : isPending ? "pending" : "idle"
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
emitChange("Debouncer", this);
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Returns the current enabled state of the debouncer
|
|
135
|
+
*/
|
|
136
|
+
#getEnabled = () => {
|
|
137
|
+
return !!parseFunctionOrValue(this.options.enabled, this);
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Returns the current wait time in milliseconds
|
|
141
|
+
*/
|
|
142
|
+
#getWait = () => {
|
|
143
|
+
return parseFunctionOrValue(this.options.wait, this);
|
|
144
|
+
};
|
|
145
|
+
#execute = (...args) => {
|
|
146
|
+
if (!this.#getEnabled()) return void 0;
|
|
147
|
+
this.fn(...args);
|
|
148
|
+
this.#setState({
|
|
149
|
+
executionCount: this.store.state.executionCount + 1,
|
|
150
|
+
isPending: false,
|
|
151
|
+
lastArgs: void 0
|
|
152
|
+
});
|
|
153
|
+
this.options.onExecute?.(args, this);
|
|
154
|
+
};
|
|
155
|
+
#clearTimeout = () => {
|
|
156
|
+
if (this.#timeoutId) {
|
|
157
|
+
clearTimeout(this.#timeoutId);
|
|
158
|
+
this.#timeoutId = void 0;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* Creates a debounced function that delays invoking the provided function until after a specified wait time.
|
|
164
|
+
* Multiple calls during the wait period will cancel previous pending invocations and reset the timer.
|
|
165
|
+
*
|
|
166
|
+
* This synchronous version is lighter weight and often all you need - upgrade to asyncDebounce when you need promises, retry support, abort/cancel capabilities, or advanced error handling.
|
|
167
|
+
*
|
|
168
|
+
* If leading option is true, the function will execute immediately on the first call, then wait the delay
|
|
169
|
+
* before allowing another execution.
|
|
170
|
+
*
|
|
171
|
+
* State Management:
|
|
172
|
+
* - Uses TanStack Store for reactive state management
|
|
173
|
+
* - Use `initialState` to provide initial state values when creating the debouncer
|
|
174
|
+
* - Use `onExecute` callback to react to function execution and implement custom logic
|
|
175
|
+
* - The state includes canLeadingExecute, execution count, and isPending status
|
|
176
|
+
* - State can be accessed via the underlying Debouncer instance's `store.state` property
|
|
177
|
+
* - When using framework adapters (React/Solid), state is accessed from the hook's state property
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* const debounced = debounce(() => {
|
|
182
|
+
* saveChanges();
|
|
183
|
+
* }, { wait: 1000 });
|
|
184
|
+
*
|
|
185
|
+
* // Called repeatedly but executes at most once per second
|
|
186
|
+
* inputElement.addEventListener('input', debounced);
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
function debounce(fn, initialOptions) {
|
|
190
|
+
return new Debouncer(fn, initialOptions).maybeExecute;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
//#endregion
|
|
194
|
+
export { Debouncer, debounce, debouncerOptions };
|
|
195
|
+
//# sourceMappingURL=debouncer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debouncer.js","names":["defaultOptions: Omit<\n Required<DebouncerOptions<any>>,\n 'initialState' | 'onExecute' | 'key'\n>","fn: TFn","#getEnabled","#setState","#execute","#timeoutId","#getWait","#clearTimeout"],"sources":["../src/debouncer.ts"],"sourcesContent":["import { Store } from '@tanstack/store'\nimport { parseFunctionOrValue } from './utils'\nimport { emitChange, pacerEventClient } from './event-client'\nimport type { AnyFunction } from './types'\n\nexport interface DebouncerState<TFn extends AnyFunction> {\n /**\n * Whether the debouncer can execute on the leading edge of the timeout\n */\n canLeadingExecute: boolean\n /**\n * Number of function executions that have been completed\n */\n executionCount: number\n /**\n * Whether the debouncer is waiting for the timeout to trigger execution\n */\n isPending: boolean\n /**\n * The arguments from the most recent call to maybeExecute\n */\n lastArgs: Parameters<TFn> | undefined\n /**\n * Number of times maybeExecute has been called (for reduction calculations)\n */\n maybeExecuteCount: number\n /**\n * Current execution status - 'idle' when not active, 'pending' when waiting for timeout\n */\n status: 'disabled' | 'idle' | 'pending'\n}\n\nfunction getDefaultDebouncerState<\n TFn extends AnyFunction,\n>(): DebouncerState<TFn> {\n return {\n canLeadingExecute: true,\n executionCount: 0,\n isPending: false,\n lastArgs: undefined,\n status: 'idle',\n maybeExecuteCount: 0,\n }\n}\n\n/**\n * Options for configuring a debounced function\n */\nexport interface DebouncerOptions<TFn extends AnyFunction> {\n /**\n * Whether the debouncer is enabled. When disabled, maybeExecute will not trigger any executions.\n * Can be a boolean or a function that returns a boolean.\n * Defaults to true.\n */\n enabled?: boolean | ((debouncer: Debouncer<TFn>) => boolean)\n /**\n * Initial state for the debouncer\n */\n initialState?: Partial<DebouncerState<TFn>>\n /**\n * A key to identify the debouncer.\n * If provided, the debouncer will be identified by this key in the devtools and PacerProvider if applicable.\n */\n key?: string\n /**\n * Whether to execute on the leading edge of the timeout.\n * The first call will execute immediately and the rest will wait the delay.\n * Defaults to false.\n */\n leading?: boolean\n /**\n * Callback function that is called after the function is executed\n */\n onExecute?: (args: Parameters<TFn>, debouncer: Debouncer<TFn>) => void\n /**\n * Whether to execute on the trailing edge of the timeout.\n * Defaults to true.\n */\n trailing?: boolean\n /**\n * Delay in milliseconds before executing the function.\n * Can be a number or a function that returns a number.\n * Defaults to 0ms\n */\n wait: number | ((debouncer: Debouncer<TFn>) => number)\n}\n\n/**\n * Utility function for sharing common `DebouncerOptions` options between different `Debouncer` instances.\n */\nexport function debouncerOptions<\n TFn extends AnyFunction = AnyFunction,\n TOptions extends Partial<DebouncerOptions<TFn>> = Partial<\n DebouncerOptions<TFn>\n >,\n>(options: TOptions): TOptions {\n return options\n}\n\nconst defaultOptions: Omit<\n Required<DebouncerOptions<any>>,\n 'initialState' | 'onExecute' | 'key'\n> = {\n enabled: true,\n leading: false,\n trailing: true,\n wait: 0,\n}\n\n/**\n * A class that creates a debounced function.\n *\n * Debouncing ensures that a function is only executed after a certain amount of time has passed\n * since its last invocation. This is useful for handling frequent events like window resizing,\n * scroll events, or input changes where you want to limit the rate of execution.\n * This synchronous version is lighter weight and often all you need - upgrade to AsyncDebouncer when you need promises, retry support, abort/cancel capabilities, or advanced error handling.\n *\n * The debounced function can be configured to execute either at the start of the delay period\n * (leading edge) or at the end (trailing edge, default). Each new call during the wait period\n * will reset the timer.\n *\n * State Management:\n * - Uses TanStack Store for reactive state management\n * - Use `initialState` to provide initial state values when creating the debouncer\n * - Use `onExecute` callback to react to function execution and implement custom logic\n * - The state includes canLeadingExecute, execution count, and isPending status\n * - State can be accessed via `debouncer.store.state` when using the class directly\n * - When using framework adapters (React/Solid), state is accessed from `debouncer.state`\n *\n * @example\n * ```ts\n * const debouncer = new Debouncer((value: string) => {\n * saveToDatabase(value);\n * }, { wait: 500 });\n *\n * // Will only save after 500ms of no new input\n * inputElement.addEventListener('input', () => {\n * debouncer.maybeExecute(inputElement.value);\n * });\n * ```\n */\nexport class Debouncer<TFn extends AnyFunction> {\n readonly store: Store<Readonly<DebouncerState<TFn>>> = new Store(\n getDefaultDebouncerState<TFn>(),\n )\n key: string | undefined\n options: DebouncerOptions<TFn>\n #timeoutId: NodeJS.Timeout | undefined\n\n constructor(\n public fn: TFn,\n initialOptions: DebouncerOptions<TFn>,\n ) {\n this.key = initialOptions.key\n this.options = {\n ...defaultOptions,\n ...initialOptions,\n }\n this.#setState(this.options.initialState ?? {})\n\n if (this.key) {\n pacerEventClient.on('d-Debouncer', (event) => {\n if (event.payload.key !== this.key) return\n this.#setState(event.payload.store.state as DebouncerState<TFn>)\n this.setOptions(event.payload.options)\n })\n }\n }\n\n /**\n * Updates the debouncer options\n */\n setOptions = (newOptions: Partial<DebouncerOptions<TFn>>): void => {\n this.options = { ...this.options, ...newOptions }\n\n // Cancel pending execution if the debouncer is disabled\n if (!this.#getEnabled()) {\n this.cancel()\n }\n }\n\n #setState = (newState: Partial<DebouncerState<TFn>>): void => {\n this.store.setState((state) => {\n const combinedState = {\n ...state,\n ...newState,\n }\n const { isPending } = combinedState\n return {\n ...combinedState,\n status: !this.#getEnabled()\n ? 'disabled'\n : isPending\n ? 'pending'\n : 'idle',\n }\n })\n emitChange('Debouncer', this)\n }\n\n /**\n * Returns the current enabled state of the debouncer\n */\n #getEnabled = (): boolean => {\n return !!parseFunctionOrValue(this.options.enabled, this)\n }\n\n /**\n * Returns the current wait time in milliseconds\n */\n #getWait = (): number => {\n return parseFunctionOrValue(this.options.wait, this)\n }\n\n /**\n * Attempts to execute the debounced function\n * If a call is already in progress, it will be queued\n */\n maybeExecute = (...args: Parameters<TFn>): void => {\n if (!this.#getEnabled()) return undefined\n\n this.#setState({\n maybeExecuteCount: this.store.state.maybeExecuteCount + 1,\n })\n\n let _didLeadingExecute = false\n\n // Handle leading execution\n if (this.options.leading && this.store.state.canLeadingExecute) {\n this.#setState({ canLeadingExecute: false })\n _didLeadingExecute = true\n this.#execute(...args)\n }\n\n // Start pending state to indicate that the debouncer is waiting for the trailing edge\n if (this.options.trailing) {\n this.#setState({ isPending: true, lastArgs: args })\n }\n\n // Clear any existing timeout\n if (this.#timeoutId) clearTimeout(this.#timeoutId)\n\n // Set new timeout that will reset canLeadingExecute and execute trailing only if enabled and did not execute leading\n this.#timeoutId = setTimeout(() => {\n this.#setState({ canLeadingExecute: true })\n if (this.options.trailing && !_didLeadingExecute) {\n this.#execute(...args)\n }\n }, this.#getWait())\n }\n\n #execute = (...args: Parameters<TFn>): void => {\n if (!this.#getEnabled()) return undefined\n this.fn(...args) // EXECUTE!\n this.#setState({\n executionCount: this.store.state.executionCount + 1,\n isPending: false,\n lastArgs: undefined,\n })\n this.options.onExecute?.(args, this)\n }\n\n /**\n * Processes the current pending execution immediately\n */\n flush = (): void => {\n if (this.store.state.isPending && this.store.state.lastArgs) {\n this.#clearTimeout() // clear any pending timeout\n this.#execute(...this.store.state.lastArgs) // execute immediately\n }\n }\n\n #clearTimeout = (): void => {\n if (this.#timeoutId) {\n clearTimeout(this.#timeoutId)\n this.#timeoutId = undefined\n }\n }\n\n /**\n * Cancels any pending execution\n */\n cancel = (): void => {\n this.#clearTimeout()\n this.#setState({\n canLeadingExecute: true,\n isPending: false,\n })\n }\n\n /**\n * Resets the debouncer state to its default values\n */\n reset = (): void => {\n this.#setState(getDefaultDebouncerState<TFn>())\n }\n}\n\n/**\n * Creates a debounced function that delays invoking the provided function until after a specified wait time.\n * Multiple calls during the wait period will cancel previous pending invocations and reset the timer.\n *\n * This synchronous version is lighter weight and often all you need - upgrade to asyncDebounce when you need promises, retry support, abort/cancel capabilities, or advanced error handling.\n *\n * If leading option is true, the function will execute immediately on the first call, then wait the delay\n * before allowing another execution.\n *\n * State Management:\n * - Uses TanStack Store for reactive state management\n * - Use `initialState` to provide initial state values when creating the debouncer\n * - Use `onExecute` callback to react to function execution and implement custom logic\n * - The state includes canLeadingExecute, execution count, and isPending status\n * - State can be accessed via the underlying Debouncer instance's `store.state` property\n * - When using framework adapters (React/Solid), state is accessed from the hook's state property\n *\n * @example\n * ```ts\n * const debounced = debounce(() => {\n * saveChanges();\n * }, { wait: 1000 });\n *\n * // Called repeatedly but executes at most once per second\n * inputElement.addEventListener('input', debounced);\n * ```\n */\nexport function debounce<TFn extends AnyFunction>(\n fn: TFn,\n initialOptions: DebouncerOptions<TFn>,\n): (...args: Parameters<TFn>) => void {\n const debouncer = new Debouncer(fn, initialOptions)\n return debouncer.maybeExecute\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
let __tanstack_devtools_event_client = require("@tanstack/devtools-event-client");
|
|
2
|
+
|
|
3
|
+
//#region src/event-client.ts
|
|
4
|
+
var PacerEventClient = class extends __tanstack_devtools_event_client.EventClient {
|
|
5
|
+
constructor(props) {
|
|
6
|
+
super({
|
|
7
|
+
pluginId: "pacer",
|
|
8
|
+
debug: props?.debug
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
const emitChange = (event, payload) => {
|
|
13
|
+
if (payload.key) pacerEventClient.emit(event, payload);
|
|
14
|
+
};
|
|
15
|
+
const pacerEventClient = new PacerEventClient();
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
exports.emitChange = emitChange;
|
|
19
|
+
exports.pacerEventClient = pacerEventClient;
|
|
20
|
+
//# sourceMappingURL=event-client.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-client.cjs","names":["EventClient"],"sources":["../src/event-client.ts"],"sourcesContent":["import { EventClient } from '@tanstack/devtools-event-client'\nimport type { AsyncBatcher } from './async-batcher'\nimport type { AsyncDebouncer } from './async-debouncer'\nimport type { AsyncQueuer } from './async-queuer'\nimport type { AsyncRateLimiter } from './async-rate-limiter'\nimport type { AsyncRetryer } from './async-retryer'\nimport type { AsyncThrottler } from './async-throttler'\nimport type { Debouncer } from './debouncer'\nimport type { Batcher } from './batcher'\nimport type { Queuer } from './queuer'\nimport type { RateLimiter } from './rate-limiter'\nimport type { Throttler } from './throttler'\n\nexport interface PacerEventMap {\n 'pacer:d-AsyncBatcher': AsyncBatcher<any>\n 'pacer:d-AsyncDebouncer': AsyncDebouncer<any>\n 'pacer:d-AsyncQueuer': AsyncQueuer<any>\n 'pacer:d-AsyncRateLimiter': AsyncRateLimiter<any>\n 'pacer:d-AsyncRetryer': AsyncRetryer<any>\n 'pacer:d-AsyncThrottler': AsyncThrottler<any>\n 'pacer:d-Batcher': Batcher<any>\n 'pacer:d-Debouncer': Debouncer<any>\n 'pacer:d-Queuer': Queuer<any>\n 'pacer:d-RateLimiter': RateLimiter<any>\n 'pacer:d-Throttler': Throttler<any>\n 'pacer:AsyncBatcher': AsyncBatcher<any>\n 'pacer:AsyncDebouncer': AsyncDebouncer<any>\n 'pacer:AsyncQueuer': AsyncQueuer<any>\n 'pacer:AsyncRateLimiter': AsyncRateLimiter<any>\n 'pacer:AsyncRetryer': AsyncRetryer<any>\n 'pacer:AsyncThrottler': AsyncThrottler<any>\n 'pacer:Batcher': Batcher<any>\n 'pacer:Debouncer': Debouncer<any>\n 'pacer:Queuer': Queuer<any>\n 'pacer:RateLimiter': RateLimiter<any>\n 'pacer:Throttler': Throttler<any>\n}\n\nexport type PacerEventName = keyof PacerEventMap extends `pacer:${infer T}`\n ? T\n : never\n\nclass PacerEventClient extends EventClient<PacerEventMap> {\n constructor(props?: { debug: boolean }) {\n super({\n pluginId: 'pacer',\n debug: props?.debug,\n })\n }\n}\n\nexport const emitChange = <\n TSuffix extends Extract<\n keyof PacerEventMap,\n `${string}:${string}`\n > extends `${string}:${infer S}`\n ? S\n : never,\n>(\n event: TSuffix,\n payload: PacerEventMap[`pacer:${TSuffix}`],\n) => {\n if (payload.key) {\n pacerEventClient.emit(event, payload)\n }\n}\n\nexport const pacerEventClient = new PacerEventClient()\n"],"mappings":";;;AA0CA,IAAM,mBAAN,cAA+BA,6CAA2B;CACxD,YAAY,OAA4B;AACtC,QAAM;GACJ,UAAU;GACV,OAAO,OAAO;GACf,CAAC;;;AAIN,MAAa,cAQX,OACA,YACG;AACH,KAAI,QAAQ,IACV,kBAAiB,KAAK,OAAO,QAAQ;;AAIzC,MAAa,mBAAmB,IAAI,kBAAkB"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { AsyncRetryer } from "./async-retryer.cjs";
|
|
2
|
+
import { AsyncBatcher } from "./async-batcher.cjs";
|
|
3
|
+
import { AsyncDebouncer } from "./async-debouncer.cjs";
|
|
4
|
+
import { Queuer } from "./queuer.cjs";
|
|
5
|
+
import { AsyncQueuer } from "./async-queuer.cjs";
|
|
6
|
+
import { AsyncRateLimiter } from "./async-rate-limiter.cjs";
|
|
7
|
+
import { AsyncThrottler } from "./async-throttler.cjs";
|
|
8
|
+
import { Batcher } from "./batcher.cjs";
|
|
9
|
+
import { Debouncer } from "./debouncer.cjs";
|
|
10
|
+
import { RateLimiter } from "./rate-limiter.cjs";
|
|
11
|
+
import { Throttler } from "./throttler.cjs";
|
|
12
|
+
import { EventClient } from "@tanstack/devtools-event-client";
|
|
13
|
+
|
|
14
|
+
//#region src/event-client.d.ts
|
|
15
|
+
interface PacerEventMap {
|
|
16
|
+
'pacer:d-AsyncBatcher': AsyncBatcher<any>;
|
|
17
|
+
'pacer:d-AsyncDebouncer': AsyncDebouncer<any>;
|
|
18
|
+
'pacer:d-AsyncQueuer': AsyncQueuer<any>;
|
|
19
|
+
'pacer:d-AsyncRateLimiter': AsyncRateLimiter<any>;
|
|
20
|
+
'pacer:d-AsyncRetryer': AsyncRetryer<any>;
|
|
21
|
+
'pacer:d-AsyncThrottler': AsyncThrottler<any>;
|
|
22
|
+
'pacer:d-Batcher': Batcher<any>;
|
|
23
|
+
'pacer:d-Debouncer': Debouncer<any>;
|
|
24
|
+
'pacer:d-Queuer': Queuer<any>;
|
|
25
|
+
'pacer:d-RateLimiter': RateLimiter<any>;
|
|
26
|
+
'pacer:d-Throttler': Throttler<any>;
|
|
27
|
+
'pacer:AsyncBatcher': AsyncBatcher<any>;
|
|
28
|
+
'pacer:AsyncDebouncer': AsyncDebouncer<any>;
|
|
29
|
+
'pacer:AsyncQueuer': AsyncQueuer<any>;
|
|
30
|
+
'pacer:AsyncRateLimiter': AsyncRateLimiter<any>;
|
|
31
|
+
'pacer:AsyncRetryer': AsyncRetryer<any>;
|
|
32
|
+
'pacer:AsyncThrottler': AsyncThrottler<any>;
|
|
33
|
+
'pacer:Batcher': Batcher<any>;
|
|
34
|
+
'pacer:Debouncer': Debouncer<any>;
|
|
35
|
+
'pacer:Queuer': Queuer<any>;
|
|
36
|
+
'pacer:RateLimiter': RateLimiter<any>;
|
|
37
|
+
'pacer:Throttler': Throttler<any>;
|
|
38
|
+
}
|
|
39
|
+
type PacerEventName = keyof PacerEventMap extends `pacer:${infer T}` ? T : never;
|
|
40
|
+
declare class PacerEventClient extends EventClient<PacerEventMap> {
|
|
41
|
+
constructor(props?: {
|
|
42
|
+
debug: boolean;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
declare const emitChange: <TSuffix extends (Extract<keyof PacerEventMap, `${string}:${string}`> extends `${string}:${infer S}` ? S : never)>(event: TSuffix, payload: PacerEventMap[`pacer:${TSuffix}`]) => void;
|
|
46
|
+
declare const pacerEventClient: PacerEventClient;
|
|
47
|
+
//#endregion
|
|
48
|
+
export { PacerEventMap, PacerEventName, emitChange, pacerEventClient };
|
|
49
|
+
//# sourceMappingURL=event-client.d.cts.map
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { AsyncRetryer } from "./async-retryer.js";
|
|
2
|
+
import { AsyncBatcher } from "./async-batcher.js";
|
|
3
|
+
import { AsyncDebouncer } from "./async-debouncer.js";
|
|
4
|
+
import { Queuer } from "./queuer.js";
|
|
5
|
+
import { AsyncQueuer } from "./async-queuer.js";
|
|
6
|
+
import { AsyncRateLimiter } from "./async-rate-limiter.js";
|
|
7
|
+
import { AsyncThrottler } from "./async-throttler.js";
|
|
8
|
+
import { Batcher } from "./batcher.js";
|
|
9
|
+
import { Debouncer } from "./debouncer.js";
|
|
10
|
+
import { RateLimiter } from "./rate-limiter.js";
|
|
11
|
+
import { Throttler } from "./throttler.js";
|
|
12
|
+
import { EventClient } from "@tanstack/devtools-event-client";
|
|
13
|
+
|
|
14
|
+
//#region src/event-client.d.ts
|
|
15
|
+
interface PacerEventMap {
|
|
16
|
+
'pacer:d-AsyncBatcher': AsyncBatcher<any>;
|
|
17
|
+
'pacer:d-AsyncDebouncer': AsyncDebouncer<any>;
|
|
18
|
+
'pacer:d-AsyncQueuer': AsyncQueuer<any>;
|
|
19
|
+
'pacer:d-AsyncRateLimiter': AsyncRateLimiter<any>;
|
|
20
|
+
'pacer:d-AsyncRetryer': AsyncRetryer<any>;
|
|
21
|
+
'pacer:d-AsyncThrottler': AsyncThrottler<any>;
|
|
22
|
+
'pacer:d-Batcher': Batcher<any>;
|
|
23
|
+
'pacer:d-Debouncer': Debouncer<any>;
|
|
24
|
+
'pacer:d-Queuer': Queuer<any>;
|
|
25
|
+
'pacer:d-RateLimiter': RateLimiter<any>;
|
|
26
|
+
'pacer:d-Throttler': Throttler<any>;
|
|
27
|
+
'pacer:AsyncBatcher': AsyncBatcher<any>;
|
|
28
|
+
'pacer:AsyncDebouncer': AsyncDebouncer<any>;
|
|
29
|
+
'pacer:AsyncQueuer': AsyncQueuer<any>;
|
|
30
|
+
'pacer:AsyncRateLimiter': AsyncRateLimiter<any>;
|
|
31
|
+
'pacer:AsyncRetryer': AsyncRetryer<any>;
|
|
32
|
+
'pacer:AsyncThrottler': AsyncThrottler<any>;
|
|
33
|
+
'pacer:Batcher': Batcher<any>;
|
|
34
|
+
'pacer:Debouncer': Debouncer<any>;
|
|
35
|
+
'pacer:Queuer': Queuer<any>;
|
|
36
|
+
'pacer:RateLimiter': RateLimiter<any>;
|
|
37
|
+
'pacer:Throttler': Throttler<any>;
|
|
38
|
+
}
|
|
39
|
+
type PacerEventName = keyof PacerEventMap extends `pacer:${infer T}` ? T : never;
|
|
40
|
+
declare class PacerEventClient extends EventClient<PacerEventMap> {
|
|
41
|
+
constructor(props?: {
|
|
42
|
+
debug: boolean;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
declare const emitChange: <TSuffix extends (Extract<keyof PacerEventMap, `${string}:${string}`> extends `${string}:${infer S}` ? S : never)>(event: TSuffix, payload: PacerEventMap[`pacer:${TSuffix}`]) => void;
|
|
46
|
+
declare const pacerEventClient: PacerEventClient;
|
|
47
|
+
//#endregion
|
|
48
|
+
export { PacerEventMap, PacerEventName, emitChange, pacerEventClient };
|
|
49
|
+
//# sourceMappingURL=event-client.d.ts.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { EventClient } from "@tanstack/devtools-event-client";
|
|
2
|
+
|
|
3
|
+
//#region src/event-client.ts
|
|
4
|
+
var PacerEventClient = class extends EventClient {
|
|
5
|
+
constructor(props) {
|
|
6
|
+
super({
|
|
7
|
+
pluginId: "pacer",
|
|
8
|
+
debug: props?.debug
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
const emitChange = (event, payload) => {
|
|
13
|
+
if (payload.key) pacerEventClient.emit(event, payload);
|
|
14
|
+
};
|
|
15
|
+
const pacerEventClient = new PacerEventClient();
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { emitChange, pacerEventClient };
|
|
19
|
+
//# sourceMappingURL=event-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-client.js","names":[],"sources":["../src/event-client.ts"],"sourcesContent":["import { EventClient } from '@tanstack/devtools-event-client'\nimport type { AsyncBatcher } from './async-batcher'\nimport type { AsyncDebouncer } from './async-debouncer'\nimport type { AsyncQueuer } from './async-queuer'\nimport type { AsyncRateLimiter } from './async-rate-limiter'\nimport type { AsyncRetryer } from './async-retryer'\nimport type { AsyncThrottler } from './async-throttler'\nimport type { Debouncer } from './debouncer'\nimport type { Batcher } from './batcher'\nimport type { Queuer } from './queuer'\nimport type { RateLimiter } from './rate-limiter'\nimport type { Throttler } from './throttler'\n\nexport interface PacerEventMap {\n 'pacer:d-AsyncBatcher': AsyncBatcher<any>\n 'pacer:d-AsyncDebouncer': AsyncDebouncer<any>\n 'pacer:d-AsyncQueuer': AsyncQueuer<any>\n 'pacer:d-AsyncRateLimiter': AsyncRateLimiter<any>\n 'pacer:d-AsyncRetryer': AsyncRetryer<any>\n 'pacer:d-AsyncThrottler': AsyncThrottler<any>\n 'pacer:d-Batcher': Batcher<any>\n 'pacer:d-Debouncer': Debouncer<any>\n 'pacer:d-Queuer': Queuer<any>\n 'pacer:d-RateLimiter': RateLimiter<any>\n 'pacer:d-Throttler': Throttler<any>\n 'pacer:AsyncBatcher': AsyncBatcher<any>\n 'pacer:AsyncDebouncer': AsyncDebouncer<any>\n 'pacer:AsyncQueuer': AsyncQueuer<any>\n 'pacer:AsyncRateLimiter': AsyncRateLimiter<any>\n 'pacer:AsyncRetryer': AsyncRetryer<any>\n 'pacer:AsyncThrottler': AsyncThrottler<any>\n 'pacer:Batcher': Batcher<any>\n 'pacer:Debouncer': Debouncer<any>\n 'pacer:Queuer': Queuer<any>\n 'pacer:RateLimiter': RateLimiter<any>\n 'pacer:Throttler': Throttler<any>\n}\n\nexport type PacerEventName = keyof PacerEventMap extends `pacer:${infer T}`\n ? T\n : never\n\nclass PacerEventClient extends EventClient<PacerEventMap> {\n constructor(props?: { debug: boolean }) {\n super({\n pluginId: 'pacer',\n debug: props?.debug,\n })\n }\n}\n\nexport const emitChange = <\n TSuffix extends Extract<\n keyof PacerEventMap,\n `${string}:${string}`\n > extends `${string}:${infer S}`\n ? S\n : never,\n>(\n event: TSuffix,\n payload: PacerEventMap[`pacer:${TSuffix}`],\n) => {\n if (payload.key) {\n pacerEventClient.emit(event, payload)\n }\n}\n\nexport const pacerEventClient = new PacerEventClient()\n"],"mappings":";;;AA0CA,IAAM,mBAAN,cAA+B,YAA2B;CACxD,YAAY,OAA4B;AACtC,QAAM;GACJ,UAAU;GACV,OAAO,OAAO;GACf,CAAC;;;AAIN,MAAa,cAQX,OACA,YACG;AACH,KAAI,QAAQ,IACV,kBAAiB,KAAK,OAAO,QAAQ;;AAIzC,MAAa,mBAAmB,IAAI,kBAAkB"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const require_utils = require('./utils.cjs');
|
|
2
|
+
const require_event_client = require('./event-client.cjs');
|
|
3
|
+
const require_async_retryer = require('./async-retryer.cjs');
|
|
4
|
+
const require_async_batcher = require('./async-batcher.cjs');
|
|
5
|
+
const require_async_debouncer = require('./async-debouncer.cjs');
|
|
6
|
+
const require_async_queuer = require('./async-queuer.cjs');
|
|
7
|
+
const require_async_rate_limiter = require('./async-rate-limiter.cjs');
|
|
8
|
+
const require_async_throttler = require('./async-throttler.cjs');
|
|
9
|
+
const require_batcher = require('./batcher.cjs');
|
|
10
|
+
const require_debouncer = require('./debouncer.cjs');
|
|
11
|
+
const require_queuer = require('./queuer.cjs');
|
|
12
|
+
const require_rate_limiter = require('./rate-limiter.cjs');
|
|
13
|
+
const require_throttler = require('./throttler.cjs');
|
|
14
|
+
|
|
15
|
+
exports.AsyncBatcher = require_async_batcher.AsyncBatcher;
|
|
16
|
+
exports.AsyncDebouncer = require_async_debouncer.AsyncDebouncer;
|
|
17
|
+
exports.AsyncQueuer = require_async_queuer.AsyncQueuer;
|
|
18
|
+
exports.AsyncRateLimiter = require_async_rate_limiter.AsyncRateLimiter;
|
|
19
|
+
exports.AsyncRetryer = require_async_retryer.AsyncRetryer;
|
|
20
|
+
exports.AsyncThrottler = require_async_throttler.AsyncThrottler;
|
|
21
|
+
exports.Batcher = require_batcher.Batcher;
|
|
22
|
+
exports.Debouncer = require_debouncer.Debouncer;
|
|
23
|
+
exports.Queuer = require_queuer.Queuer;
|
|
24
|
+
exports.RateLimiter = require_rate_limiter.RateLimiter;
|
|
25
|
+
exports.Throttler = require_throttler.Throttler;
|
|
26
|
+
exports.asyncBatch = require_async_batcher.asyncBatch;
|
|
27
|
+
exports.asyncBatcherOptions = require_async_batcher.asyncBatcherOptions;
|
|
28
|
+
exports.asyncDebounce = require_async_debouncer.asyncDebounce;
|
|
29
|
+
exports.asyncDebouncerOptions = require_async_debouncer.asyncDebouncerOptions;
|
|
30
|
+
exports.asyncQueue = require_async_queuer.asyncQueue;
|
|
31
|
+
exports.asyncQueuerOptions = require_async_queuer.asyncQueuerOptions;
|
|
32
|
+
exports.asyncRateLimit = require_async_rate_limiter.asyncRateLimit;
|
|
33
|
+
exports.asyncRateLimiterOptions = require_async_rate_limiter.asyncRateLimiterOptions;
|
|
34
|
+
exports.asyncRetry = require_async_retryer.asyncRetry;
|
|
35
|
+
exports.asyncRetryerOptions = require_async_retryer.asyncRetryerOptions;
|
|
36
|
+
exports.asyncThrottle = require_async_throttler.asyncThrottle;
|
|
37
|
+
exports.asyncThrottlerOptions = require_async_throttler.asyncThrottlerOptions;
|
|
38
|
+
exports.batch = require_batcher.batch;
|
|
39
|
+
exports.debounce = require_debouncer.debounce;
|
|
40
|
+
exports.debouncerOptions = require_debouncer.debouncerOptions;
|
|
41
|
+
exports.isFunction = require_utils.isFunction;
|
|
42
|
+
exports.pacerEventClient = require_event_client.pacerEventClient;
|
|
43
|
+
exports.parseFunctionOrValue = require_utils.parseFunctionOrValue;
|
|
44
|
+
exports.queue = require_queuer.queue;
|
|
45
|
+
exports.queuerOptions = require_queuer.queuerOptions;
|
|
46
|
+
exports.rateLimit = require_rate_limiter.rateLimit;
|
|
47
|
+
exports.rateLimiterOptions = require_rate_limiter.rateLimiterOptions;
|
|
48
|
+
exports.throttle = require_throttler.throttle;
|
|
49
|
+
exports.throttlerOptions = require_throttler.throttlerOptions;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AnyAsyncFunction, AnyFunction, OptionalKeys } from "./types.cjs";
|
|
2
|
+
import { AsyncRetryer, AsyncRetryerOptions, AsyncRetryerState, asyncRetry, asyncRetryerOptions } from "./async-retryer.cjs";
|
|
3
|
+
import { AsyncBatcher, AsyncBatcherOptions, AsyncBatcherState, asyncBatch, asyncBatcherOptions } from "./async-batcher.cjs";
|
|
4
|
+
import { AsyncDebouncer, AsyncDebouncerOptions, AsyncDebouncerState, asyncDebounce, asyncDebouncerOptions } from "./async-debouncer.cjs";
|
|
5
|
+
import { QueuePosition, Queuer, QueuerOptions, QueuerState, queue, queuerOptions } from "./queuer.cjs";
|
|
6
|
+
import { AsyncQueuer, AsyncQueuerOptions, AsyncQueuerState, asyncQueue, asyncQueuerOptions } from "./async-queuer.cjs";
|
|
7
|
+
import { AsyncRateLimiter, AsyncRateLimiterOptions, AsyncRateLimiterState, asyncRateLimit, asyncRateLimiterOptions } from "./async-rate-limiter.cjs";
|
|
8
|
+
import { AsyncThrottler, AsyncThrottlerOptions, AsyncThrottlerState, asyncThrottle, asyncThrottlerOptions } from "./async-throttler.cjs";
|
|
9
|
+
import { Batcher, BatcherOptions, BatcherState, batch } from "./batcher.cjs";
|
|
10
|
+
import { Debouncer, DebouncerOptions, DebouncerState, debounce, debouncerOptions } from "./debouncer.cjs";
|
|
11
|
+
import { RateLimiter, RateLimiterOptions, RateLimiterState, rateLimit, rateLimiterOptions } from "./rate-limiter.cjs";
|
|
12
|
+
import { Throttler, ThrottlerOptions, ThrottlerState, throttle, throttlerOptions } from "./throttler.cjs";
|
|
13
|
+
import { PacerEventMap, PacerEventName, pacerEventClient } from "./event-client.cjs";
|
|
14
|
+
import { isFunction, parseFunctionOrValue } from "./utils.cjs";
|
|
15
|
+
export { AnyAsyncFunction, AnyFunction, AsyncBatcher, AsyncBatcherOptions, AsyncBatcherState, AsyncDebouncer, AsyncDebouncerOptions, AsyncDebouncerState, AsyncQueuer, AsyncQueuerOptions, AsyncQueuerState, AsyncRateLimiter, AsyncRateLimiterOptions, AsyncRateLimiterState, AsyncRetryer, AsyncRetryerOptions, AsyncRetryerState, AsyncThrottler, AsyncThrottlerOptions, AsyncThrottlerState, Batcher, BatcherOptions, BatcherState, Debouncer, DebouncerOptions, DebouncerState, OptionalKeys, type PacerEventMap, type PacerEventName, QueuePosition, Queuer, QueuerOptions, QueuerState, RateLimiter, RateLimiterOptions, RateLimiterState, Throttler, ThrottlerOptions, ThrottlerState, asyncBatch, asyncBatcherOptions, asyncDebounce, asyncDebouncerOptions, asyncQueue, asyncQueuerOptions, asyncRateLimit, asyncRateLimiterOptions, asyncRetry, asyncRetryerOptions, asyncThrottle, asyncThrottlerOptions, batch, debounce, debouncerOptions, isFunction, pacerEventClient, parseFunctionOrValue, queue, queuerOptions, rateLimit, rateLimiterOptions, throttle, throttlerOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AnyAsyncFunction, AnyFunction, OptionalKeys } from "./types.js";
|
|
2
|
+
import { AsyncRetryer, AsyncRetryerOptions, AsyncRetryerState, asyncRetry, asyncRetryerOptions } from "./async-retryer.js";
|
|
3
|
+
import { AsyncBatcher, AsyncBatcherOptions, AsyncBatcherState, asyncBatch, asyncBatcherOptions } from "./async-batcher.js";
|
|
4
|
+
import { AsyncDebouncer, AsyncDebouncerOptions, AsyncDebouncerState, asyncDebounce, asyncDebouncerOptions } from "./async-debouncer.js";
|
|
5
|
+
import { QueuePosition, Queuer, QueuerOptions, QueuerState, queue, queuerOptions } from "./queuer.js";
|
|
6
|
+
import { AsyncQueuer, AsyncQueuerOptions, AsyncQueuerState, asyncQueue, asyncQueuerOptions } from "./async-queuer.js";
|
|
7
|
+
import { AsyncRateLimiter, AsyncRateLimiterOptions, AsyncRateLimiterState, asyncRateLimit, asyncRateLimiterOptions } from "./async-rate-limiter.js";
|
|
8
|
+
import { AsyncThrottler, AsyncThrottlerOptions, AsyncThrottlerState, asyncThrottle, asyncThrottlerOptions } from "./async-throttler.js";
|
|
9
|
+
import { Batcher, BatcherOptions, BatcherState, batch } from "./batcher.js";
|
|
10
|
+
import { Debouncer, DebouncerOptions, DebouncerState, debounce, debouncerOptions } from "./debouncer.js";
|
|
11
|
+
import { RateLimiter, RateLimiterOptions, RateLimiterState, rateLimit, rateLimiterOptions } from "./rate-limiter.js";
|
|
12
|
+
import { Throttler, ThrottlerOptions, ThrottlerState, throttle, throttlerOptions } from "./throttler.js";
|
|
13
|
+
import { PacerEventMap, PacerEventName, pacerEventClient } from "./event-client.js";
|
|
14
|
+
import { isFunction, parseFunctionOrValue } from "./utils.js";
|
|
15
|
+
export { AnyAsyncFunction, AnyFunction, AsyncBatcher, AsyncBatcherOptions, AsyncBatcherState, AsyncDebouncer, AsyncDebouncerOptions, AsyncDebouncerState, AsyncQueuer, AsyncQueuerOptions, AsyncQueuerState, AsyncRateLimiter, AsyncRateLimiterOptions, AsyncRateLimiterState, AsyncRetryer, AsyncRetryerOptions, AsyncRetryerState, AsyncThrottler, AsyncThrottlerOptions, AsyncThrottlerState, Batcher, BatcherOptions, BatcherState, Debouncer, DebouncerOptions, DebouncerState, OptionalKeys, type PacerEventMap, type PacerEventName, QueuePosition, Queuer, QueuerOptions, QueuerState, RateLimiter, RateLimiterOptions, RateLimiterState, Throttler, ThrottlerOptions, ThrottlerState, asyncBatch, asyncBatcherOptions, asyncDebounce, asyncDebouncerOptions, asyncQueue, asyncQueuerOptions, asyncRateLimit, asyncRateLimiterOptions, asyncRetry, asyncRetryerOptions, asyncThrottle, asyncThrottlerOptions, batch, debounce, debouncerOptions, isFunction, pacerEventClient, parseFunctionOrValue, queue, queuerOptions, rateLimit, rateLimiterOptions, throttle, throttlerOptions };
|
|
@@ -1,51 +1,15 @@
|
|
|
1
|
+
import { isFunction, parseFunctionOrValue } from "./utils.js";
|
|
2
|
+
import { pacerEventClient } from "./event-client.js";
|
|
3
|
+
import { AsyncRetryer, asyncRetry, asyncRetryerOptions } from "./async-retryer.js";
|
|
1
4
|
import { AsyncBatcher, asyncBatch, asyncBatcherOptions } from "./async-batcher.js";
|
|
2
5
|
import { AsyncDebouncer, asyncDebounce, asyncDebouncerOptions } from "./async-debouncer.js";
|
|
3
6
|
import { AsyncQueuer, asyncQueue, asyncQueuerOptions } from "./async-queuer.js";
|
|
4
7
|
import { AsyncRateLimiter, asyncRateLimit, asyncRateLimiterOptions } from "./async-rate-limiter.js";
|
|
5
|
-
import { AsyncRetryer, asyncRetry, asyncRetryerOptions } from "./async-retryer.js";
|
|
6
8
|
import { AsyncThrottler, asyncThrottle, asyncThrottlerOptions } from "./async-throttler.js";
|
|
7
9
|
import { Batcher, batch } from "./batcher.js";
|
|
8
10
|
import { Debouncer, debounce, debouncerOptions } from "./debouncer.js";
|
|
9
11
|
import { Queuer, queue, queuerOptions } from "./queuer.js";
|
|
10
12
|
import { RateLimiter, rateLimit, rateLimiterOptions } from "./rate-limiter.js";
|
|
11
13
|
import { Throttler, throttle, throttlerOptions } from "./throttler.js";
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export {
|
|
15
|
-
AsyncBatcher,
|
|
16
|
-
AsyncDebouncer,
|
|
17
|
-
AsyncQueuer,
|
|
18
|
-
AsyncRateLimiter,
|
|
19
|
-
AsyncRetryer,
|
|
20
|
-
AsyncThrottler,
|
|
21
|
-
Batcher,
|
|
22
|
-
Debouncer,
|
|
23
|
-
Queuer,
|
|
24
|
-
RateLimiter,
|
|
25
|
-
Throttler,
|
|
26
|
-
asyncBatch,
|
|
27
|
-
asyncBatcherOptions,
|
|
28
|
-
asyncDebounce,
|
|
29
|
-
asyncDebouncerOptions,
|
|
30
|
-
asyncQueue,
|
|
31
|
-
asyncQueuerOptions,
|
|
32
|
-
asyncRateLimit,
|
|
33
|
-
asyncRateLimiterOptions,
|
|
34
|
-
asyncRetry,
|
|
35
|
-
asyncRetryerOptions,
|
|
36
|
-
asyncThrottle,
|
|
37
|
-
asyncThrottlerOptions,
|
|
38
|
-
batch,
|
|
39
|
-
debounce,
|
|
40
|
-
debouncerOptions,
|
|
41
|
-
isFunction,
|
|
42
|
-
pacerEventClient,
|
|
43
|
-
parseFunctionOrValue,
|
|
44
|
-
queue,
|
|
45
|
-
queuerOptions,
|
|
46
|
-
rateLimit,
|
|
47
|
-
rateLimiterOptions,
|
|
48
|
-
throttle,
|
|
49
|
-
throttlerOptions
|
|
50
|
-
};
|
|
51
|
-
//# sourceMappingURL=index.js.map
|
|
14
|
+
|
|
15
|
+
export { AsyncBatcher, AsyncDebouncer, AsyncQueuer, AsyncRateLimiter, AsyncRetryer, AsyncThrottler, Batcher, Debouncer, Queuer, RateLimiter, Throttler, asyncBatch, asyncBatcherOptions, asyncDebounce, asyncDebouncerOptions, asyncQueue, asyncQueuerOptions, asyncRateLimit, asyncRateLimiterOptions, asyncRetry, asyncRetryerOptions, asyncThrottle, asyncThrottlerOptions, batch, debounce, debouncerOptions, isFunction, pacerEventClient, parseFunctionOrValue, queue, queuerOptions, rateLimit, rateLimiterOptions, throttle, throttlerOptions };
|