Skip to content

Instantly share code, notes, and snippets.

@componhead
Forked from CrowdHailer/actor.js
Created March 23, 2021 10:43
Show Gist options
  • Save componhead/73f525b2b53a7eabfe3619fb91972038 to your computer and use it in GitHub Desktop.
Save componhead/73f525b2b53a7eabfe3619fb91972038 to your computer and use it in GitHub Desktop.

Revisions

  1. @CrowdHailer CrowdHailer revised this gist Jun 15, 2016. 1 changed file with 28 additions and 8 deletions.
    36 changes: 28 additions & 8 deletions actor.js
    Original file line number Diff line number Diff line change
    @@ -18,15 +18,25 @@ var actor = {
    }
    };

    actor.send(1);
    actor.send(1);
    actor.send(1);

    // console.log("hello");
    // actor.send(1);
    // actor.send(1);
    // actor.send(1);

    function End(){}

    var end = new End();

    function Actor(handler, state){
    this.handler = handler;
    this.state = state;
    }

    Actor.prototype.end = function(){
    return new End();
    };

    Actor.prototype.send = function(message){
    var self = this;
    setTimeout(function(){
    @@ -36,14 +46,24 @@ Actor.prototype.send = function(message){
    };

    Actor.prototype.receive = function(message){
    this.state = this.handler(this.state, message);
    reply = this.handler(this.state, message);
    // Could just end by throwing errors instead
    if (reply instanceof End) {
    console.log("ending");
    this.handler = function(){};
    } else {
    this.state = reply;
    }
    };

    function add(a, b){ console.log(a); return a + b; }
    function add(a, b){
    console.log(a);
    return a === 3 ? new End() : a + b;
    }

    var a = new Actor(add, 0);

    a.send(1).send(1);
    a.send(1).send(1).send(1).send(1).send(1).send(1);

    var shout = new Actor(function(state, message){
    console.log(message);
    @@ -54,8 +74,8 @@ var forward = new Actor(function(target, message){
    return target;
    }, shout);

    forward.send("first");
    shout.send("second");
    // forward.send("first");
    // shout.send("second");

    var ponger = new Actor(function(state, message){
    message.from.send(message.payload);
  2. @CrowdHailer CrowdHailer created this gist Jun 15, 2016.
    73 changes: 73 additions & 0 deletions actor.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    // Turns out this was not too difficult.
    // By using the event loop as a global mail box ordering is even guaranteed for messages between only two actors.
    // TODO would like to try and put one actor in a web worker to get some real parallism
    // TODO would like to handle errors and have the concept of a deceased actor while a reference to an actor still exists. Probably involves creating an actor system like in Scala. Eurgh.

    var actor = {
    send: function(message) {
    var self = this;
    console.log("sending to:", self);
    setTimeout(function(){
    self.receive(message);
    }, 0);
    },
    state: 0,
    receive: function(message){
    this.state += message;
    console.log(this.state);
    }
    };

    actor.send(1);
    actor.send(1);
    actor.send(1);

    function Actor(handler, state){
    this.handler = handler;
    this.state = state;
    }

    Actor.prototype.send = function(message){
    var self = this;
    setTimeout(function(){
    self.receive(message);
    });
    return self;
    };

    Actor.prototype.receive = function(message){
    this.state = this.handler(this.state, message);
    };

    function add(a, b){ console.log(a); return a + b; }

    var a = new Actor(add, 0);

    a.send(1).send(1);

    var shout = new Actor(function(state, message){
    console.log(message);
    });

    var forward = new Actor(function(target, message){
    target.send(message);
    return target;
    }, shout);

    forward.send("first");
    shout.send("second");

    var ponger = new Actor(function(state, message){
    message.from.send(message.payload);
    });

    var pinger = new Actor(function(state, message){
    if (message.ping) {
    state.send({from: this, payload: message.payload});
    } else {
    console.log(message);
    }
    }, ponger);


    pinger.send({ping: true, payload: "huzzah"});