master
1export default class EventAggregator {
2 constructor() {
3 this.subscriptions = { };
4 }
5
6 subscribe(event, subscriber) {
7 this._subscriptionsFor(event).add(subscriber);
8 }
9
10 publish(event) {
11 this._subscriptionsFor(event.event).forEach((x) => {
12 x.notify(event);
13 });
14 }
15
16 unsubscribe(subscriber) {
17 for (var subscription in this.subscriptions) {
18 let subscribers = this._subscriptionsFor(subscription)
19 if (subscribers.has(subscriber)) {
20 subscribers.delete(subscriber);
21 }
22 }
23 }
24
25 _subscriptionsFor(event) {
26 let key = event;
27 if (!this.subscriptions.hasOwnProperty(key)) {
28 this.subscriptions[key] = new Set();
29 }
30 return this.subscriptions[key];
31 }
32}