]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/app.service.ts
9b582e472a07d1fdf69f1c61558b5666cc1a4471
[github/Chocobozzz/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 // already return a clone of the current state
13 public get state() {
14 return this._state = this._clone(this._state);
15 }
16 // never allow mutation
17 public set state(value) {
18 throw new Error('do not mutate the `.state` directly');
19 }
20
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;
25 }
26
27 public set(prop: string, value: any) {
28 // internally mutate our state
29 return this._state[prop] = value;
30 }
31
32 private _clone(object: InternalStateType) {
33 // simple object clone
34 return JSON.parse(JSON.stringify( object ));
35 }
36 }