Library for handling dispachable events like Commands, Jobs, etc.
npm install @o.yilmaz/js-dispatcher
import { AbstractDispatchable, AbstractHandler, dispatcher } from '@o.yilmaz/js-dispatcher'
class OpenAccountCommand extends AbstractDispatchable {
kind: string = 'OpenAccountCommand'
data() {
return 'Account opened'
}
}
class CloseAccountCommand extends AbstractDispatchable {
kind: string = 'CloseAccountCommand'
data() {
return 'Account closed'
}
}
class OpenAccountCommandHandler extends AbstractHandler {
handle(dispatchable: OpenAccountCommand) {
console.log('OpenAccountCommandHandler run -> ', dispatchable.data())
}
}
class CloseAccountCommandHandler extends AbstractHandler {
handle(dispatchable: CloseAccountCommand) {
console.log('CloseAccountCommandHandler run -> ', dispatchable.data())
}
}
dispatcher.registerHandler(new OpenAccountCommandHandler(), new OpenAccountCommand())
dispatcher.registerHandler(new CloseAccountCommandHandler(), new CloseAccountCommand())
// since dispatcher is a singleton object, it can be called anywhere from the application
dispatcher.dispatch()
The output of the above code would be:
OpenAccountCommandHandler run -> Account opened
CloseAccountCommandHandler run -> Account closed