import { Injectable } from '@angular/core' ; import { HmrState } from 'angular2-hmr' ; import { Subject } from "rxjs/Subject" ; @ Injectable ( ) export class AppState { private _state = { } ; private _stateSubject = new Subject < any > ( ) ; constructor ( ) { } get state ( ) { return this . _state = this . _clone ( this . _state ) ; } set state ( value ) { throw new Error ( 'do not mutate the `.state` directly' ) ; } get ( prop ? ) { const state = this . state ; return state . hasOwnProperty ( prop ) ? state [ prop ] : state ; } set ( prop , value ) { let newValue = this . _state [ prop ] = value ; this . _stateSubject . next ( this . state ) ; return newValue ; } get subject ( ) { return this . _stateSubject ; } _clone ( object ) { return JSON . parse ( JSON . stringify ( object ) ) ; } }