| | --- |
| | license: gpl-3.0 |
| | pretty_name: Typed Actor Model for Rust |
| | --- |
| | # π§ Actory β Typed Actor Model for Rust |
| |
|
| | `actory` is a lightweight, strongly typed actor framework for Dart that enables building concurrent, message-driven systems with: |
| |
|
| | * β
Async & sync actors |
| | * β
Isolate-based concurrency |
| | * β
Typed message passing (no use of `dynamic`) |
| | * β
Ask/reply patterns with `Future`-based messaging |
| | * β
Supervision strategies for fault tolerance |
| | * β
Remote actors with WebSocket clustering support |
| | * β
Thread-safe data structures (SafeMap) for shared state |
| |
|
| | --- |
| |
|
| | ## π Basic Usage |
| |
|
| | ```dart |
| | final system = ActorSystem(); |
| | |
| | final greeter = await system.spawn<Greet>( |
| | Greeter(), |
| | (msg) => msg as Greet, |
| | ); |
| | |
| | greeter.send(Greet('Alice')); |
| | ``` |
| |
|
| | --- |
| |
|
| | ## π Remote Actors |
| |
|
| | Actory supports distributed actor systems spanning multiple nodes. |
| |
|
| | ### Using `RemoteActorFactory` (Recommended) |
| |
|
| | ```dart |
| | final factory = RemoteActorFactory( |
| | host: 'localhost', |
| | port: 8080, |
| | peerUrls: ['ws://localhost:8081'], |
| | ); |
| | |
| | await factory.start(); |
| | |
| | await factory.createLocalActor<ChatMessage>( |
| | 'chat-actor', |
| | ChatActor(), |
| | (msg) => msg as ChatMessage, |
| | ); |
| | |
| | factory.registerRemoteActor('remote-chat', 'ws://localhost:8081'); |
| | |
| | factory.sendMessage('chat-actor', ChatMessage('User', 'Hello!')); |
| | factory.sendMessage('remote-chat', ChatMessage('User', 'Hello remote!')); |
| | ``` |
| |
|
| | ### Manual Remote Actor Setup |
| |
|
| | ```dart |
| | final clusterNode = ClusterNode('localhost', 8080); |
| | final registry = ActorRegistry(); |
| | |
| | await clusterNode.start(); |
| | |
| | final localActor = await system.spawn<Message>(actor, decoder); |
| | registry.registerLocal('my-actor', localActor); |
| | |
| | registry.registerRemote('remote-actor', 'ws://localhost:8081'); |
| | |
| | final localRef = registry.get('my-actor'); |
| | final remoteRef = registry.get('remote-actor'); // Returns RemoteActorRef |
| | |
| | localRef?.send(message); |
| | remoteRef?.send(message); |
| | ``` |
| |
|
| | --- |
| |
|
| | ## π SafeMap β Thread-Safe Data Structure |
| |
|
| | `SafeMap` provides atomic and thread-safe operations on shared state **within a single Dart isolate**. Since each actor runs in its own isolate, `SafeMap` is useful for shared data inside an isolate, while cross-isolate communication should use message passing. |
| |
|
| | ### Usage Example |
| |
|
| | ```dart |
| | final sharedData = SafeMap<String, String>(); |
| | |
| | sharedData.set('key', 'value'); |
| | final value = sharedData.get('key'); |
| | |
| | final wasAdded = sharedData.putIfAbsent('key', () => 'default'); |
| | final computed = sharedData.getOrCompute('key', () => 'computed'); |
| | |
| | final wasUpdated = sharedData.updateIfPresent('key', (v) => 'updated_$v'); |
| | |
| | final keys = sharedData.keys; |
| | final values = sharedData.values; |
| | final entries = sharedData.entries; |
| | |
| | final filtered = sharedData.where((key, value) => key.startsWith('user_')); |
| | final transformed = sharedData.map((key, value) => MapEntry('new_$key', value.toUpperCase())); |
| | |
| | sharedData.merge({'key2': 'value2'}); |
| | sharedData.mergeSafeMap(otherSafeMap); |
| | |
| | final copy = sharedData.copy(); |
| | final regularMap = sharedData.toMap(); |
| | sharedData.clear(); |
| | ``` |
| |
|
| | ### SafeMap in an Actor |
| |
|
| | ```dart |
| | class SharedStateActor extends Actor<dynamic> { |
| | final SafeMap<String, int> _counters = SafeMap<String, int>(); |
| | |
| | @override |
| | Future<void> onMessage(dynamic message, ActorContext<dynamic> context) async { |
| | if (message is IncrementCounter) { |
| | final current = _counters.get(message.key) ?? 0; |
| | _counters.set(message.key, current + 1); |
| | context.send('Counter ${message.key}: ${current + 1}'); |
| | } |
| | } |
| | } |
| | ``` |
| |
|
| | --- |
| |
|
| | ## π Examples |
| |
|
| | * `/example/main.dart` β Basic actor usage |
| | * `/example/cluster_main.dart` β Cluster node example |
| | * `/example/simple_remote_actor.dart` β Simple remote actor |
| | * `/example/remote_actor_example.dart` β Full remote actor demo |
| | * `/example/factory_remote_actor.dart` β `RemoteActorFactory` usage |
| | * `/example/safe_map_example.dart` β SafeMap usage with actors |
| | * `/example/safe_map_actor_example.dart` β Focused SafeMap actor demo |
| |
|
| | --- |
| |
|
| | ## π§ Architecture Overview |
| |
|
| | * **Actor** β Basic message processing unit |
| | * **ActorSystem** β Manages actor lifecycle and supervision |
| | * **ClusterNode** β WebSocket-based cluster node for distributed communication |
| | * **ActorRegistry** β Maps actor IDs to local or remote references |
| | * **RemoteActorRef** β Proxy to communicate with remote actors |
| | * **RemoteActorFactory** β High-level API to manage local and remote actors |
| | * **SafeMap** β Thread-safe map implementation for shared state within isolates |