import { atom } from '.' ; import { Atom , Updater } from '..' ; type Verifier < T > = ( v : unknown ) => v is T ; const getInitialItem = < T > ( defaultValue , key , verifier ? ) => { const storedItem = localStorage . getItem ( key ) ; if ( storedItem === null ) { return defaultValue ; } try { const parsedItem = JSON . parse ( storedItem ) ; if ( ! verifier || verifier ( parsedItem ) ) { return parsedItem ; } } catch ( err ) { } return defaultValue ; } ; export const localStorageAtom = < T > ( initialValue , key , verifyItem ? ) : Atom < T > => { const initialItem = getInitialItem ( initialValue , key , verifyItem ) ; const baseAtom = atom ( initialItem ) ; const derivedAtom = atom ( get => get ( baseAtom ) , ( producer ) => { const newValue = producer instanceof Function ? producer ( baseAtom . getValue ( ) ) : producer ; baseAtom . update ( newValue ) ; localStorage . setItem ( key , JSON . stringify ( newValue ) ) ; } ) ; return derivedAtom ; } ;