Skip to content

Commit 81531f2

Browse files
author
Isaque
authored
feat: Add adapter level options (#27)
* feat(adapters): add adapter level options * fix(adapters/sveltekit): update options in SvelteKit adapter to be a getter
1 parent 06ebca9 commit 81531f2

5 files changed

Lines changed: 46 additions & 25 deletions

File tree

packages/nuqs-svelte/src/lib/adapters/svelte-kit/adapter.svelte

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,28 @@
33
import { goto, pushState, replaceState } from "$app/navigation";
44
import { page } from "$app/state";
55
import type { Snippet } from "svelte";
6-
import type { AdapterInterface } from "../types";
6+
import type { AdapterInterface, AdapterOptions } from "../types";
77
import NuqsContext from "../NuqsContext.svelte";
88
import { renderQueryString } from "$lib/url-encoding";
99
import { SvelteURL } from "svelte/reactivity";
1010
1111
type Props = {
1212
children?: Snippet;
13+
/**
14+
* Options defined at adapter level take precedence over options setted at hook (`useQueryState`) level.
15+
*/
16+
options?: AdapterOptions;
1317
};
1418
15-
let { children }: Props = $props();
19+
let { children, options }: Props = $props();
1620
1721
// Using page.url directly will update the URL without invalidating the loaders dependent on it.
1822
let url = $derived(new SvelteURL(page.url));
1923
2024
const adapter: AdapterInterface = {
25+
get options() {
26+
return options;
27+
},
2128
updateUrl: (search, options) => {
2229
const { history, scroll, shallow } = options;
2330

packages/nuqs-svelte/src/lib/adapters/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Options } from "$lib/types";
22

3-
export type AdapterOptions = Pick<Options, "history" | "scroll" | "shallow">;
3+
export type AdapterOptions = Pick<Options, "history" | "scroll" | "shallow" | "throttleMs">;
44

55
export type UpdateUrlFunction = (
66
search: URLSearchParams,
@@ -18,4 +18,5 @@ export type AdapterInterface = {
1818
updateUrl: UpdateUrlFunction;
1919
getSearchParamsSnapshot?: () => URLSearchParams;
2020
rateLimitFactor?: number;
21+
options?: AdapterOptions;
2122
};

packages/nuqs-svelte/src/lib/update-queue.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ function flushUpdateQueue({
152152
history: options.history,
153153
scroll: options.scroll,
154154
shallow: options.shallow,
155+
throttleMs: options.throttleMs,
155156
});
156157
});
157158
return [search, null];

packages/nuqs-svelte/src/lib/use-query-state.svelte.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ export function useQueryState(key: string): UseQueryStateReturn<string, undefine
202202
export function useQueryState<T = string>(
203203
key: string,
204204
{
205-
history = "replace",
206-
shallow = true,
207-
scroll = false,
208-
throttleMs = FLUSH_RATE_LIMIT_MS,
205+
history,
206+
shallow,
207+
scroll,
208+
throttleMs,
209209
parse = (x) => x as unknown as T,
210210
serialize = String,
211211
eq = (a, b) => a === b,
@@ -214,10 +214,6 @@ export function useQueryState<T = string>(
214214
}: Partial<UseQueryStateOptions<T>> & {
215215
defaultValue?: T;
216216
} = {
217-
history: "replace",
218-
scroll: false,
219-
shallow: true,
220-
throttleMs: 50,
221217
parse: (x) => x as unknown as T,
222218
serialize: String,
223219
eq: (a, b) => a === b,
@@ -306,12 +302,15 @@ export function useQueryState<T = string>(
306302
newValue = null;
307303
}
308304

305+
const adapterOptions = adapter.options;
306+
309307
const query = enqueueQueryStringUpdate(key, newValue, serialize, {
310-
// Call-level options take precedence over hook declaration options
311-
history: options.history ?? history,
312-
shallow: options.shallow ?? shallow,
313-
scroll: options.scroll ?? scroll,
314-
throttleMs: options.throttleMs ?? throttleMs,
308+
// Call-level options take precedence over hook declaration options that take precedence over adapter-level options
309+
history: options.history ?? history ?? adapterOptions?.history ?? "replace",
310+
shallow: options.shallow ?? shallow ?? adapterOptions?.shallow ?? true,
311+
scroll: options.scroll ?? scroll ?? adapterOptions?.scroll ?? false,
312+
throttleMs:
313+
options.throttleMs ?? throttleMs ?? adapterOptions?.throttleMs ?? FLUSH_RATE_LIMIT_MS,
315314
});
316315

317316
emitter.emit(key, { state: newValue, query });

packages/nuqs-svelte/src/lib/use-query-states.svelte.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ const defaultUrlKeys = {};
8989
export function useQueryStates<KeyMap extends UseQueryStatesKeysMap>(
9090
keyMap: KeyMap,
9191
{
92-
history = "replace",
93-
scroll = false,
94-
shallow = true,
95-
throttleMs = FLUSH_RATE_LIMIT_MS,
92+
history,
93+
scroll,
94+
shallow,
95+
throttleMs,
9696
clearOnDefault = true,
9797
urlKeys = defaultUrlKeys,
9898
}: Partial<UseQueryStatesOptions<KeyMap>> = {},
@@ -228,13 +228,26 @@ export function useQueryStates<KeyMap extends UseQueryStatesKeysMap>(
228228
) {
229229
value = null;
230230
}
231+
232+
const adapterOptions = adapter.options;
233+
231234
const query = enqueueQueryStringUpdate(urlKey, value, parser.serialize ?? String, {
232235
// Call-level options take precedence over individual parser options
233-
// which take precedence over global options
234-
history: callOptions.history ?? parser.history ?? history,
235-
shallow: callOptions.shallow ?? parser.shallow ?? shallow,
236-
scroll: callOptions.scroll ?? parser.scroll ?? scroll,
237-
throttleMs: callOptions.throttleMs ?? parser.throttleMs ?? throttleMs,
236+
// which take precedence over global options that take precedence over adapter options
237+
history:
238+
callOptions.history ?? parser.history ?? history ?? adapterOptions?.history ?? "replace",
239+
240+
shallow:
241+
callOptions.shallow ?? parser.shallow ?? shallow ?? adapterOptions?.shallow ?? true,
242+
243+
scroll: callOptions.scroll ?? parser.scroll ?? scroll ?? adapterOptions?.scroll ?? false,
244+
245+
throttleMs:
246+
callOptions.throttleMs ??
247+
parser.throttleMs ??
248+
throttleMs ??
249+
adapterOptions?.throttleMs ??
250+
FLUSH_RATE_LIMIT_MS,
238251
});
239252
emitter.emit(urlKey, { state: value, query });
240253
}

0 commit comments

Comments
 (0)