shakespeare

    0.0.4 • Public • Published

    shakespeare

    Build Status

    Shakespeare is a simplified Actor model based on event emitters.

    Shakespeare receives an instantiated event emitter via its constuctor. It then exposes 'send' and 'spawn' methods...

    Install

    npm install shakespeare --save
    
     
    var _ee  = require('events');
    var ee   = new _ee;
     
    var play = require('shakespeare')(ee);
     
    play.spawn('channel', function actor (message, pid) {});
     
    play.send('channel', { data: Math.random() });
     

    Methods

    send

    play.send(channel_or_pid, { self: 123, ping: true });

    spawn

    play.spawn('channel', function actor (message, pid) {
        if (message.hasOwnProperty('self'))
            play.send(message.self, { self: pid, pong: true });
    }); 

    Actors

    Everything looks like an event emitter except for the extra pid argument which is passed to the event handler or 'actor'. An actor in shakespeare is simply an augmented event emitter with a unique pid for each function handler.

    Basic actor

    function actor_with_state () {
            if (! (this instanceof actor_with_state)) {
                    return new actor_with_state();
            }
            this.data = 0;
            var self = this;
            this.on_message = function (message, pid) {
                    console.log('inside actor', message);
                    self.data += message.data;
                    console.log('updated internal data structure: data='+self.data);
            }
            this.complete = function () {
                    return this.data;
            }
    }
     
    var actor = actor_with_state();
    var actor_pid = play.spawn('demo_actor', actor.on_message);
     

    Actors with behaviours

    function actor_with_behaviour () {
            if (! (this instanceof actor_with_behaviour)) {
                    return new actor_with_behaviour();
            }
            var self = this;
            this.behaviour1 = function (message, pid) {
                    play.send('actor_behaviour_reciever', { test_result: true });
            }
            this.behaviour2 = function (message, pid) {
                    play.send('actor_behaviour_reciever', { test_result: false });
            }
            this.on_message = function (message, pid) {
                    if (message.important) {
                            self.behaviour1(message, pid);
                    } else {
                            self.behaviour2(message, pid);
                    }
            }
    }
     
    var actor = actor_with_behaviour();
     
    var actor_pid = play.spawn('demo_actor_behaviour', actor.on_message, function () {
        play.send('demo_actor_behaviour', { demo: true }, function () {
        play.send('demo_actor_behaviour', { important: true });
    });
     

    Install

    npm i shakespeare

    DownloadsWeekly Downloads

    3

    Version

    0.0.4

    License

    MIT

    Last publish

    Collaborators

    • m-onz