1.4.0 • Published 8 years ago
aktor-js v1.4.0
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 messageactor.ask()- ask & await async response from an actoractor.stop()- send a message instructing an actor to terminate
Actor context ActorContext
context.stop(IActorRef)- allow an actor to be stopped via a refcontext.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 actorscontext.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_Stopcontext.actorSelection(lookup: string)- allow actor lookups via paths, such as/system/deadletteretc
Actor receive method
receive(payload, message, sender)sender.reply()for replying directly to a messagemappedMethodsdefine 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 senderreceive (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()orPoisonPill- postStop is called on instance
- actor is removed from the internal system register
Terminatedis sent to watcherspath 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)./deadlettersall messages sent to stopped or non-existing actors are re-routed here/systemis 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.