Skip to content

Commit

Permalink
feat(EventBus): Support publish multiple events (array) in event bus.
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosCraviotto committed Jun 15, 2020
1 parent 19c797b commit 129356e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ When submitting your pull-request try to follow those guides:

All notable changes to this project will be documented in this section.

### 0.2.0

#### Added

- Support publish multiple events (array) in event bus.

### 0.1.0

#### Added
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "curli-bus",
"version": "0.1.0-beta.1",
"version": "0.2.0-beta.0",
"description": "Simple Command Bus Implementation (CQRS) for NodeJS/Typescript.",
"main": "index",
"scripts": {
Expand Down
16 changes: 13 additions & 3 deletions src/EventBus/EventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ export class EventBus {
}

/**
* Publish an event
* @param {Event} event
* Publish an event or a list of events.
* @param {Event|Array<Event>} event
*/
public publish(event: Event): void {
public publish(events: Event | Array<Event>): void {
if (Array.isArray(events)) {
events.forEach((event: Event) => {
this.publishSingleEvent(event);
});
} else {
this.publishSingleEvent(events);
}
}

public publishSingleEvent(event: Event): void {

if (!this.subscribersMap.hasOwnProperty(event.eventName)) {
throw new Error(`There is not a subscriber for ${event.eventName} event.`);
Expand Down
29 changes: 29 additions & 0 deletions src/__tests__/unit/EventBus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,35 @@ describe('EventBus class tests', function () {
const event = new EventTest({name: 'carlos'});
eventBus.publish(event);
});

it('Should publish a list of events', function () {
const subscriber = new EventSubscriber();
let checkTimesCallEventCarlos = 0;
let checkTimesCallEventMiguel = 0;

subscriber.handle = (event: Event) => {
if ('carlos' === event.data.name) {
checkTimesCallEventCarlos++;
} else if ('miguel' === event.data.name) {
checkTimesCallEventMiguel++;
}
};

eventBus.register(subscriber);

const event1 = new EventTest({name: 'carlos'});
const event2 = new EventTest({name: 'carlos'});
const event3 = new EventTest({name: 'carlos'});
const event4 = new EventTest({name: 'miguel'});

const eventsList: Array<Event> = [event1, event2, event3, event4];

eventBus.publish(eventsList);

chai.assert.deepEqual(3, checkTimesCallEventCarlos);
chai.assert.deepEqual(1, checkTimesCallEventMiguel);
});

it('Should publish an event wit a few subscribers', function () {
const subscriber1 = new EventSubscriber();
const subscriber2 = new EventSubscriber();
Expand Down

0 comments on commit 129356e

Please sign in to comment.