import angular from 'angular' ; import autobind from 'autobind-decorator' ; import { StoreKey } from './store.enum' ; import { StoreContent } from './store.interface' ; @ autobind export abstract class StoreService { $q : ng . IQService ; dbName = 'xbs-store' ; storeName = 'xbs' ; constructor ( $q ) { this . $q = $q ; } protected abstract clear ( ) ; get < T = StoreContent > ( keys ? ) { return ( angular . isUndefined ( keys ?? undefined ) ? this . keys ( ) : this . $q . resolve ( keys ) ) . then ( ( allKeys ) => { const keysArr = Array . isArray ( allKeys ) ? ( allKeys as string [ ] ) : [ allKeys ] ; return this . getFromStore ( keysArr ) . then ( ( keyValues ) => { return keysArr . reduce ( ( prev , current , index ) => { const next = angular . copy ( prev ) ; next [ current as string ] = keyValues [ index ] ; return next ; } , { } as T ) ; } ) . then ( ( storeContent ) => { if ( storeContent && Object . keys ( storeContent ) . length === 1 ) { return storeContent [ keysArr [ 0 ] as string ] ; } return storeContent ; } ) ; } ) ; } protected abstract getFromStore < T = StoreContent > ( keys ) ; init ( ) { return this . clear ( ) . then ( ( ) => { return this . $q . all ( [ this . setInStore ( StoreKey . AlternateSearchBarPosition , false ) , this . setInStore ( StoreKey . AutoFetchMetadata , true ) , this . setInStore ( StoreKey . CheckForAppUpdates , true ) , this . setInStore ( StoreKey . DarkModeEnabled , false ) , this . setInStore ( StoreKey . DefaultToFolderView , false ) , this . setInStore ( StoreKey . DisplayHelp , true ) , this . setInStore ( StoreKey . DisplayOtherSyncsWarning , false ) , this . setInStore ( StoreKey . DisplayPermissions , false ) , this . setInStore ( StoreKey . DisplayUpdated , false ) , this . setInStore ( StoreKey . SyncBookmarksToolbar , false ) , this . setInStore ( StoreKey . SyncEnabled , false ) ] ) ; } ) . then ( ( ) => { } ) ; } protected abstract keys ( ) ; remove ( keys ) { const keysArr = Array . isArray ( keys ) ? keys : [ keys ] ; return this . removeFromStore ( keysArr ) ; } protected abstract removeFromStore ( keys ) ; set ( key , value ? ) { if ( angular . isUndefined ( key ?? undefined ) ) { return this . $q . resolve ( ) ; } return this . $q ( ( resolve , reject ) => { ( angular . isUndefined ( value ?? undefined ) ? this . removeFromStore ( Array . isArray ( key ) ? key : [ key ] ) : this . setInStore ( key , value ) ) . then ( resolve ) . catch ( reject ) ; } ) ; } protected abstract setInStore ( key , value ) ; }