1
0
Fork 0
peertube/client/src/app/app.service.ts

47 lines
920 B
TypeScript
Raw Normal View History

2016-09-06 16:40:57 -04:00
import { Injectable } from '@angular/core';
2017-01-13 06:16:00 -05:00
export type InternalStateType = {
[key: string]: any
};
2016-09-06 16:40:57 -04:00
@Injectable()
export class AppState {
2017-01-13 06:16:00 -05:00
public _state: InternalStateType = { };
2016-09-06 16:40:57 -04:00
2017-06-11 06:28:22 -04:00
/**
* Already return a clone of the current state.
*/
2017-01-13 06:16:00 -05:00
public get state() {
2016-09-06 16:40:57 -04:00
return this._state = this._clone(this._state);
}
2017-06-11 06:28:22 -04:00
/**
* Never allow mutation
*/
2017-01-13 06:16:00 -05:00
public set state(value) {
2016-09-06 16:40:57 -04:00
throw new Error('do not mutate the `.state` directly');
}
2017-01-13 06:16:00 -05:00
public get(prop?: any) {
2017-06-11 06:28:22 -04:00
/**
* Use our state getter for the clone.
*/
2016-09-06 16:40:57 -04:00
const state = this.state;
return state.hasOwnProperty(prop) ? state[prop] : state;
}
2017-01-13 06:16:00 -05:00
public set(prop: string, value: any) {
2017-06-11 06:28:22 -04:00
/**
* Internally mutate our state.
*/
2016-09-06 16:40:57 -04:00
return this._state[prop] = value;
}
2017-01-13 06:16:00 -05:00
private _clone(object: InternalStateType) {
2017-06-11 06:28:22 -04:00
/**
* Simple object clone.
*/
2016-09-06 16:40:57 -04:00
return JSON.parse(JSON.stringify( object ));
}
}