Update README.md
Browse files
README.md
CHANGED
|
@@ -1,15 +1,22 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
```dart
|
| 15 |
final system = ActorSystem();
|
|
@@ -22,14 +29,15 @@ final greeter = await system.spawn<Greet>(
|
|
| 22 |
greeter.send(Greet('Alice'));
|
| 23 |
```
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
Actory supports distributed actor systems
|
| 28 |
|
| 29 |
-
|
| 30 |
|
| 31 |
```dart
|
| 32 |
-
// Create a factory for your node
|
| 33 |
final factory = RemoteActorFactory(
|
| 34 |
host: 'localhost',
|
| 35 |
port: 8080,
|
|
@@ -38,87 +46,73 @@ final factory = RemoteActorFactory(
|
|
| 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 |
-
|
| 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 |
-
|
| 82 |
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 122 |
|
| 123 |
```dart
|
| 124 |
class SharedStateActor extends Actor<dynamic> {
|
|
@@ -135,22 +129,26 @@ class SharedStateActor extends Actor<dynamic> {
|
|
| 135 |
}
|
| 136 |
```
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: gpl-3.0
|
| 3 |
+
pretty_name: Typed Actor Model for Rust
|
| 4 |
+
---
|
| 5 |
+
# π§ Actory β Typed Actor Model for Rust
|
| 6 |
|
| 7 |
+
`actory` is a lightweight, strongly typed actor framework for Dart that enables building concurrent, message-driven systems with:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
* β
Async & sync actors
|
| 10 |
+
* β
Isolate-based concurrency
|
| 11 |
+
* β
Typed message passing (no use of `dynamic`)
|
| 12 |
+
* β
Ask/reply patterns with `Future`-based messaging
|
| 13 |
+
* β
Supervision strategies for fault tolerance
|
| 14 |
+
* β
Remote actors with WebSocket clustering support
|
| 15 |
+
* β
Thread-safe data structures (SafeMap) for shared state
|
| 16 |
+
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
## π Basic Usage
|
| 20 |
|
| 21 |
```dart
|
| 22 |
final system = ActorSystem();
|
|
|
|
| 29 |
greeter.send(Greet('Alice'));
|
| 30 |
```
|
| 31 |
|
| 32 |
+
---
|
| 33 |
+
|
| 34 |
+
## π Remote Actors
|
| 35 |
|
| 36 |
+
Actory supports distributed actor systems spanning multiple nodes.
|
| 37 |
|
| 38 |
+
### Using `RemoteActorFactory` (Recommended)
|
| 39 |
|
| 40 |
```dart
|
|
|
|
| 41 |
final factory = RemoteActorFactory(
|
| 42 |
host: 'localhost',
|
| 43 |
port: 8080,
|
|
|
|
| 46 |
|
| 47 |
await factory.start();
|
| 48 |
|
|
|
|
| 49 |
await factory.createLocalActor<ChatMessage>(
|
| 50 |
'chat-actor',
|
| 51 |
ChatActor(),
|
| 52 |
(msg) => msg as ChatMessage,
|
| 53 |
);
|
| 54 |
|
|
|
|
| 55 |
factory.registerRemoteActor('remote-chat', 'ws://localhost:8081');
|
| 56 |
|
|
|
|
| 57 |
factory.sendMessage('chat-actor', ChatMessage('User', 'Hello!'));
|
| 58 |
factory.sendMessage('remote-chat', ChatMessage('User', 'Hello remote!'));
|
| 59 |
```
|
| 60 |
|
| 61 |
+
### Manual Remote Actor Setup
|
| 62 |
|
| 63 |
```dart
|
|
|
|
| 64 |
final clusterNode = ClusterNode('localhost', 8080);
|
| 65 |
final registry = ActorRegistry();
|
| 66 |
|
| 67 |
await clusterNode.start();
|
| 68 |
|
|
|
|
| 69 |
final localActor = await system.spawn<Message>(actor, decoder);
|
| 70 |
registry.registerLocal('my-actor', localActor);
|
| 71 |
|
|
|
|
| 72 |
registry.registerRemote('remote-actor', 'ws://localhost:8081');
|
| 73 |
|
|
|
|
| 74 |
final localRef = registry.get('my-actor');
|
| 75 |
final remoteRef = registry.get('remote-actor'); // Returns RemoteActorRef
|
| 76 |
|
|
|
|
| 77 |
localRef?.send(message);
|
| 78 |
remoteRef?.send(message);
|
| 79 |
```
|
| 80 |
|
| 81 |
+
---
|
| 82 |
|
| 83 |
+
## π SafeMap β Thread-Safe Data Structure
|
| 84 |
|
| 85 |
+
`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.
|
| 86 |
+
|
| 87 |
+
### Usage Example
|
| 88 |
|
| 89 |
```dart
|
|
|
|
| 90 |
final sharedData = SafeMap<String, String>();
|
| 91 |
|
|
|
|
| 92 |
sharedData.set('key', 'value');
|
| 93 |
final value = sharedData.get('key');
|
| 94 |
|
|
|
|
| 95 |
final wasAdded = sharedData.putIfAbsent('key', () => 'default');
|
| 96 |
final computed = sharedData.getOrCompute('key', () => 'computed');
|
| 97 |
|
|
|
|
| 98 |
final wasUpdated = sharedData.updateIfPresent('key', (v) => 'updated_$v');
|
| 99 |
|
|
|
|
| 100 |
final keys = sharedData.keys;
|
| 101 |
final values = sharedData.values;
|
| 102 |
final entries = sharedData.entries;
|
| 103 |
|
|
|
|
| 104 |
final filtered = sharedData.where((key, value) => key.startsWith('user_'));
|
| 105 |
final transformed = sharedData.map((key, value) => MapEntry('new_$key', value.toUpperCase()));
|
| 106 |
|
|
|
|
| 107 |
sharedData.merge({'key2': 'value2'});
|
| 108 |
sharedData.mergeSafeMap(otherSafeMap);
|
| 109 |
|
|
|
|
| 110 |
final copy = sharedData.copy();
|
| 111 |
final regularMap = sharedData.toMap();
|
| 112 |
sharedData.clear();
|
| 113 |
```
|
| 114 |
|
| 115 |
+
### SafeMap in an Actor
|
| 116 |
|
| 117 |
```dart
|
| 118 |
class SharedStateActor extends Actor<dynamic> {
|
|
|
|
| 129 |
}
|
| 130 |
```
|
| 131 |
|
| 132 |
+
---
|
| 133 |
+
|
| 134 |
+
## π Examples
|
| 135 |
+
|
| 136 |
+
* `/example/main.dart` β Basic actor usage
|
| 137 |
+
* `/example/cluster_main.dart` β Cluster node example
|
| 138 |
+
* `/example/simple_remote_actor.dart` β Simple remote actor
|
| 139 |
+
* `/example/remote_actor_example.dart` β Full remote actor demo
|
| 140 |
+
* `/example/factory_remote_actor.dart` β `RemoteActorFactory` usage
|
| 141 |
+
* `/example/safe_map_example.dart` β SafeMap usage with actors
|
| 142 |
+
* `/example/safe_map_actor_example.dart` β Focused SafeMap actor demo
|
| 143 |
+
|
| 144 |
+
---
|
| 145 |
+
|
| 146 |
+
## π§ Architecture Overview
|
| 147 |
+
|
| 148 |
+
* **Actor** β Basic message processing unit
|
| 149 |
+
* **ActorSystem** β Manages actor lifecycle and supervision
|
| 150 |
+
* **ClusterNode** β WebSocket-based cluster node for distributed communication
|
| 151 |
+
* **ActorRegistry** β Maps actor IDs to local or remote references
|
| 152 |
+
* **RemoteActorRef** β Proxy to communicate with remote actors
|
| 153 |
+
* **RemoteActorFactory** β High-level API to manage local and remote actors
|
| 154 |
+
* **SafeMap** β Thread-safe map implementation for shared state within isolates
|