Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# π§ Actory β Typed Actor Model for Dart
|
2 |
+
|
3 |
+
`actory` is a lightweight actor framework for Dart that enables concurrent, message-driven systems with:
|
4 |
+
- β
Async & sync actors
|
5 |
+
- β
Isolate-based concurrency
|
6 |
+
- β
Typed message passing (no `dynamic`)
|
7 |
+
- β
Ask/reply (`Future`-based)
|
8 |
+
- β
Supervision strategies
|
9 |
+
- β
Remote actors with WebSocket clustering
|
10 |
+
- β
Thread-safe data structures (SafeMap)
|
11 |
+
|
12 |
+
### π Basic Usage
|
13 |
+
|
14 |
+
```dart
|
15 |
+
final system = ActorSystem();
|
16 |
+
|
17 |
+
final greeter = await system.spawn<Greet>(
|
18 |
+
Greeter(),
|
19 |
+
(msg) => msg as Greet,
|
20 |
+
);
|
21 |
+
|
22 |
+
greeter.send(Greet('Alice'));
|
23 |
+
```
|
24 |
+
|
25 |
+
### π Remote Actors
|
26 |
+
|
27 |
+
Actory supports distributed actor systems with remote actors across multiple nodes:
|
28 |
+
|
29 |
+
#### Using RemoteActorFactory (Recommended)
|
30 |
+
|
31 |
+
```dart
|
32 |
+
// Create a factory for your node
|
33 |
+
final factory = RemoteActorFactory(
|
34 |
+
host: 'localhost',
|
35 |
+
port: 8080,
|
36 |
+
peerUrls: ['ws://localhost:8081'],
|
37 |
+
);
|
38 |
+
|
39 |
+
await factory.start();
|
40 |
+
|
41 |
+
// Create local actors
|
42 |
+
await factory.createLocalActor<ChatMessage>(
|
43 |
+
'chat-actor',
|
44 |
+
ChatActor(),
|
45 |
+
(msg) => msg as ChatMessage,
|
46 |
+
);
|
47 |
+
|
48 |
+
// Register remote actors
|
49 |
+
factory.registerRemoteActor('remote-chat', 'ws://localhost:8081');
|
50 |
+
|
51 |
+
// Send messages (works for both local and remote)
|
52 |
+
factory.sendMessage('chat-actor', ChatMessage('User', 'Hello!'));
|
53 |
+
factory.sendMessage('remote-chat', ChatMessage('User', 'Hello remote!'));
|
54 |
+
```
|
55 |
+
|
56 |
+
#### Manual Remote Actor Creation
|
57 |
+
|
58 |
+
```dart
|
59 |
+
// Create cluster node and registry
|
60 |
+
final clusterNode = ClusterNode('localhost', 8080);
|
61 |
+
final registry = ActorRegistry();
|
62 |
+
|
63 |
+
await clusterNode.start();
|
64 |
+
|
65 |
+
// Register local actor
|
66 |
+
final localActor = await system.spawn<Message>(actor, decoder);
|
67 |
+
registry.registerLocal('my-actor', localActor);
|
68 |
+
|
69 |
+
// Register remote actor
|
70 |
+
registry.registerRemote('remote-actor', 'ws://localhost:8081');
|
71 |
+
|
72 |
+
// Get actor references
|
73 |
+
final localRef = registry.get('my-actor');
|
74 |
+
final remoteRef = registry.get('remote-actor'); // Returns RemoteActorRef
|
75 |
+
|
76 |
+
// Send messages
|
77 |
+
localRef?.send(message);
|
78 |
+
remoteRef?.send(message);
|
79 |
+
```
|
80 |
+
|
81 |
+
### π SafeMap - Thread-Safe Data Structure
|
82 |
+
|
83 |
+
SafeMap provides atomic operations for shared state management in concurrent actor systems:
|
84 |
+
|
85 |
+
**Thread Safety Note**: SafeMap is designed for use within a single Dart isolate. In Dart's actor model, each actor runs in its own isolate, so SafeMap provides safety when multiple actors within the same isolate need to share state. For cross-isolate communication, use message passing through the actor system.
|
86 |
+
|
87 |
+
```dart
|
88 |
+
// Create a thread-safe map
|
89 |
+
final sharedData = SafeMap<String, String>();
|
90 |
+
|
91 |
+
// Atomic operations
|
92 |
+
sharedData.set('key', 'value');
|
93 |
+
final value = sharedData.get('key');
|
94 |
+
|
95 |
+
// Conditional operations
|
96 |
+
final wasAdded = sharedData.putIfAbsent('key', () => 'default');
|
97 |
+
final computed = sharedData.getOrCompute('key', () => 'computed');
|
98 |
+
|
99 |
+
// Update operations
|
100 |
+
final wasUpdated = sharedData.updateIfPresent('key', (v) => 'updated_$v');
|
101 |
+
|
102 |
+
// Collection operations
|
103 |
+
final keys = sharedData.keys;
|
104 |
+
final values = sharedData.values;
|
105 |
+
final entries = sharedData.entries;
|
106 |
+
|
107 |
+
// Functional operations
|
108 |
+
final filtered = sharedData.where((key, value) => key.startsWith('user_'));
|
109 |
+
final transformed = sharedData.map((key, value) => MapEntry('new_$key', value.toUpperCase()));
|
110 |
+
|
111 |
+
// Merge operations
|
112 |
+
sharedData.merge({'key2': 'value2'});
|
113 |
+
sharedData.mergeSafeMap(otherSafeMap);
|
114 |
+
|
115 |
+
// Utility operations
|
116 |
+
final copy = sharedData.copy();
|
117 |
+
final regularMap = sharedData.toMap();
|
118 |
+
sharedData.clear();
|
119 |
+
```
|
120 |
+
|
121 |
+
#### SafeMap with Actors
|
122 |
+
|
123 |
+
```dart
|
124 |
+
class SharedStateActor extends Actor<dynamic> {
|
125 |
+
final SafeMap<String, int> _counters = SafeMap<String, int>();
|
126 |
+
|
127 |
+
@override
|
128 |
+
Future<void> onMessage(dynamic message, ActorContext<dynamic> context) async {
|
129 |
+
if (message is IncrementCounter) {
|
130 |
+
final current = _counters.get(message.key) ?? 0;
|
131 |
+
_counters.set(message.key, current + 1);
|
132 |
+
context.send('Counter ${message.key}: ${current + 1}');
|
133 |
+
}
|
134 |
+
}
|
135 |
+
}
|
136 |
+
```
|
137 |
+
|
138 |
+
### π Examples
|
139 |
+
|
140 |
+
- `/example/main.dart` - Basic actor usage
|
141 |
+
- `/example/cluster_main.dart` - Cluster node example
|
142 |
+
- `/example/simple_remote_actor.dart` - Simple remote actor example
|
143 |
+
- `/example/remote_actor_example.dart` - Full remote actor demo
|
144 |
+
- `/example/factory_remote_actor.dart` - RemoteActorFactory usage
|
145 |
+
- `/example/safe_map_example.dart` - SafeMap usage with actors
|
146 |
+
- `/example/safe_map_actor_example.dart` - Focused SafeMap actor demo
|
147 |
+
|
148 |
+
### π§ Architecture
|
149 |
+
|
150 |
+
- **Actor**: Message processing unit
|
151 |
+
- **ActorSystem**: Manages actor lifecycle
|
152 |
+
- **ClusterNode**: WebSocket-based node for distributed communication
|
153 |
+
- **ActorRegistry**: Maps actor IDs to local/remote references
|
154 |
+
- **RemoteActorRef**: Proxy for remote actors
|
155 |
+
- **RemoteActorFactory**: High-level API for remote actor management
|
156 |
+
- **SafeMap**: Thread-safe map for shared state management
|