command_bus 1.2.0 copy "command_bus: ^1.2.0" to clipboard
command_bus: ^1.2.0 copied to clipboard

Minimal command bus toolkit with middleware, safe result handling, and examples.

Command Bus (Dart) #

Минималистичная библиотека для регистрации и выполнения команд через связанные обработчики.

pub

Компоненты #

  • Command<R> — описание намерения выполнить действие и ожидаемого результата R.
  • CommandHandler<C, R, S> — знает, как выполнить команду C и вернуть R. Тип S — состояние, передаваемое из CommandBus.
  • CommandBus<S> — регистрирует обработчики, вызывает их через dispatch и dispatchSafe. Параметр S — тип состояния.
  • Middleware — опциональная цепочка, выполняемая после dispatch, но до обработчика (логирование, метрики, транзакции и т.д.).
  • Result<R> — типизированная обёртка результата с данными или ошибкой для dispatchSafe.

Быстрый старт #

import 'package:command_bus/command_bus.dart';

class AppState {
  final String userId;
  const AppState(this.userId);
}

class GetLength extends Command<int> {
  final String value;
  const GetLength(this.value);
}

class GetLengthHandler extends CommandHandler<GetLength, int, AppState> {
  @override
  int handle(GetLength command, AppState state) {
    print('User: ${state.userId}'); // доступ к состоянию
    if (command.value.isEmpty) throw ArgumentError('String must not be empty');
    return command.value.length;
  }
}

Future<void> main() async {
  final bus = CommandBus<AppState>(state: AppState('user-123'));
  bus.register(GetLengthHandler());

  final length = await bus.dispatch(const GetLength('Hello'));
  print(length); // 5
}

Более развернутый пример находится в example/command_bus_example.dart.

1
likes
155
points
199
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Minimal command bus toolkit with middleware, safe result handling, and examples.

Repository (GitHub)
View/report issues

License

MIT (license)

More

Packages that depend on command_bus