dactor 1.3.0 copy "dactor: ^1.3.0" to clipboard
dactor: ^1.3.0 copied to clipboard

An actor system for Dart

example/dactor_example.dart

import 'package:dactor/dactor.dart';

class CounterActor extends Actor {
  int _count = 0;

  @override
  Future<void> onMessage(dynamic message) async {
    // onMessage receives the payload, not the LocalMessage envelope.
    // The sender is available through the context.
    switch (message) {
      case 'increment':
        _count++;
        print('Count: $_count');
        context.sender?.tell(LocalMessage(payload: _count));
      case 'get':
        context.sender?.tell(LocalMessage(payload: _count));
    }
  }

  @override
  void preStart() {
    print('Counter actor started');
  }

  @override
  void postStop() {
    print('Counter actor stopped');
  }
}

void main() async {
  final system = ActorSystem.create();
  final actor = await system.spawn('counter', () => CounterActor());

  actor.tell(LocalMessage(payload: 'increment'));
  actor.tell(LocalMessage(payload: 'increment'));

  // ask completes with the payload of the reply.
  final count = await actor.ask<int>(
    LocalMessage(payload: 'get'),
    Duration(seconds: 1),
  );
  print('Count from ask: $count');

  await system.shutdown();
}
3
likes
130
points
227
downloads

Documentation

API reference

Publisher

verified publisherwerkswinkel.com

Weekly Downloads

An actor system for Dart

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

meta, uuid

More

Packages that depend on dactor