mirror of
https://github.com/akveo/ngx-admin.git
synced 2025-12-25 19:58:50 +01:00
43 lines
1,012 B
TypeScript
43 lines
1,012 B
TypeScript
import {Injectable} from '@angular/core'
|
|
import {Subject} from 'rxjs/Subject';
|
|
|
|
@Injectable()
|
|
export class AppState {
|
|
|
|
private _data = new Subject<Object>();
|
|
private _dataStream$ = this._data.asObservable();
|
|
|
|
private _subscriptions:Map<string, Array<Function>> = new Map<string, Array<Function>>();
|
|
|
|
constructor() {
|
|
this._dataStream$.subscribe((data) => this._onEvent(data));
|
|
}
|
|
|
|
notifyDataChanged(event, value) {
|
|
|
|
let current = this._data[event];
|
|
if (current != value) {
|
|
this._data[event] = value;
|
|
|
|
this._data.next({
|
|
event: event,
|
|
data: this._data[event]
|
|
});
|
|
}
|
|
}
|
|
|
|
subscribe(event:string, callback:Function) {
|
|
var subscribers = this._subscriptions.get(event) || [];
|
|
subscribers.push(callback);
|
|
|
|
this._subscriptions.set(event, subscribers);
|
|
}
|
|
|
|
_onEvent(data:any) {
|
|
var subscribers = this._subscriptions.get(data['event']) || [];
|
|
|
|
subscribers.forEach((callback) => {
|
|
callback.call(null, data['data']);
|
|
});
|
|
}
|
|
}
|