import { Injectable , OnDestroy } from '@angular/core' ; import { UserSettingsService } from './user-settings.service' ; import { NgxIndexedDBService } from 'ngx-indexed-db' ; import { UserSetting , UserSettingPayload } from '../models/user-setting' ; import { Observable , ReplaySubject , Subject } from 'rxjs' ; import { takeUntil } from 'rxjs/operators' ; import { User } from '../../models/user' ; @ Injectable ( { providedIn : 'root' } ) export class UserSettingsDBService < T = UserSetting , P = UserSettingPayload > implements OnDestroy { /**
     * Name of the store settings of the indexedDB `DATAGERRY`.
     */ private readonly storeName : string = 'user-settings' ; /**
     * Subscriber subject. When active auto set to backend db.
     */ private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; /**
     * The current login user.
     */ private currentUser : User ; /**
     * Holder subject when new settings are in the database.
     */ private newSettings : Subject < UserSetting > = new Subject < UserSetting > ( ) ; constructor ( private dbService , private userSettingsService ) { this . newSettings . asObservable ( ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ) ; this . userSettingsService . currentUserObservable . subscribe ( ( user ) => { this . currentUser = user ; if ( user ) { this . syncSettings ( ) . then ( ( ) => { console . log ( 'User settings loaded' ) ; } ) ; } } ) ; } /**
     * Syncs the indexedDB settings with database.
     */ public async syncSettings ( ) { try { this . userSettingsService . getUserSettings ( ) . subscribe ( async ( userSettings ) => { await this . dbService . clear ( this . storeName ) ; for ( const setting of userSettings ) { this . dbService . add ( this . storeName , setting ) . subscribe ( ) ; } } , error => console . error ( `Error while loading user settings: ${ error }` ) ) ; } catch ( e ) { console . error ( `Error while init user settings: ${ e }` ) ; } } /**
     * Clear the settings database.
     */ public clear ( ) { this . dbService . clear ( this . storeName ) ; } param resource public getSetting ( resource ) { return this . dbService . getByKey ( this . storeName , resource ) ; } param setting public addSetting ( setting ) { this . dbService . add ( this . storeName , setting ) . subscribe ( key => { this . userSettingsService . addUserSetting ( setting ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ) ; } ) ; } param setting public updateSetting ( setting ) { this . dbService . update ( this . storeName , setting ) . subscribe ( key => { this . userSettingsService . updateUserSetting ( setting . resource , setting ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ) ; } ) ; } param id public deleteSetting ( id ) { this . dbService . delete ( this . storeName , id ) . subscribe ( key => { this . userSettingsService . deleteUserSetting ( id ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ) ; } ) ; } /**
     * Auto unsubscribe when service is destroyed.
     */ public ngOnDestroy ( ) { this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } }