1- /* eslint-disable @typescript-eslint/no-explicit-any */
21import { browser , building } from '$app/environment' ;
32import { goto } from '$app/navigation' ;
43import { page as page_store } from '$app/stores' ;
@@ -12,10 +11,7 @@ import {
1211 type Readable ,
1312 readable ,
1413} from 'svelte/store' ;
15- import {
16- compressToEncodedURIComponent ,
17- decompressFromEncodedURIComponent ,
18- } from './lz-string/index.js' ;
14+ import type { EncodeAndDecodeOptions , StoreOptions } from './types' ;
1915
2016// during building we fake the page store with an URL with no search params
2117// as it should be during prerendering. This allow the application to still build
@@ -43,22 +39,6 @@ const GOTO_OPTIONS_PUSH = {
4339 replaceState : false ,
4440} ;
4541
46- export type EncodeAndDecodeOptions < T = any > = {
47- encode : ( value : T ) => string | undefined ;
48- decode : ( value : string | null ) => T | null ;
49- defaultValue ?: T ;
50- } ;
51-
52- export type StoreOptions < T > = {
53- debounceHistory ?: number ;
54- pushHistory ?: boolean ;
55- sort ?: boolean ;
56- showDefaults ?: boolean ;
57- equalityFn ?: T extends object
58- ? ( current : T | null , next : T | null ) => boolean
59- : never ;
60- } ;
61-
6242type LooseAutocomplete < T > = {
6343 [ K in keyof T ] : T [ K ] ;
6444} & {
@@ -142,121 +122,11 @@ function isComplexEqual<T>(
142122 ) ;
143123}
144124
145- function primitiveEncodeAndDecodeOptions < T > ( {
146- encode,
147- decode,
148- } : {
149- encode : ( value : T ) => string | undefined ;
150- decode : ( value : string | null ) => T | null ;
151- } ) {
152- function ssp (
153- defaultValue : T ,
154- ) : EncodeAndDecodeOptions < T > & { defaultValue : T } ;
155- function ssp ( ) : EncodeAndDecodeOptions < T > & { defaultValue : undefined } ;
156- function ssp ( defaultValue ?: T ) : EncodeAndDecodeOptions < T > {
157- return { encode, decode, defaultValue } ;
158- }
159- return ssp ;
160- }
161-
162- function objectEncodeAndDecodeOptions < T extends object = any > (
163- defaultValue : T ,
164- ) : EncodeAndDecodeOptions < T > & { defaultValue : T } ;
165- function objectEncodeAndDecodeOptions <
166- T extends object = any ,
167- > ( ) : EncodeAndDecodeOptions < T > & { defaultValue : undefined } ;
168- function objectEncodeAndDecodeOptions < T extends object = any > (
169- defaultValue ?: T ,
170- ) : EncodeAndDecodeOptions < T > {
171- return {
172- encode : ( value : T ) => JSON . stringify ( value ) ,
173- decode : ( value : string | null ) : T | null => {
174- if ( value === null ) return null ;
175- try {
176- return JSON . parse ( value ) ;
177- } catch {
178- return null ;
179- }
180- } ,
181- defaultValue,
182- } ;
183- }
184-
185- function arrayEncodeAndDecodeOptions < T = any > (
186- defaultValue : T [ ] ,
187- ) : EncodeAndDecodeOptions < T [ ] > & { defaultValue : T [ ] } ;
188- function arrayEncodeAndDecodeOptions < T = any > ( ) : EncodeAndDecodeOptions < T [ ] > & {
189- defaultValue : undefined ;
190- } ;
191- function arrayEncodeAndDecodeOptions < T = any > (
192- defaultValue ?: T [ ] ,
193- ) : EncodeAndDecodeOptions < T [ ] > {
194- return {
195- encode : ( value : T [ ] ) => JSON . stringify ( value ) ,
196- decode : ( value : string | null ) : T [ ] | null => {
197- if ( value === null ) return null ;
198- try {
199- return JSON . parse ( value ) ;
200- } catch {
201- return null ;
202- }
203- } ,
204- defaultValue,
205- } ;
206- }
207-
208- function lzEncodeAndDecodeOptions < T = any > (
209- defaultValue : T ,
210- ) : EncodeAndDecodeOptions < T > & { defaultValue : T } ;
211- function lzEncodeAndDecodeOptions < T = any > ( ) : EncodeAndDecodeOptions < T > & {
212- defaultValue : undefined ;
213- } ;
214- function lzEncodeAndDecodeOptions < T = any > (
215- defaultValue ?: T ,
216- ) : EncodeAndDecodeOptions < T > {
217- return {
218- encode : ( value : T ) =>
219- compressToEncodedURIComponent ( JSON . stringify ( value ) ) ,
220- decode : ( value : string | null ) : T | null => {
221- if ( ! value ) return null ;
222- try {
223- return JSON . parse (
224- decompressFromEncodedURIComponent ( value ) ?? '' ,
225- ) ;
226- } catch {
227- return null ;
228- }
229- } ,
230- defaultValue,
231- } ;
232- }
233-
234- export const ssp = {
235- number : primitiveEncodeAndDecodeOptions ( {
236- encode : ( value : number ) => value . toString ( ) ,
237- decode : ( value : string | null ) => ( value ? parseFloat ( value ) : null ) ,
238- } ) ,
239- boolean : primitiveEncodeAndDecodeOptions ( {
240- encode : ( value : boolean ) => value + '' ,
241- decode : ( value : string | null ) => value !== null && value !== 'false' ,
242- } ) ,
243- string : primitiveEncodeAndDecodeOptions ( {
244- encode : ( value : string | null ) => value ?? '' ,
245- decode : ( value : string | null ) => value ,
246- } ) ,
247- object : objectEncodeAndDecodeOptions ,
248- array : arrayEncodeAndDecodeOptions ,
249- lz : lzEncodeAndDecodeOptions ,
250- } satisfies Record <
251- string ,
252- < T = any > ( defaultValue ?: T ) => EncodeAndDecodeOptions < T >
253- > ;
254-
255125type SetTimeout = ReturnType < typeof setTimeout > ;
256126
257127const batchedUpdates = new Set < ( query : URLSearchParams ) => void > ( ) ;
258128
259- let batchTimeout : number ;
129+ let batchTimeout : ReturnType < typeof setTimeout > ;
260130
261131const debouncedTimeouts = new Map < string , SetTimeout > ( ) ;
262132
0 commit comments