import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs' ; import { BgsHeroStat } from '../../models/battlegrounds/stats/bgs-hero-stat' ; import { MainWindowStoreEvent } from '../mainwindow/store/events/main-window-store-event' ; import { OverwolfService } from '../overwolf.service' ; import { AppUiStoreService , BattlegroundsStateSelector , GameStateSelector , MercenariesHighlightsSelector , MercenariesOutOfCombatStateSelector , MercenariesStateSelector , PrefsSelector , Selector , } from './app-ui-store.service' ; @ Injectable ( ) export class AppUiStoreFacadeService { private store : AppUiStoreService ; constructor ( private readonly ow ) { this . init ( ) ; } private async init ( ) { this . store = this . ow . getMainWindow ( ) ?. appStore ; if ( ! this . store ) { console . warn ( 'could not retrieve store from main window' ) ; setTimeout ( ( ) => this . init ( ) , 10 ) ; } } public async initComplete ( ) { await this . waitForStoreInstance ( ) ; return this . store . initComplete ( ) ; } public listen$ < S extends Selector < any > [ ] > ( ... selectors ) { return this . debugObservable ( this . store . listen$ ( ... selectors ) ) ; } public listenPrefs$ < S extends PrefsSelector < any > [ ] > ( ... selectors ) { return this . debugObservable ( this . store . listenPrefs$ ( ... selectors ) ) ; } public listenDeckState$ < S extends GameStateSelector < any > [ ] > ( ... selectors ) { return this . debugObservable ( this . store . listenDeckState$ ( ... selectors ) ) ; } public listenBattlegrounds$ < S extends BattlegroundsStateSelector < any > [ ] > ( ... selectors ) { return this . debugObservable ( this . store . listenBattlegrounds$ ( ... selectors ) ) ; } public listenMercenaries$ < S extends MercenariesStateSelector < any > [ ] > ( ... selectors ) { return this . debugObservable ( this . store . listenMercenaries$ ( ... selectors ) ) ; } public listenMercenariesOutOfCombat$ < S extends MercenariesOutOfCombatStateSelector < any > [ ] > ( ... selectors ) { return this . debugObservable ( this . store . listenMercenariesOutOfCombat$ ( ... selectors ) ) ; } public listenMercenariesHighlights$ < S extends MercenariesHighlightsSelector < any > [ ] > ( ... selectors ) { return this . debugObservable ( this . store . listenMercenariesHighlights$ ( ... selectors ) ) ; } public bgHeroStats$ ( ) { return this . debugObservable ( this . store . bgHeroStats$ ( ) ) ; } public send ( event ) { return this . store . send ( event ) ; } private debugObservable < T > ( obs ) { if ( process . env . NODE_ENV === 'production' ) { return obs ; } obs . subscribe = this . overrideSubscribe ( obs , obs . subscribe , new Error ( ) , this ) ; return obs ; } private overrideSubscribe ( obs , oldSubscribe , error , obj ) { return function ( ) { const result = oldSubscribe . apply ( obs , arguments ) ; result [ 'hop' ] = error . stack . split ( '\n' ) ; result [ 'hopContext' ] = obj ; result [ 'hopArgs' ] = arguments ; return result ; } ; } private async waitForStoreInstance ( ) { return new Promise < void > ( ( resolve ) => { const dbWait = ( ) => { if ( this . store ) { resolve ( ) ; } else { setTimeout ( ( ) => dbWait ( ) , 10 ) ; } } ; dbWait ( ) ; } ) ; } }