NARIK
Search…
17.-Event-Aggregator
Since Narik is a module framework, you sometimes need to handle events from one module in another module. Narik Event Aggregator Service helps you to do that easily.
1
/**
2
* Event aggregator service
3
* A service that manage events inside application.
4
*/
5
export abstract class EventAggregatorService {
6
7
/**
8
* Publish event aggregator service
9
* @param eventType eventType
10
* @param eventArgs event argument
11
*/
12
abstract publish(eventType: any, eventArgs: any);
13
14
/**
15
* Listens event aggregator service
16
* @param eventType eventType
17
* @returns an `Observable` that emit whenever event occurs.
18
*/
19
abstract listen<T>(eventType: any): Observable<T>;
20
}
Copied!
It has two methods. One for listen and another for publishing. For example, you may listen for "MODULE_LOAD_COMPLETELY" event to do something when one module loads completely.
example:
1
export class ModuleLoadCompletelyGuard implements CanActivate {
2
constructor(
3
private eventAggregatorService: EventAggregatorService
4
) {}
5
6
canActivate(route: ActivatedRouteSnapshot) {
7
const moduleKey = route.data["moduleKey"];
8
return Observable.create((observer: Observer<any>) => {
9
this.eventAggregatorService
10
.listen<{ moduleKey: string }>("MODULE_LOAD_COMPLETELY")
11
.pipe(
12
filter(x => x.moduleKey === moduleKey),
13
first()
14
)
15
.subscribe(() => {
16
observer.next(true);
17
});
18
});
19
}
20
}
Copied!
Each event can be published by its owner. For example "MODULE_LOAD_COMPLETELY" is published by ModuleManager.
1
export class test
2
{
3
someMethod()
4
{
5
this.eventAggregatorService.publish("MODULE_LOAD_COMPLETELY", {
6
moduleKey: key,
7
moduleInfo: moduleInfo
8
});
9
}
10
}
Copied!
Narik Event Aggregator Service use one subject for each event type. If you specify event's attributes in metadata, Event Aggregator Service uses them, otherwise uses simple subject.
Last modified 7mo ago
Copy link