From 8c83ffc19eee6c58ce771611b0714e6ed2d46dd2 Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Sun, 29 Jan 2023 22:42:59 +0100 Subject: [PATCH] Remove shameful hack for load function Fix it the proper way with await --- package.json | 2 +- src/lib/sveltekit-search-params.ts | 38 ++++++++---------------------- 2 files changed, 11 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index ec3b10d..fc0fe7a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sveltekit-search-params", - "version": "1.0.3", + "version": "1.0.4", "repository": "git+https://github.com/paoloricciuti/sveltekit-search-params.git", "author": "Paolo Ricciuti", "license": "MIT", diff --git a/src/lib/sveltekit-search-params.ts b/src/lib/sveltekit-search-params.ts index 713fc7c..d2639e3 100644 --- a/src/lib/sveltekit-search-params.ts +++ b/src/lib/sveltekit-search-params.ts @@ -1,10 +1,10 @@ /* eslint-disable @typescript-eslint/no-empty-function */ /* eslint-disable @typescript-eslint/no-explicit-any */ -import { writable, get, type Writable, type Updater } from 'svelte/store'; -import { goto, afterNavigate } from '$app/navigation'; -import { page } from '$app/stores'; import { browser } from '$app/environment'; -import { decompressFromEncodedURIComponent, compressToEncodedURIComponent } from "./lz-string/index.js"; +import { goto } from '$app/navigation'; +import { page } from '$app/stores'; +import { get, writable, type Updater, type Writable } from 'svelte/store'; +import { compressToEncodedURIComponent, decompressFromEncodedURIComponent } from "./lz-string/index.js"; // eslint-disable-next-line @typescript-eslint/no-unused-vars function noop(value: T) { } @@ -143,12 +143,6 @@ export function queryParameters( pushHistory = true, }: StoreOptions = {}, ): Writable> { - let pushHistoryCb: (() => void) | null = null; - afterNavigate(() => { - if (pushHistoryCb) { - pushHistoryCb(); - } - }); const { set: _set, subscribe } = writable>(); const setRef: { value: Writable["set"]; } = { value: noop }; const unsubPage = page.subscribe(($page) => { @@ -171,18 +165,15 @@ export function queryParameters( }; batchedUpdates.add(toBatch); clearTimeout(batchTimeout); - batchTimeout = setTimeout(() => { + batchTimeout = setTimeout(async () => { batchedUpdates.forEach((batched) => { batched(query); }); - goto(`?${query}`, GOTO_OPTIONS); clearTimeout(debouncedTimeouts.get("queryParameters")); + await goto(`?${query}`, GOTO_OPTIONS); if (pushHistory) { debouncedTimeouts.set("queryParameters", setTimeout(() => { - pushHistoryCb = () => { - goto("", GOTO_OPTIONS_PUSH); - pushHistoryCb = null; - }; + goto("", GOTO_OPTIONS_PUSH); }, debounceHistory)); } batchedUpdates.clear(); @@ -233,12 +224,6 @@ export function queryParam( pushHistory = true, }: StoreOptions = {} ): Writable { - let pushHistoryCb: (() => void) | null = null; - afterNavigate(() => { - if (pushHistoryCb) { - pushHistoryCb(); - } - }); const { set: _set, subscribe } = writable(); const setRef: { value: Writable["set"]; } = { value: noop }; const unsubPage = page.subscribe(($page) => { @@ -254,18 +239,15 @@ export function queryParam( batchedUpdates.add(toBatch); clearTimeout(batchTimeout); const query = new URLSearchParams($page.url.searchParams); - batchTimeout = setTimeout(() => { + batchTimeout = setTimeout(async () => { batchedUpdates.forEach((batched) => { batched(query); }); - goto(`?${query}`, GOTO_OPTIONS); clearTimeout(debouncedTimeouts.get(name)); + await goto(`?${query}`, GOTO_OPTIONS); if (pushHistory) { debouncedTimeouts.set(name, setTimeout(() => { - pushHistoryCb = () => { - goto("", GOTO_OPTIONS_PUSH); - pushHistoryCb = null; - }; + goto("", GOTO_OPTIONS_PUSH); }, debounceHistory)); } batchedUpdates.clear();