]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/app.service.ts
Client: avoid loading javascript ressource over the network
[github/Chocobozzz/PeerTube.git] / client / src / app / app.service.ts
CommitLineData
ab32b0fc
C
1import { Injectable } from '@angular/core';
2
c16ce1de
C
3export type InternalStateType = {
4 [key: string]: any
5};
6
ab32b0fc
C
7@Injectable()
8export class AppState {
ab32b0fc 9
c16ce1de 10 public _state: InternalStateType = { };
ab32b0fc
C
11
12 // already return a clone of the current state
c16ce1de 13 public get state() {
ab32b0fc
C
14 return this._state = this._clone(this._state);
15 }
16 // never allow mutation
c16ce1de 17 public set state(value) {
ab32b0fc
C
18 throw new Error('do not mutate the `.state` directly');
19 }
20
c16ce1de 21 public get(prop?: any) {
ab32b0fc
C
22 // use our state getter for the clone
23 const state = this.state;
24 return state.hasOwnProperty(prop) ? state[prop] : state;
25 }
26
c16ce1de 27 public set(prop: string, value: any) {
ab32b0fc
C
28 // internally mutate our state
29 return this._state[prop] = value;
30 }
31
c16ce1de 32 private _clone(object: InternalStateType) {
ab32b0fc
C
33 // simple object clone
34 return JSON.parse(JSON.stringify( object ));
35 }
36}