0.0.7 • Published 8 years ago

busjs v0.0.7

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

BusJS

BusJS simple command bus for Javascript apps. It allows you to implement CQRS architecture in your app.

Installation

npm install busjs --save

Basic Usage

Handling commands with Command Handlers

Every Command must have name, so it can be identified. Command is always handled by exacly one command handler. Command handler can be function which recives command object, or it can object with handle(command) method.

Let's take a look at exmaples:

import Bus from 'busjs'

let bus = new Bus;

bus.addCommandHandler('RegisterUserHandler', (command) => {
    console.log('Handling RegisterUser command')
})

bus.handle('RegisterUser', { email: 'john@doe.com', password: 'secret123' })

Command handler can be also an object:

...
let commandHandler = {
    handle: (command) => {
        console.log('Handling RegisterUser command')
    }
}

bus.addCommandHandler('RegisterUserHandler', commandHandler)

bus.handle('RegisterUser', { email: 'john@doe.com', password: 'secret123' })

REMEMBER

Command hanlder's are resolved based on naming convenction. If your command is callled "RegisterUser", than command handler name must be "RegisterUserHandler". If command is "SendNotification" than handler is "SendNotificationHandler" and so on...

Dipatching and handling events

Through lifcecyle of your application you may want to dipatch events, when something happened. For example we may want to dispatch UserWasRegistered event. We can disptach our events in command handler's or in your domain objects. We stick with command handlers in this example.

import { recordThat }, Bus from 'busjs'

let bus = new Bus;

bus.addCommandHandler('RegisterUserHandler', (command) => {
    recordThat('UserWasRegistered', {email: command.email})
})

let eventSubscriber = {
    notify: (event) => {
        console.log('Event subscriber was trigged')
    }
}

bus.addEventSubscriber('SendNotification', eventSubscriber, ['UserWasRegistered'])

bus.handle('RegisterUser', { email: 'john@doe.com', password: 'secret123' })

In this example when we handling command, we record a new event called UserWasRegistered. Then we are adding event subscriber which will handle this event. Event subscriber can be a function or object with notify(event) method. Unlike Command handlers, one event can be handled by many event subscribers.

Because of that we need to spcify which events eventSubscriber should handle. To do this we pass an array of event's names as a third argument for addEventSubscriber method.

Middlewares

comming soon

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago

0.1.1

8 years ago