GithubHelp home page GithubHelp logo

drama's Introduction

drama

Actor model implementation for JavaScript and Node.js (work in progress)

Inspirations: Scala Actors, Akka, Pykka

walkthrough

Actors live in systems, so let's define ours:

var drama = require('drama')
var sys = drama('sys')

Okay, now let's create a simple actor in our system, with some default behavior:

var actor = sys.actor({ hello: function (message) { console.log(message) } })

And then run it!

actor.tell('hello', 'world') // 'world'

Right. Now let's see what else we can do. Maybe we need to put and get some value back and forth from the actor. So let's create an actor that does that:

var actor = sys.actor(function (initial) {
  var value = initial
  return {
    set: function (val) {
      value = val
    }
  , get: function () {
      this.reply(value)
    }
  }
})

Wait, what happened here? We defined an initial behavior as a function. These functions catch all the messages, but we'll only be using the first one to set an initial value and put our var in scope.

We then return or designate a behavior for any message that'll come in the future. return is essentially overloaded to be the react method familiar with other actor implementations. Scala users should read return as react.

So let's initialize it and use it:

actor.init('some value')
actor.ask(actor, 'get', function (val) {
  console.log(val) // 'some value'
})

You probably don't want literals all over your code. Don't worry, you can create a proxy and use it as a regular object:

var proxy = actor.pick('?get', 'set')
proxy.set('another value')
proxy.get(function (val) {
  console.log(val) // 'another value'
})

Here we create a proxy using pick on an actor reference. It essentially allows you to pick methods from the actor. You'll also notice the ? which is used to declare that this method should be invoked with ask rather than tell. There is also a ?? prefix to indicate that the reply is expected to be a future, and that you want to auto-resolve and callback for you. Hopefully, when we get harmony proxies from ES6 everywhere, this step could be omitted.

remote actors

Just fork the system and use like local:

var remote = sys.fork('remote')
var remoteActor = remote.actor({
  ping: function () { this.reply('pong')
}}).pick('?ping')

remoteActor.ping(function (response) {
  console.log(response) // 'pong'
})

These and a lot more, can be found in the examples/.

Enjoy!

licence

MIT/X11

drama's People

Contributors

stagas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

drama's Issues

Is this still active

Hi @stagas ,
I have been looking a lot at the actor model for programming (erlang,scala akka, akka.net) and came across this while searching for a js implementation. i wish to know if this is still active.
Thanks

Memory Leak

remote-pass-future.js leaks creating future actors that are not "stopped" anymore and therefore retain memory.

image

[Feature Request] message priority

Hello there,

Using a binary heap (http://eloquentjavascript.net/appendix2.html) it is possible to send prioritized messages. This helps implementing QoS (i.e. when certain actors are CPU-bound).

An actor could contain both type of inboxes (binary heap and array), so messages would go to one or another depending if there is an assigned priority or not. The actor would first process all messages from the heap inbox before jumping to array inbox. Also, replying to a prioritized message should inherit the priority (in case the recipient is also an actor).

Sending a message with priority could be as simple as:

// higher priority
actor.withPriority(0).tell("hello");

// lower priority
actor.withPriority(1).tell("hello");

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.