@tanstack/pacer 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/dist/cjs/async-debouncer.cjs +10 -6
  2. package/dist/cjs/async-debouncer.cjs.map +1 -1
  3. package/dist/cjs/async-debouncer.d.cts +2 -2
  4. package/dist/cjs/async-queuer.cjs +140 -111
  5. package/dist/cjs/async-queuer.cjs.map +1 -1
  6. package/dist/cjs/async-queuer.d.cts +121 -93
  7. package/dist/cjs/async-rate-limiter.cjs +3 -4
  8. package/dist/cjs/async-rate-limiter.cjs.map +1 -1
  9. package/dist/cjs/async-rate-limiter.d.cts +1 -2
  10. package/dist/cjs/async-throttler.cjs +14 -4
  11. package/dist/cjs/async-throttler.cjs.map +1 -1
  12. package/dist/cjs/async-throttler.d.cts +3 -2
  13. package/dist/cjs/batcher.cjs +138 -0
  14. package/dist/cjs/batcher.cjs.map +1 -0
  15. package/dist/cjs/batcher.d.cts +149 -0
  16. package/dist/cjs/debouncer.cjs +3 -4
  17. package/dist/cjs/debouncer.cjs.map +1 -1
  18. package/dist/cjs/debouncer.d.cts +1 -2
  19. package/dist/cjs/index.cjs +3 -0
  20. package/dist/cjs/index.cjs.map +1 -1
  21. package/dist/cjs/index.d.cts +1 -0
  22. package/dist/cjs/queuer.cjs +70 -43
  23. package/dist/cjs/queuer.cjs.map +1 -1
  24. package/dist/cjs/queuer.d.cts +126 -95
  25. package/dist/cjs/rate-limiter.cjs +3 -4
  26. package/dist/cjs/rate-limiter.cjs.map +1 -1
  27. package/dist/cjs/rate-limiter.d.cts +1 -2
  28. package/dist/cjs/throttler.cjs +3 -4
  29. package/dist/cjs/throttler.cjs.map +1 -1
  30. package/dist/cjs/throttler.d.cts +1 -2
  31. package/dist/esm/async-debouncer.d.ts +2 -2
  32. package/dist/esm/async-debouncer.js +10 -6
  33. package/dist/esm/async-debouncer.js.map +1 -1
  34. package/dist/esm/async-queuer.d.ts +121 -93
  35. package/dist/esm/async-queuer.js +140 -111
  36. package/dist/esm/async-queuer.js.map +1 -1
  37. package/dist/esm/async-rate-limiter.d.ts +1 -2
  38. package/dist/esm/async-rate-limiter.js +3 -4
  39. package/dist/esm/async-rate-limiter.js.map +1 -1
  40. package/dist/esm/async-throttler.d.ts +3 -2
  41. package/dist/esm/async-throttler.js +14 -4
  42. package/dist/esm/async-throttler.js.map +1 -1
  43. package/dist/esm/batcher.d.ts +149 -0
  44. package/dist/esm/batcher.js +138 -0
  45. package/dist/esm/batcher.js.map +1 -0
  46. package/dist/esm/debouncer.d.ts +1 -2
  47. package/dist/esm/debouncer.js +3 -4
  48. package/dist/esm/debouncer.js.map +1 -1
  49. package/dist/esm/index.d.ts +1 -0
  50. package/dist/esm/index.js +3 -0
  51. package/dist/esm/index.js.map +1 -1
  52. package/dist/esm/queuer.d.ts +126 -95
  53. package/dist/esm/queuer.js +70 -43
  54. package/dist/esm/queuer.js.map +1 -1
  55. package/dist/esm/rate-limiter.d.ts +1 -2
  56. package/dist/esm/rate-limiter.js +3 -4
  57. package/dist/esm/rate-limiter.js.map +1 -1
  58. package/dist/esm/throttler.d.ts +1 -2
  59. package/dist/esm/throttler.js +3 -4
  60. package/dist/esm/throttler.js.map +1 -1
  61. package/package.json +11 -1
  62. package/src/async-debouncer.ts +12 -6
  63. package/src/async-queuer.ts +217 -194
  64. package/src/async-rate-limiter.ts +3 -4
  65. package/src/async-throttler.ts +18 -4
  66. package/src/batcher.ts +253 -0
  67. package/src/debouncer.ts +3 -4
  68. package/src/index.ts +1 -0
  69. package/src/queuer.ts +145 -101
  70. package/src/rate-limiter.ts +3 -4
  71. package/src/throttler.ts +3 -4
@@ -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.executeFunction(...args)
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.executeFunction(...this._lastArgs)
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 executeFunction(
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