This module implements a simple command bus to help app components interact via commands.
npm i @itross/cbus
'use strict'
const CommandBus = require('.')
const cbus = new CommandBus()
// register your handler
cbus.registerHandler('create_user', async (data) => {
// your handling code
// call a service or whatever
const user = await saveUser(data)
return user
})
async function yourMagicBusiness () {
// ...
// send your command
const user = await cbus.send('create_user', {
username: 'frank.zappa',
email: 'fz@test.com'
})
console.log(`created user: ${JSON.stringify(user, null, 2)}`)
// ...
}
yourMagicBusiness()
// somewhere in your business galaxy
async function saveUser (data) {
// your (async) business
// for sake of test
return Promise.resolve({
id: 1,
...data
})
}
Register an handler for a specific command.
command
: a string representing the command namehandler
: a function accepting an object as the command payload
Checks if an handler for the input command has been registered.
Returns a boolean value.
command
: a string representing the command name to check handler for
Send the command with data payload to the handler.
Returns the command result, if any.
Rejects if no handler has been registered for the command.
command
: a string representing the command name to be sentdata
: an object to be sent as command payload