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