Skip to content

Commit ad48440

Browse files
committed
ref: Normalise API for queues (separate timeMs from options)
1 parent de47c35 commit ad48440

File tree

4 files changed

+13
-16
lines changed

4 files changed

+13
-16
lines changed

packages/nuqs/src/lib/queues/throttle.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,22 @@ describe('throttle: ThrottleQueue option combination logic', () => {
9494
})
9595
it('keeps the maximum value for timeMs', () => {
9696
const queue = new ThrottledQueue()
97-
queue.push({ key: 'a', query: null, options: {}, timeMs: 100 })
98-
queue.push({ key: 'b', query: null, options: {}, timeMs: 200 })
99-
queue.push({ key: 'c', query: null, options: {}, timeMs: 300 })
97+
queue.push({ key: 'a', query: null, options: {} }, 100)
98+
queue.push({ key: 'b', query: null, options: {} }, 200)
99+
queue.push({ key: 'c', query: null, options: {} }, 300)
100100
expect(queue.timeMs).toEqual(300)
101101
})
102102
it('clamps the minimum value for timeMs to the default rate limit', () => {
103103
expect(defaultRateLimit.timeMs).toBeGreaterThan(10) // precondition
104104
const queue = new ThrottledQueue()
105-
queue.push({ key: 'a', query: null, options: {}, timeMs: 10 })
105+
queue.push({ key: 'a', query: null, options: {} }, 10)
106106
expect(queue.timeMs).toEqual(defaultRateLimit.timeMs)
107107
})
108108
it('supports passing Infinity to the timeMs option (but can be cleared)', () => {
109109
const queue = new ThrottledQueue()
110-
queue.push({ key: 'a', query: null, options: {}, timeMs: Infinity })
110+
queue.push({ key: 'a', query: null, options: {} }, Infinity)
111111
expect(queue.timeMs).toBe(Infinity)
112-
queue.push({ key: 'b', query: null, options: {}, timeMs: 100 })
112+
queue.push({ key: 'b', query: null, options: {} }, 100)
113113
expect(queue.timeMs).toBe(100)
114114
})
115115
})

packages/nuqs/src/lib/queues/throttle.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export type UpdateQueuePushArgs = {
2020
key: string
2121
query: string | null
2222
options: AdapterOptions & Pick<Options, 'startTransition'>
23-
timeMs?: number
2423
}
2524

2625
function getSearchParamsSnapshotFromLocation() {
@@ -39,12 +38,10 @@ export class ThrottledQueue {
3938
resolvers: Resolvers<URLSearchParams> | null = null
4039
lastFlushedAt = 0
4140

42-
push({
43-
key,
44-
query,
45-
options,
41+
push(
42+
{ key, query, options }: UpdateQueuePushArgs,
4643
timeMs = defaultRateLimit.timeMs
47-
}: UpdateQueuePushArgs) {
44+
) {
4845
debug('[nuqs queue] Enqueueing %s=%s %O', key, query, options)
4946
// Enqueue update
5047
this.updateMap.set(key, query)

packages/nuqs/src/useQueryState.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,12 @@ export function useQueryState<T = string>(
306306
defaultRateLimit.timeMs
307307
return debounceController.push(update, timeMs, adapter)
308308
} else {
309-
update.throttleMs =
309+
const timeMs =
310310
options.limitUrlUpdates?.timeMs ??
311311
limitUrlUpdates?.timeMs ??
312312
options.throttleMs ??
313313
throttleMs
314-
globalThrottleQueue.push(update)
314+
globalThrottleQueue.push(update, timeMs)
315315
return globalThrottleQueue.flush(adapter)
316316
}
317317
},

packages/nuqs/src/useQueryStates.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,14 @@ export function useQueryStates<KeyMap extends UseQueryStatesKeysMap>(
274274
maxDebounceTime = timeMs
275275
}
276276
} else {
277-
update.throttleMs =
277+
const timeMs =
278278
callOptions?.limitUrlUpdates?.timeMs ??
279279
parser?.limitUrlUpdates?.timeMs ??
280280
limitUrlUpdates?.timeMs ??
281281
callOptions.throttleMs ??
282282
parser.throttleMs ??
283283
throttleMs
284-
globalThrottleQueue.push(update)
284+
globalThrottleQueue.push(update, timeMs)
285285
}
286286
}
287287
// We need to flush the throttle queue, but we may have a pending

0 commit comments

Comments
 (0)