import { ChangeDetectionStrategy , Component , HostListener , NgZone , OnDestroy , OnInit } from '@angular/core' ; import { EventSubscription } from '@app/event' ; import { OWGames } from '@app/odk' ; import { SettingsFeature , SettingsWindowService } from '@layout/service' ; import { InspectWindowData , InspectWindowService } from '@modules/inspect/service' ; import { BehaviorSubject , Subscription } from 'rxjs' ; import { debounceTime , filter } from 'rxjs/operators' ; const DISPOSE_TIMEOUT = 1000 * 15 ; @ Component ( { selector : 'app-inspect-window' , templateUrl : './inspect-window.component.html' , styleUrls : [ './inspect-window.component.scss' ] , changeDetection : ChangeDetectionStrategy . OnPush } ) export class InspectWindowComponent implements OnInit , OnDestroy { private subscription : EventSubscription ; private disposeSubscription : Subscription ; public data$ = new BehaviorSubject < InspectWindowData > ( null ) ; constructor ( private readonly window , private readonly settings , private readonly ngZone ) { } @ HostListener ( 'document:keydown.escape' ) public onKeydownHandler ( ) { this . reset ( ) ; } @ HostListener ( 'window:beforeunload' ) public onWindowBeforeUnload ( ) { this . dispose ( ) ; } public ngOnInit ( ) { this . data$ . next ( this . window . data$ . get ( ) ) ; this . addSubscription ( ) ; this . addEvents ( ) ; } public ngOnDestroy ( ) { this . dispose ( ) ; } public onToggleSettings ( ) { this . settings . toggle ( SettingsFeature . Inspect ) . subscribe ( ) ; } public onSupportToggle ( ) { this . settings . toggle ( SettingsFeature . Support ) . subscribe ( ) ; } private onMouseUp = ( event ) : void => { if ( event ?. onGame ) { this . ngZone . run ( ( ) => this . reset ( ) ) ; } } ; private dispose ( ) { this . removeEvents ( ) ; this . removeSubscription ( ) ; } private reset ( ) { this . window . minimize ( ) . subscribe ( ( ) => { this . data$ . next ( null ) ; } ) ; } private addEvents ( ) { OWGames . onMouseUp . addListener ( this . onMouseUp ) ; } private removeEvents ( ) { OWGames . onMouseUp . removeListener ( this . onMouseUp ) ; } private addSubscription ( ) { this . subscription = this . window . data$ . on ( data => { this . ngZone . run ( ( ) => { if ( data ) { if ( this . data$ . value ) { this . data$ . next ( null ) ; setTimeout ( ( ) => { this . data$ . next ( data ) ; } ) ; } else { this . data$ . next ( data ) ; } } else { this . reset ( ) ; } } ) ; } ) ; this . disposeSubscription = this . data$ . pipe ( debounceTime ( DISPOSE_TIMEOUT ) , filter ( data => ! data ) ) . subscribe ( ( ) => { this . dispose ( ) ; this . window . close ( ) . subscribe ( ) ; } ) ; } private removeSubscription ( ) { this . subscription ?. unsubscribe ( ) ; this . disposeSubscription ?. unsubscribe ( ) ; } }