import { injectable } from 'tsyringe' ; import { ArianeeEventEmitter } from '../../wallet/services/arianeeEventEmitterService/ArianeeEventEmitter' ; import { Store } from './store' ; import { ConfigurationService } from '../../wallet/services/configurationService/configurationService' ; import { WalletService } from '../../wallet/services/walletService/walletService' ; @ injectable ( ) export class SimpleStore { constructor ( private store , private arianeeConfig , private walletService , private arianeeEventEmitter ) { } private cache = { } ; keyBuilder = ( namespace , key ) : string => { return `${ this . walletService . address }/${ this . arianeeConfig . arianeeConfiguration . chainId }/${ namespace }/${ key }` ; } ; public get = async < T > ( namespace , key , getter , force = false ) : Promise < T > => { const storeKey = this . keyBuilder ( namespace , key ) ; if ( ! ( await this . store . hasItem ( storeKey ) ) || force ) { if ( ! Object . prototype . hasOwnProperty . call ( this . cache , storeKey ) ) { this . cache [ storeKey ] = getter ( ) . then ( value => { this . set ( namespace , key , value ) ; return value ; } ) . finally ( ( ) => { delete this . cache [ storeKey ] ; } ) ; } return this . cache [ storeKey ] ; } else { return this . store . getStoreItem ( storeKey ) ; } } ; public set = async ( namespace , key , value ) : Promise < Store > => { const storeKey = this . keyBuilder ( namespace , key ) ; const store = await this . store . setStoreItem ( storeKey , value ) ; this . arianeeEventEmitter . EE . emit ( 'StoreChange' , { namespace , key , value } ) ; return store ; } ; }