usecase_forge 0.1.0-dev.5 copy "usecase_forge: ^0.1.0-dev.5" to clipboard
usecase_forge: ^0.1.0-dev.5 copied to clipboard

A pure Dart command-driven UseCase runtime with typed handlers, scheduling policies, cancellation, snapshots, and bounded history.

example/usecase_forge_example.dart

import 'package:usecase_forge/usecase_forge.dart';

final class CounterState {
  const CounterState(this.value);

  final int value;

  @override
  bool operator ==(final Object other) =>
      other is CounterState && other.value == value;

  @override
  int get hashCode => value.hashCode;
}

final class Increment extends UseCaseCommand {
  const Increment({this.by = 1});

  final int by;
}

final class CounterUseCase extends UseCase<CounterState> {
  CounterUseCase() : super(initialState: const CounterState(0)) {
    registerCommand<Increment>(_increment);
  }

  Future<void> _increment(
    final Increment command,
    final UseCaseExecutionContext<CounterState> context,
  ) async {
    final int current = context.snapshot.state.value;
    context.publish(CounterState(current + command.by));
  }
}

Future<int> runCounterExample() async {
  final CounterUseCase counter = CounterUseCase();
  final Future<UseCaseSnapshot<CounterState>> finished = counter.stream
      .firstWhere(
        (final UseCaseSnapshot<CounterState> snapshot) =>
            snapshot.phase == UseCaseExecutionPhase.finished,
      );

  counter.add(const Increment(by: 2));
  await finished;

  final int value = counter.state.state.value;
  await counter.close();
  return value;
}

Future<void> main() async {
  final int value = await runCounterExample();
  print('Counter: $value');
}
0
likes
160
points
218
downloads

Documentation

Documentation
API reference

Publisher

verified publisherarktelos.dev

Weekly Downloads

A pure Dart command-driven UseCase runtime with typed handlers, scheduling policies, cancellation, snapshots, and bounded history.

Homepage
Repository (GitLab)
View/report issues

Topics

#architecture #concurrency #state-management #use-case

License

Apache-2.0 (license)

More

Packages that depend on usecase_forge