Commit b16f425
Changed files (2)
app
infrastructure
__tests__
app/infrastructure/__tests__/event_aggregator_spec.js
@@ -23,5 +23,14 @@ describe("EventAggregator", () => {
expect(subscriber.called).toBeTruthy();
expect(subscriber.calledWith.username).toEqual('blah');
});
+
+ it ("does not publish events to subscribers interested in other events", function() {
+ let subscriber = new TestSubscriber();
+ subject.subscribe('LOGGED_IN', subscriber);
+ subject.publish({ event: 'LOGGED_OUT' })
+
+ expect(subscriber.called).toBeFalsy();
+ expect(subscriber.calledWith).toBeUndefined();
+ });
});
});
app/infrastructure/event-aggregator.js
@@ -1,14 +1,21 @@
export default class EventAggregator {
constructor() {
- this.subscribers = [];
+ this.subscriptions = { };
}
subscribe(event, subscriber) {
- this.subscribers.push(subscriber);
+ this.subscriptionsFor(event).push(subscriber);
}
publish(event) {
- this.subscribers.forEach(x => x.notify(event));
+ this.subscriptionsFor(event.event).forEach(x => x.notify(event));
+ }
+
+ subscriptionsFor(event) {
+ if (this.subscriptions.hasOwnProperty(event) == false) {
+ this.subscriptions[event] = [];
+ }
+ return this.subscriptions[event];
}
}