1- import type { Setter , Signal } from "solid-js" ;
1+ import type { Accessor , Setter , Signal } from "solid-js" ;
22import { createUniqueId , untrack } from "solid-js" ;
33import { isServer , isDev } from "solid-js/web" ;
44import type { SetStoreFunction , Store } from "solid-js/store" ;
@@ -73,6 +73,10 @@ export type PersistenceOptions<T, O extends Record<string, any> | undefined> = {
7373export type SignalType < S extends Signal < any > | [ Store < any > , SetStoreFunction < any > ] > =
7474 S extends Signal < infer T > ? T : S extends [ Store < infer T > , SetStoreFunction < infer T > ] ? T : never ;
7575
76+ export type EnhancedSignalOrStore < T > =
77+ | [ get : Accessor < T > , set : Setter < T > , init : Promise < string > | string | null ]
78+ | [ get : Store < T > , set : SetStoreFunction < T > , init : Promise < string > | string | null ] ;
79+
7680/**
7781 * Persists a signal, store or similar API
7882 * ```ts
@@ -91,31 +95,35 @@ export type SignalType<S extends Signal<any> | [Store<any>, SetStoreFunction<any
9195 *
9296 * @param {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>] } signal - The signal or store to be persisted.
9397 * @param {PersistenceOptions<T, O> } options - The options for persistence.
94- * @returns {Signal <T> | [get: Store<T>, set: SetStoreFunction<T>] } - The persisted signal or store.
98+ * @returns {EnhancedSignalOrStore <T> } - The persisted signal or store.
9599 */
96100export function makePersisted < S extends Signal < any > | [ Store < any > , SetStoreFunction < any > ] > (
97101 signal : S ,
98102 options ?: PersistenceOptions < SignalType < S > , undefined > ,
99- ) : S ;
103+ ) : [ S [ 0 ] , S [ 1 ] , init : Promise < string > | string | null ] ;
100104export function makePersisted <
101105 S extends Signal < any > | [ Store < any > , SetStoreFunction < any > ] ,
102106 O extends Record < string , any > ,
103- > ( signal : S , options : PersistenceOptions < SignalType < S > , O > ) : S ;
107+ > (
108+ signal : S ,
109+ options : PersistenceOptions < SignalType < S > , O > ,
110+ ) : [ S [ 0 ] , S [ 1 ] , init : Promise < string > | string | null ] ;
104111export function makePersisted <
105112 S extends Signal < any > | [ Store < any > , SetStoreFunction < any > ] ,
106113 O extends Record < string , any > | undefined ,
107114 T = SignalType < S > ,
108- > ( signal : S , options : PersistenceOptions < T , O > = { } as PersistenceOptions < T , O > ) : S {
109- const storage = options . storage || globalThis . localStorage ;
115+ > (
116+ signal : S ,
117+ options : PersistenceOptions < T , O > = { } as PersistenceOptions < T , O > ,
118+ ) : [ S [ 0 ] , S [ 1 ] , init : Promise < string > | string | null ] {
119+ const storage = options . storage || ( globalThis . localStorage as Storage | undefined ) ;
110120 const name = options . name || `storage-${ createUniqueId ( ) } ` ;
111- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
112121 if ( ! storage ) {
113- return signal ;
122+ return [ signal [ 0 ] , signal [ 1 ] , null ] ;
114123 }
115124 const storageOptions = ( options as unknown as { storageOptions : O } ) . storageOptions ;
116125 const serialize : ( data : T ) => string = options . serialize || JSON . stringify . bind ( JSON ) ;
117126 const deserialize : ( data : string ) => T = options . deserialize || JSON . parse . bind ( JSON ) ;
118- // @ts -ignore
119127 const init = storage . getItem ( name , storageOptions ) ;
120128 const set =
121129 typeof signal [ 0 ] === "function"
@@ -163,9 +171,7 @@ export function makePersisted<
163171 const serialized : string | null | undefined =
164172 value != null ? ( serialize ( output ) as string ) : ( value as null | undefined ) ;
165173 options . sync ?. [ 1 ] ( name , serialized ) ;
166- // @ts -ignore
167174 if ( value != null ) storage . setItem ( name , serialized as string , storageOptions ) ;
168- // @ts -ignore
169175 else storage . removeItem ( name , storageOptions ) ;
170176 unchanged = false ;
171177 return output ;
@@ -174,11 +180,11 @@ export function makePersisted<
174180 ( signal [ 1 ] as any ) ( ...args ) ;
175181 const value = serialize ( untrack ( ( ) => signal [ 0 ] as any ) ) ;
176182 options . sync ?. [ 1 ] ( name , value ) ;
177- // @ts -ignore
178183 storage . setItem ( name , value , storageOptions ) ;
179184 unchanged = false ;
180185 } ,
181- ] as S ;
186+ init ,
187+ ] as [ S [ 0 ] , S [ 1 ] , init : Promise < string > | string | null ] ;
182188}
183189
184190/**
0 commit comments