@tanstack/pacer 0.6.0 → 0.7.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/cjs/async-debouncer.cjs +10 -6
- package/dist/cjs/async-debouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer.d.cts +2 -2
- package/dist/cjs/async-queuer.cjs +135 -106
- package/dist/cjs/async-queuer.cjs.map +1 -1
- package/dist/cjs/async-queuer.d.cts +121 -93
- package/dist/cjs/async-rate-limiter.cjs +3 -4
- package/dist/cjs/async-rate-limiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter.d.cts +1 -2
- package/dist/cjs/async-throttler.cjs +14 -4
- package/dist/cjs/async-throttler.cjs.map +1 -1
- package/dist/cjs/async-throttler.d.cts +3 -2
- package/dist/cjs/batcher.cjs +138 -0
- package/dist/cjs/batcher.cjs.map +1 -0
- package/dist/cjs/batcher.d.cts +149 -0
- package/dist/cjs/debouncer.cjs +3 -4
- package/dist/cjs/debouncer.cjs.map +1 -1
- package/dist/cjs/debouncer.d.cts +1 -2
- package/dist/cjs/index.cjs +3 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/queuer.cjs +68 -41
- package/dist/cjs/queuer.cjs.map +1 -1
- package/dist/cjs/queuer.d.cts +123 -92
- package/dist/cjs/rate-limiter.cjs +3 -4
- package/dist/cjs/rate-limiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter.d.cts +1 -2
- package/dist/cjs/throttler.cjs +3 -4
- package/dist/cjs/throttler.cjs.map +1 -1
- package/dist/cjs/throttler.d.cts +1 -2
- package/dist/esm/async-debouncer.d.ts +2 -2
- package/dist/esm/async-debouncer.js +10 -6
- package/dist/esm/async-debouncer.js.map +1 -1
- package/dist/esm/async-queuer.d.ts +121 -93
- package/dist/esm/async-queuer.js +135 -106
- package/dist/esm/async-queuer.js.map +1 -1
- package/dist/esm/async-rate-limiter.d.ts +1 -2
- package/dist/esm/async-rate-limiter.js +3 -4
- package/dist/esm/async-rate-limiter.js.map +1 -1
- package/dist/esm/async-throttler.d.ts +3 -2
- package/dist/esm/async-throttler.js +14 -4
- package/dist/esm/async-throttler.js.map +1 -1
- package/dist/esm/batcher.d.ts +149 -0
- package/dist/esm/batcher.js +138 -0
- package/dist/esm/batcher.js.map +1 -0
- package/dist/esm/debouncer.d.ts +1 -2
- package/dist/esm/debouncer.js +3 -4
- package/dist/esm/debouncer.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/queuer.d.ts +123 -92
- package/dist/esm/queuer.js +68 -41
- package/dist/esm/queuer.js.map +1 -1
- package/dist/esm/rate-limiter.d.ts +1 -2
- package/dist/esm/rate-limiter.js +3 -4
- package/dist/esm/rate-limiter.js.map +1 -1
- package/dist/esm/throttler.d.ts +1 -2
- package/dist/esm/throttler.js +3 -4
- package/dist/esm/throttler.js.map +1 -1
- package/package.json +11 -1
- package/src/async-debouncer.ts +12 -6
- package/src/async-queuer.ts +216 -193
- package/src/async-rate-limiter.ts +3 -4
- package/src/async-throttler.ts +18 -4
- package/src/batcher.ts +253 -0
- package/src/debouncer.ts +3 -4
- package/src/index.ts +1 -0
- package/src/queuer.ts +142 -98
- package/src/rate-limiter.ts +3 -4
- package/src/throttler.ts +3 -4
package/src/async-debouncer.ts
CHANGED
|
@@ -110,6 +110,9 @@ export class AsyncDebouncer<TFn extends AnyAsyncFunction> {
|
|
|
110
110
|
private _settleCount = 0
|
|
111
111
|
private _successCount = 0
|
|
112
112
|
private _timeoutId: NodeJS.Timeout | null = null
|
|
113
|
+
private _resolvePreviousPromise:
|
|
114
|
+
| ((value?: ReturnType<TFn> | undefined) => void)
|
|
115
|
+
| null = null
|
|
113
116
|
|
|
114
117
|
constructor(
|
|
115
118
|
private fn: TFn,
|
|
@@ -124,7 +127,6 @@ export class AsyncDebouncer<TFn extends AnyAsyncFunction> {
|
|
|
124
127
|
|
|
125
128
|
/**
|
|
126
129
|
* Updates the debouncer options
|
|
127
|
-
* Returns the new options state
|
|
128
130
|
*/
|
|
129
131
|
setOptions(newOptions: Partial<AsyncDebouncerOptions<TFn>>): void {
|
|
130
132
|
this._options = { ...this._options, ...newOptions }
|
|
@@ -179,7 +181,7 @@ export class AsyncDebouncer<TFn extends AnyAsyncFunction> {
|
|
|
179
181
|
// Handle leading execution
|
|
180
182
|
if (this._options.leading && this._canLeadingExecute) {
|
|
181
183
|
this._canLeadingExecute = false
|
|
182
|
-
await this.
|
|
184
|
+
await this.execute(...args)
|
|
183
185
|
return this._lastResult
|
|
184
186
|
}
|
|
185
187
|
|
|
@@ -189,20 +191,22 @@ export class AsyncDebouncer<TFn extends AnyAsyncFunction> {
|
|
|
189
191
|
}
|
|
190
192
|
|
|
191
193
|
return new Promise((resolve) => {
|
|
194
|
+
this._resolvePreviousPromise = resolve
|
|
192
195
|
this._timeoutId = setTimeout(async () => {
|
|
193
196
|
// Execute trailing if enabled
|
|
194
197
|
if (this._options.trailing && this._lastArgs) {
|
|
195
|
-
await this.
|
|
198
|
+
await this.execute(...this._lastArgs)
|
|
196
199
|
}
|
|
197
200
|
|
|
198
201
|
// Reset state and resolve
|
|
199
202
|
this._canLeadingExecute = true
|
|
203
|
+
this._resolvePreviousPromise = null
|
|
200
204
|
resolve(this._lastResult)
|
|
201
205
|
}, this.getWait())
|
|
202
206
|
})
|
|
203
207
|
}
|
|
204
208
|
|
|
205
|
-
private async
|
|
209
|
+
private async execute(
|
|
206
210
|
...args: Parameters<TFn>
|
|
207
211
|
): Promise<ReturnType<TFn> | undefined> {
|
|
208
212
|
if (!this.getEnabled()) return undefined
|
|
@@ -217,8 +221,6 @@ export class AsyncDebouncer<TFn extends AnyAsyncFunction> {
|
|
|
217
221
|
this._options.onError?.(error, this)
|
|
218
222
|
if (this._options.throwOnError) {
|
|
219
223
|
throw error
|
|
220
|
-
} else {
|
|
221
|
-
console.error(error)
|
|
222
224
|
}
|
|
223
225
|
} finally {
|
|
224
226
|
this._isExecuting = false
|
|
@@ -242,6 +244,10 @@ export class AsyncDebouncer<TFn extends AnyAsyncFunction> {
|
|
|
242
244
|
this._abortController.abort()
|
|
243
245
|
this._abortController = null
|
|
244
246
|
}
|
|
247
|
+
if (this._resolvePreviousPromise) {
|
|
248
|
+
this._resolvePreviousPromise(this._lastResult)
|
|
249
|
+
this._resolvePreviousPromise = null
|
|
250
|
+
}
|
|
245
251
|
this._lastArgs = undefined
|
|
246
252
|
this._isPending = false
|
|
247
253
|
this._isExecuting = false
|