diff options
Diffstat (limited to 'client/src/app/app.service.ts')
-rw-r--r-- | client/src/app/app.service.ts | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/client/src/app/app.service.ts b/client/src/app/app.service.ts index 033c21900..9b582e472 100644 --- a/client/src/app/app.service.ts +++ b/client/src/app/app.service.ts | |||
@@ -1,35 +1,35 @@ | |||
1 | |||
2 | import { Injectable } from '@angular/core'; | 1 | import { Injectable } from '@angular/core'; |
3 | 2 | ||
3 | export type InternalStateType = { | ||
4 | [key: string]: any | ||
5 | }; | ||
6 | |||
4 | @Injectable() | 7 | @Injectable() |
5 | export class AppState { | 8 | export class AppState { |
6 | _state = { }; | ||
7 | 9 | ||
8 | constructor() { ; } | 10 | public _state: InternalStateType = { }; |
9 | 11 | ||
10 | // already return a clone of the current state | 12 | // already return a clone of the current state |
11 | get state() { | 13 | public get state() { |
12 | return this._state = this._clone(this._state); | 14 | return this._state = this._clone(this._state); |
13 | } | 15 | } |
14 | // never allow mutation | 16 | // never allow mutation |
15 | set state(value) { | 17 | public set state(value) { |
16 | throw new Error('do not mutate the `.state` directly'); | 18 | throw new Error('do not mutate the `.state` directly'); |
17 | } | 19 | } |
18 | 20 | ||
19 | 21 | public get(prop?: any) { | |
20 | get(prop?: any) { | ||
21 | // use our state getter for the clone | 22 | // use our state getter for the clone |
22 | const state = this.state; | 23 | const state = this.state; |
23 | return state.hasOwnProperty(prop) ? state[prop] : state; | 24 | return state.hasOwnProperty(prop) ? state[prop] : state; |
24 | } | 25 | } |
25 | 26 | ||
26 | set(prop: string, value: any) { | 27 | public set(prop: string, value: any) { |
27 | // internally mutate our state | 28 | // internally mutate our state |
28 | return this._state[prop] = value; | 29 | return this._state[prop] = value; |
29 | } | 30 | } |
30 | 31 | ||
31 | 32 | private _clone(object: InternalStateType) { | |
32 | _clone(object) { | ||
33 | // simple object clone | 33 | // simple object clone |
34 | return JSON.parse(JSON.stringify( object )); | 34 | return JSON.parse(JSON.stringify( object )); |
35 | } | 35 | } |