import { storage } from 'background/webapi' ; import { debounce } from 'debounce' ; const persistStorage = ( name , obj ) => { debounce ( storage . set ( name , obj ) , 1000 ) ; } ; interface CreatePersistStoreParams < T > { name : string ; template ? : T ; fromStorage ? : boolean ; } const createPersistStore = async < T extends object > ( { name , template = Object . create ( null ) , fromStorage = true , } ) : Promise < T > => { let tpl = template ; if ( fromStorage ) { const storageCache = await storage . get ( name ) ; tpl = storageCache || template ; if ( ! storageCache ) { await storage . set ( name , tpl ) ; } } const createProxy = < A extends object > ( obj ) : A => new Proxy ( obj , { set ( target , prop , value ) { if ( typeof value === 'object' && value !== null ) { target [ prop ] = createProxy ( value ) ; } target [ prop ] = value ; persistStorage ( name , target ) ; return true ; } , deleteProperty ( target , prop ) { if ( Reflect . has ( target , prop ) ) { Reflect . deleteProperty ( target , prop ) ; persistStorage ( name , target ) ; } return true ; } , } ) ; return createProxy < T > ( tpl ) ; } ; export default createPersistStore ;