npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

actorjs-core

v1.0.11

Published

ActorJs Core

Downloads

29

Readme

ActorJs Core

ActorJs is a basic implementation of an actor model. The implementation is heavily based on the Akka.

ActorJs works with nodejs as well in the browser. This makes that it easy integrate client and server.

Javascript is single threaded language. So why does it make sens to write an actor model in this language. Things like thread polling are not applicable for this language. Although there are advantages of an actor system which can be use full in javascript.

To make your application vertical and horizontal scalable actor model can be very useful. By making use of remoting different of instances of the application can pass messages.

Event sourcing is another advantage which can be leveraged from the actor model. By persisting events and replay them to get back the state.

Installation

Install it

npm i actorjs-core

Use it in nodejs

var actorJsCore = require('actor-core');

Use it in web

var actorJsCore =  actorjs.core

Actors

Actors are defined as an object or a function. The actor only requires a single receive function.

Actor as object

var MyActor = {
    receive: function(msg){
        console.log(msg)
    }
}

Actor as function

function MyActor () {
    this.receive = function(msg){
        console.log(msg)
    }
}

An actor is instantiate by making use of the actorOf function. This function can be called on the system or on an other actor. By using the function on a actor hierarchy of actors can be created.

var system = new ActorSystem('MySystem');
var actorRef = system.actorOf(MyActor);
var ref = actorRef.actorOf(MyActor);

Self

With self a message can be send to the actor its own inbox.

function actor () {
 
  this.receive = function(msg){
    this.context.self.tell("Hello Parent!")
  }
} 

Parent

With parent a message can be send to the overlying actor in the system.

function actor () {
  
  this.receive = function(msg){
    this.context.parent.tell("Hello Parent!")
  }
} 

Sender

Sender gives the ability to reply a message to the sending actor. To be able to send back a message to the actor a reference is needed to the sending actorRef. To provide this actorRef an reference to the actor needs to be provided will sending a message. This is done by passing it to the second argument of the tell function. The following example will end up in a endless loop but to get the idea.

function SendActor(actorRef) {
    this.receive = function (message) {
        actorRef.tell("Hello World!", this);
    }
}

function ActorReply() {
    this.receive = function (message) {
        this.sender.tell("Hello Back!")
    }
}

var actorReplyRef = system.actorOf(new ActorReply());
var actorSendRef = system.actorOf(new ActorSend(actorReplyRef))

Forward

Forwarding messages sends a message to the next actor but keeps the original sender. This is done by giving te sender as the second argument of the tell function. Example also end up in endless loop.

function SendActor(forwardActorRef) {
    this.receive = function (message) {
        forwardActorRef.tell("Hello World!", this);
    }
}

function ForwardActor(replyActorRef) {
    this.receive = function (message) {
        replyActorRef.tell(message, this.sender)
    }
}

function ReplyActor() {
    this.sender.tell("Hello Back!")
}

var system = new ActorSystem('my-sys');

var replyActorRef = system.actorOf(new ReplyActor());
var forwardActorRef = system.actorOf(new ForwardActor(replyActorRef));
var sendActorRef = system.actorOf(new SendActor(forwardActorRef))

Messages

Messages are helper classes to generate messages in a predefined way. These messages are understood by the corresponding matchers.

Key Value Message

Key Value Messages are messages which hold one object with one key value pair. The KeyValueMacher is able to match on the these messages.

ActorMessages.KeyValueMessage(<key>, <value>);

This produces the following message

{ <key>: <value> }

Matchers

Matchers are introduced to easy match on incoming messages. There are different matchers which can be used.

Key Value Matcher

Key Value Matcher assumes that the message which is send has the following structure.

{ <key>: <value> }

Key Value Matcher can be used as follow

ActorMatcher.KeyValueMatcher({
    <key>: function(<value>){
        // do something wtith <value>
    }
);