import { buildRegFn } from './buildRegFn' ; import { useCallback , useLayoutEffect , useState } from 'react' ; export interface StorageObject { /**
     * NOTICE: 与save不同， set存储 1 天
     */ set : ( key : string , data : any ) => Promise < void > ; get : ( key : string , defaultVal ? : any ) => Promise < any > ; remove : ( key : string ) => Promise < void > ; save : ( key : string , data : any ) => Promise < void > ; } /**
 * 持久化存储相关逻辑
 */ export const [ getStorage , setStorage ] = buildRegFn < ( ) => StorageObject > ( 'storage' ) ; param key param defaultValue export function useStorage < T = any > ( key , defaultValue ? ) { const [ value , setValue ] = useState < T | undefined > ( defaultValue ) ; useLayoutEffect ( ( ) => { getStorage ( ) . get ( key ) . then ( ( data ) => setValue ( data ?? defaultValue ) ) ; } , [ key ] ) ; const set = useCallback ( ( newVal ) => { setValue ( newVal ) ; getStorage ( ) . set ( key , newVal ) ; } , [ key ] ) ; const save = useCallback ( ( newVal ) => { setValue ( newVal ) ; getStorage ( ) . save ( key , newVal ) ; } , [ key ] ) ; return [ value , { set , save } ] ; }