Skip to content

Build robust and resilient message-driven systems

License

Notifications You must be signed in to change notification settings

alexeyraspopov/actor-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JavaScript Actors

This lib is a proof of concept implementation of actor system in JavaScript.

Install

npm install actor-system

Thanks to @kt3k for providing the name in NPM registry.

How to use

  1. 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();
  1. Define message types.
import { Message } from 'actor-system';

class Ping extends Message { }
class Pong extends Message { }
  1. 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());
}
  1. Spawn them.
system.spawn(PingActor);
system.spawn(PongActor);
system.spawn(Main);

Articles