3 import { Injectable } from '@angular/core'
5 export type InternalStateType = {
10 export class AppState {
12 public _state: InternalStateType = { }
15 * Already return a clone of the current state.
18 return this._state = this._clone(this._state)
21 * Never allow mutation
23 public set state(value) {
24 throw new Error('do not mutate the `.state` directly')
27 public get(prop?: any) {
29 * Use our state getter for the clone.
31 const state = this.state
32 return state.hasOwnProperty(prop) ? state[prop] : state
35 public set(prop: string, value: any) {
37 * Internally mutate our state.
39 return this._state[prop] = value
42 private _clone(object: InternalStateType) {
44 * Simple object clone.
46 return JSON.parse(JSON.stringify( object ))