@almin/usecase-bus

A mediator for UseCase and Command.

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
48525Aug 4, 2021Apr 21, 2016Minified + gzip package size for @almin/usecase-bus in KB

Readme

@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

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.