--- 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( 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( '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(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(); 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 { final SafeMap _counters = SafeMap(); @override Future onMessage(dynamic message, ActorContext 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