1 import { Injectable } from '@angular/core';
3 export type InternalStateType = {
8 export class AppState {
10 public _state: InternalStateType = { };
13 * Already return a clone of the current state.
16 return this._state = this._clone(this._state);
19 * Never allow mutation
21 public set state(value) {
22 throw new Error('do not mutate the `.state` directly');
25 public get(prop?: any) {
27 * Use our state getter for the clone.
29 const state = this.state;
30 return state.hasOwnProperty(prop) ? state[prop] : state;
33 public set(prop: string, value: any) {
35 * Internally mutate our state.
37 return this._state[prop] = value;
40 private _clone(object: InternalStateType) {
42 * Simple object clone.
44 return JSON.parse(JSON.stringify( object ));