import { NonNullableRepository } from "ddd-base" ; import { App , AppJSON , AppSerializer } from "../../domain/App/App" ; import { createStorageInstance } from "./Storage" ; export class AppRepository extends NonNullableRepository < App > { storage : LocalForage ; constructor ( protected initialEntity ) { super ( initialEntity ) ; this . storage = createStorageInstance ( { name : "AppRepository" } ) ; } returns { Promise < any > } async ready ( ) { if ( this . map . size > 0 ) { return Promise . resolve ( this ) ; } await this . storage . ready ( ) ; const values = [ ] ; await this . storage . iterate ( ( value ) => { values . push ( value as AppJSON ) ; } ) ; values . map ( ( json ) => { try { return AppSerializer . fromJSON ( json ) ; } catch ( error ) { this . storage . removeItem ( json . id ) ; return this . initialEntity ; } } ) . forEach ( ( app ) => { this . save ( app ) ; } ) ; return this ; } save ( entity ) { super . save ( entity ) ; return this . storage . setItem ( entity . id . toValue ( ) , AppSerializer . toJSON ( entity ) ) ; } clear ( ) { super . clear ( ) ; return this . storage . clear ( ) ; } }