1 import { Injectable } from '@angular/core';
3 export type InternalStateType = {
8 export class AppState {
10 public _state: InternalStateType = { };
12 // already return a clone of the current state
14 return this._state = this._clone(this._state);
16 // never allow mutation
17 public set state(value) {
18 throw new Error('do not mutate the `.state` directly');
21 public get(prop?: any) {
22 // use our state getter for the clone
23 const state = this.state;
24 return state.hasOwnProperty(prop) ? state[prop] : state;
27 public set(prop: string, value: any) {
28 // internally mutate our state
29 return this._state[prop] = value;
32 private _clone(object: InternalStateType) {
33 // simple object clone
34 return JSON.parse(JSON.stringify( object ));