]>
Commit | Line | Data |
---|---|---|
1 | /* tslint:disable */ | |
2 | ||
3 | import { Injectable } from '@angular/core' | |
4 | ||
5 | export type InternalStateType = { | |
6 | [key: string]: any | |
7 | } | |
8 | ||
9 | @Injectable() | |
10 | export class AppState { | |
11 | ||
12 | public _state: InternalStateType = { } | |
13 | ||
14 | /** | |
15 | * Already return a clone of the current state. | |
16 | */ | |
17 | public get state() { | |
18 | return this._state = this._clone(this._state) | |
19 | } | |
20 | /** | |
21 | * Never allow mutation | |
22 | */ | |
23 | public set state(value) { | |
24 | throw new Error('do not mutate the `.state` directly') | |
25 | } | |
26 | ||
27 | public get(prop?: any) { | |
28 | /** | |
29 | * Use our state getter for the clone. | |
30 | */ | |
31 | const state = this.state | |
32 | return state.hasOwnProperty(prop) ? state[prop] : state | |
33 | } | |
34 | ||
35 | public set(prop: string, value: any) { | |
36 | /** | |
37 | * Internally mutate our state. | |
38 | */ | |
39 | return this._state[prop] = value | |
40 | } | |
41 | ||
42 | private _clone(object: InternalStateType) { | |
43 | /** | |
44 | * Simple object clone. | |
45 | */ | |
46 | return JSON.parse(JSON.stringify( object )) | |
47 | } | |
48 | } |