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

49 lines
933 B
TypeScript
Raw Normal View History

/* tslint:disable */
import { Injectable } from '@angular/core'
2016-09-06 16:40:57 -04:00
2017-01-13 06:16:00 -05:00
export type InternalStateType = {
[key: string]: any
}
2017-01-13 06:16:00 -05:00
2016-09-06 16:40:57 -04:00
@Injectable()
export class AppState {
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() {
return this._state = this._clone(this._state)
2016-09-06 16:40:57 -04:00
}
2017-06-11 06:28:22 -04:00
/**
* Never allow mutation
*/
2017-01-13 06:16:00 -05:00
public set state(value) {
throw new Error('do not mutate the `.state` directly')
2016-09-06 16:40:57 -04:00
}
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.
*/
const state = this.state
return state.hasOwnProperty(prop) ? state[prop] : state
2016-09-06 16:40:57 -04:00
}
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.
*/
return this._state[prop] = value
2016-09-06 16:40:57 -04:00
}
2017-01-13 06:16:00 -05:00
private _clone(object: InternalStateType) {
2017-06-11 06:28:22 -04:00
/**
* Simple object clone.
*/
return JSON.parse(JSON.stringify( object ))
2016-09-06 16:40:57 -04:00
}
}