mirror of
https://github.com/akveo/ngx-admin.git
synced 2026-03-14 17:26:30 +01:00
app state
This commit is contained in:
parent
e6be0e4519
commit
38e1c6cfa3
8 changed files with 62 additions and 57 deletions
43
src/app/app.state.ts
Normal file
43
src/app/app.state.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
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']);
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue