usecase-bus
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

@almin/usecase-bus

A mediator for UseCase and Command. This library provide Command Bus.

What is Command and Command handler pattern?

Command

  • A bus send Command to a single Command Handler
  • Command may be rejected by system
  • Command may be failed during executing in Handler
  • Command may be various effect in system state
  • Command does not be over the boundary
  • Command should have imperative named.

What is merits?

  • Flexible
  • Retry the UseCase
  • Logging
  • Adoptable
  • Additional Event

What is de-merits?

  • Add new Layer(CommandBus)

Install

Install with npm:

npm install @almin/usecase-bus

Usage

import { UseCase, Context } from "almin";
import { UseCaseCommandBus } from "@almin/usecase-bus"
// async code
(async () => {
    const context = new Context({
        store: new NopeStore()
    });
 
    class CommandA {
        type = "CommandA";
    }
 
    class CommandB {
        type = "CommandB";
    }
 
    const executed: (CommandA | CommandB)[] = [];
 
    class TestUseCaseA extends UseCase {
        execute(command: CommandA) {
            executed.push(command);
        }
    }
 
    class TestUseCaseB extends UseCase {
        execute(command: CommandB) {
            executed.push(command);
        }
    }
 
    const createTestUseCaseB = (_command: CommandB) => {
        return new TestUseCaseB();
    };
 
    // create binding between Command Constructor and UseCase/UseCaseFactory.
    const bus = UseCaseCommandBus.create(context)
        .bind(CommandA, new TestUseCaseA())
        .bindFactory(CommandB, createTestUseCaseB);
    // send CommandA => execute TestUseCaseA
    await bus.send(new CommandA());
    assert.strictEqual(executed.length, 1);
    assert.ok(executed[0] instanceof CommandA);
    // send CommandB => execute createTestUseCaseB()
    await bus.send(new CommandB());
    assert.strictEqual(executed.length, 2);
    assert.ok(executed[1] instanceof CommandB);
})()

Changelog

See Releases page.

Reference

Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

License

MIT © azu

Readme

Keywords

Package Sidebar

Install

npm i usecase-bus

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

103 kB

Total Files

37

Last publish

Collaborators

  • azu