aktor-js
TypeScript icon, indicating that this package has built-in type declarations

1.4.0 • Public • Published

Actor Model in JS

Exploring the potential benefits of implementing APIs in the style of the Actor Model, even in single-threaded programming environments such as Javascript.

TODO

Actor system ActorSystem

  • createSystem()
  • system.actorOf(IActorFactory) -> ActorRef

Actors Actor

  • actor.tell() - fire & forget message
  • actor.ask() - ask & await async response from an actor
  • actor.stop() - send a message instructing an actor to terminate

Actor context ActorContext

  • context.stop(IActorRef) - allow an actor to be stopped via a ref
  • context.gracefulStop(IActorRef) - allow an actor to be stopped via a ref with confirmation (for sequencing etc)
  • context.actorOf(IActorFactory) - allow an actor to create more actors
  • context.parent - allow an actor to access it's parent (in order to send it messages)
  • context.self - allow an actor to access it's own ref (in order to send it's self messages)
  • context.become(newHandler) - designate a new handler for future messages http://doc.akka.io/docs/akka/current/scala/actors.html#Graceful_Stop
  • context.actorSelection(lookup: string) - allow actor lookups via paths, such as /system /deadletter etc

Actor receive method

  • receive(payload, message, sender)

  • sender.reply() for replying directly to a message

  • mappedMethods define 1 function per message name

  • handle cancellation of pending message should a new one override it

  • pass state into mapped methods for encapsulated state management

  • sender - allow every message access to an ActorRef that allow communication with the sender

    receive (payload message sender)
      switch payload
        case 'ping' sender.reply 'pong'
        default sender.reply 'missing'
      
    // callsite
    actor.ask 'ping'
      |> resp console.log 'resp:' + resp

Actor Lifecycle

  • actorOf(...)

    • path is reserved
    • uuid is assigned
    • actor instance is created
    • preStart is called on instance
  • Incarnation (restarting)

    • preRestart called on old instance
    • new instance replaces old
    • postRestart called on new instance
  • Stop, context.stop() or PoisonPill

    • postStop is called on instance
    • actor is removed from the internal system register
    • Terminated is sent to watchers
    • path is free to be used again
  • graceful stop http://doc.akka.io/docs/akka/current/scala/actors.html#Graceful_Stop

    • become() - designate a new handler for future messages

Actor References, Paths and Addresses

  • actorOf() only ever creates a new actor, and it creates it as a direct child of the context on which this method is invoked (which may be any actor or actor system).
  • /deadletters all messages sent to stopped or non-existing actors are re-routed here
  • /system is the guardian actor for all system-created top-level actors, e.g. logging listeners or actors automatically deployed by configuration at the start of the actor system.

Readme

Keywords

Package Sidebar

Install

npm i aktor-js

Weekly Downloads

17

Version

1.4.0

License

ISC

Last publish

Collaborators

  • shakyshane