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