Coder Social home page Coder Social logo

actor-system's Introduction

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

actor-system's People

Contributors

alexeyraspopov avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.