This lib is a proof of concept implementation of actor system in JavaScript.
npm install actor-system
Thanks to @kt3k for providing the name in NPM registry.
- Bootstrap actor system.
import { ActorSystem, MessageDispatcher, ExecutionContext, AnimationFrameExecutor } from 'actor-system';
const executor = new AnimationFrameExecutor(60);
const context = new ExecutionContext(executor);
const dispatcher = new MessageDispatcher(context);
const system = new ActorSystem(dispatcher);
Or by using default set of tools:
import { ActorSystem } from 'actor-system';
const system = ActorSystem.fromDefaults();
- Define message types.
import { Message } from 'actor-system';
class Ping extends Message { }
class Pong extends Message { }
- Implement actors.
async function PingActor(system) {
for await (const message of system.dispatcher) {
switch (message.subject) {
case Ping:
system.dispatcher.dispatch(new Pong());
break;
}
}
}
async function PongActor(system) {
for await (const message of system.dispatcher) {
switch (message.subject) {
case Pong:
system.dispatcher.dispatch(new Ping());
break;
}
}
}
async function Main(system) {
system.dispatcher.dispatch(new Ping());
}
- Spawn them.
system.spawn(PingActor);
system.spawn(PongActor);
system.spawn(Main);