mirror of
https://github.com/akveo/ngx-admin.git
synced 2025-12-22 10:20:14 +01:00
36 lines
728 B
TypeScript
36 lines
728 B
TypeScript
import { Injectable } from '@angular/core';
|
|
|
|
@Injectable()
|
|
export class AppState {
|
|
_state = { };
|
|
|
|
constructor() {
|
|
}
|
|
|
|
// already return a clone of the current state
|
|
get state() {
|
|
return this._state = this._clone(this._state);
|
|
}
|
|
// never allow mutation
|
|
set state(value) {
|
|
throw new Error('do not mutate the `.state` directly');
|
|
}
|
|
|
|
|
|
get(prop?: any) {
|
|
// use our state getter for the clone
|
|
const state = this.state;
|
|
return state.hasOwnProperty(prop) ? state[prop] : state;
|
|
}
|
|
|
|
set(prop: string, value: any) {
|
|
// internally mutate our state
|
|
return this._state[prop] = value;
|
|
}
|
|
|
|
|
|
_clone(object) {
|
|
// simple object clone
|
|
return JSON.parse(JSON.stringify( object ));
|
|
}
|
|
}
|