File size: 4,427 Bytes
b3acecc cddba7c b3acecc cddba7c b3acecc cddba7c b3acecc cddba7c b3acecc cddba7c b3acecc cddba7c b3acecc cddba7c b3acecc cddba7c b3acecc cddba7c b3acecc cddba7c b3acecc cddba7c b3acecc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
---
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 |