actors 0.3.0

actors #

actors is a library that enables the use of the Actors Model in Dart.

It is a thin wrapper around Dart's Isolate that makes them much easier to use.

Actor #

To start an Actor is very easy. You simply create a Handler implementing the logic to handle messages within the Actor's Isolate, then create an Actor using it:

class Two with Handler<int, int> {
  int handle(int n) => n * 2;
}

main() async {
  final actor = Actor(Two());
  print(await actor.send(5)); // 10
  exit(0);
}

As you can see, the Actor can send a message back to the caller asynchronously.

Actors expose the Isolate they run on. To kill an Isolate, and consequently the Actor, do the following:

final isolate = await actor.isolate;
isolate.kill(priority: Isolate.immediate);

ActorGroup #

For cases where messages are CPU intensive to handle and there may be many of them, the ActorGroup class can be used. It multiplexes messages to a number of Actors which use the same type of Handler to handle messages.

// create a group of 4 actors
final group = ActorGroup(Two(), size: 4);
print(await group.send(5)); // 10

Messenger #

The Messenger mixin is implemented by Actor, ActorGroup, and also LocalMessenger, which runs its Handler in the local Isolate.

Messenger<int, int> messenger;

// a Messenger can be local
messenger = LocalMessenger(Two());
print(await messenger.send(2)); // 4

// or it can be an Actor
messenger = Actor(Two());
print(await messenger.send(3)); // 6

// or an ActorGroup
messenger = ActorGroup(Two(), size: 2);
print(await messenger.send(4)); // 8
print(await messenger.send(5)); // 10

This makes it possible to write code that works the same whether the message is handled locally or in another Isolate.

0.3.0 #

  • Added ActorGroup.
  • Added LocalMessenger.

0.2.0 #

  • Lighter message representation for smaller overhead.

0.1.0 #

  • Initial version, created by Stagehand.
  • Implemented basic functionaliy of an Actor.

example/actors_example.dart

import 'dart:io' show exit;
import 'dart:isolate' show Isolate;

import 'package:actors/actors.dart';

class Two with Handler<int, int> {
  int handle(int n) => n * 2;
}

main() async {
  await actorExample();
  await actorGroupExample();
  await localMessengerExample();

  exit(0);
}

Future actorExample() async {
  final actor = Actor(Two());
  print(await actor.send(5)); // 10
  print(await actor.send(6)); // 12
  print(await actor.send(7)); // 14
  print(await actor.send(8)); // 16

  final isolate = await actor.isolate;
  isolate.kill(priority: Isolate.immediate);
}

Future actorGroupExample() async {
  // create a group of 4 actors
  final group = ActorGroup(Two(), size: 4);
  print(await group.send(5)); // 10
  print(await group.send(6)); // 12
  print(await group.send(7)); // 14
  print(await group.send(8)); // 16

  for (var actor in group.actors) {
    (await actor.isolate).kill(priority: Isolate.immediate);
  }
}

Future localMessengerExample() async {
  Messenger<int, int> messenger;

  // a Messenger can be local
  messenger = LocalMessenger(Two());
  print(await messenger.send(2)); // 4

  // or it can be an Actor
  messenger = Actor(Two());
  print(await messenger.send(3)); // 6

  // or an ActorGroup
  messenger = ActorGroup(Two(), size: 2);
  print(await messenger.send(4)); // 8
  print(await messenger.send(5)); // 10
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  actors: ^0.3.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:actors/actors.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Jul 17, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, other

Primary library: package:actors/actors.dart with components: isolate.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
Dev dependencies
pedantic ^1.0.0
test ^1.0.0

Admin