import { Component , ViewEncapsulation , ChangeDetectionStrategy , OnInit , OnDestroy } from 'angular2/core' ; import { Observable } from 'rxjs/Observable' ; import { Subject } from 'rxjs/Subject' ; import { Subscription } from 'rxjs/Subscription' ; import { Store } from '@ngrx/store' ; import { SagaRunner } from 'store-saga' ; import { IAuth } from './_auth/interfaces/IAuth' ; import { APP_SAGAS } from './sagas/sagas' ; import { Ng2Permission } from './services/Ng2Permission' ; import { checkAuth , logout } from './_auth/services/auth-actions.service' ; import { isAuthenticated$ , getAuth$ } from './_auth/services/auth-selectors.service' ; import { NotificationsContainer } from './containers/notifications.container/notifications.container' ; import { START_NOTIFICATIONS } from './reducers/notifications.reducer' ; @ Component ( { selector : 'app' , styles : [ require ( './styles/app.scss' ) ] , template : require ( './app.component.html' ) , directives : [ NotificationsContainer ] , encapsulation : ViewEncapsulation . None , changeDetection : ChangeDetectionStrategy . OnPush } ) export class AppComponent implements OnInit { public isAuthenticated$ : Observable < boolean > = this . _store . let ( isAuthenticated$ ( ) ) ; public userData$ : Observable < IAuth > = this . _store . let ( getAuth$ ( ) ) ; public onLogout$ = new Subject < any > ( ) ; public onInit$ = new Subject < any > ( ) ; public logoutAction$ = this . onLogout$ . map ( logout ( ) ) ; public initAction$ = this . onInit$ . map ( checkAuth ( ) ) ; private _subscription : Subscription ; constructor ( public permission , private _runner , private _store ) { permission . define ( 'user' , ( ) => { return _store . let ( isAuthenticated$ ( ) ) ; } ) ; APP_SAGAS . forEach ( saga$ => this . _runner . run ( saga$ ) ) ; _store . dispatch ( { type : START_NOTIFICATIONS } ) ; } ngOnInit ( ) { this . _subscription = Observable . merge ( this . logoutAction$ , this . initAction$ ) . subscribe ( this . _store ) ; this . onInit$ . next ( null ) ; } }