An javascript version actor, inspired by Akka Actor
npm i js-actor --save
run your first actor is simple as follow:
import { ActorSystem } from "js-actor"
import { AbstractActor } from "js-actor"
// create an actor system
const system = ActorSystem.create("systemName")
// create a message object
class Greeting {
constructor(public message: string) {}
}
// create an actor
class MyActor extends AbstractActor {
public createReceive() {
return this.receiveBuilder()
.match(Greeting, greeting => console.log(greeting.message))
.build()
}
}
// mount actor to system, return an ActorRef object
const myActor = system.actorOf(new MyActor)
// myactor is ready an listening, send message to it.
myActor.tell(new Greeting("hello~"))
will print
hello~
ActorSystem is a heavyweight structure that theoretically should allocate thread, but in js just allocate listener. imagine your computing unit as a realword actor, the ActorSystem is the stage that give the all actor to work upon it. all actor listen ActorSystem. so create on for per logic application.
create an ActorSystem. Equal to new ActorSystem(name: string)
.
- name. ActorSystem's name. in some scene will make sense.
the event stream of the ActorSystem. All actor should listen it.
call emit on eventStream
- event. actor name
- message: Message object's entity
is equal to ActorContext.actorOf
ActorSystem implicitly initiate an rootActor that all user custom actor will mount under it. this method can get it.
is equal to ActorContext.stop.
remove all listener and clear the children of the rootActor.
expose contextual information for your actor.
actor name
return reference of self.
return the ActorSystem which the actor is using.
return the reference of sender. if there no sender, it would be null.
current scheduler of the actor.
return parent actor reference
path is composed of parent path and actor name. because root path is /
, so your first actor path is /firstname/
.your child actor path is /firstname/childname/
.and so on.
return children
regist actor to parent actor use the given name. return the reference of your actor that have mounted under parent actor.
- actor. which actor you want to mount under parent.
- name. actor name.if name is undefined, it will be random.
recursively find child use the given actor name from current actor. this method just find children of given actor.
- name. which actor name that you want to find
this method can stop self and children actor. it will remove the listener of the stopped actor. and remove the reference from parent.
- actorRef. which actor you want to stop.default is stop self.
change the Actor's behavior to become the new "Receive" handler.eg.
// create a new Receive object
const behavior = ReceiveBuilder.create().build()
// the actor will drop previous receive object and use new receive
actor.getContext().become(behavior)
return if the actor is listening.
abstract class that should be extended to create your actor.
class MyActor extends AbstractActor {
public createReceive() {
return this.receiveBuilder()
.match(Greeting, greeting => console.log(greeting.message))
.build()
}
}
return context of the actor.
equal to this.context.self
equal to this.context.sender
return new ReceiveBuilder
if this method have been implemented, it must return a Receive object. there's a convenient build-in method receiveBuilder
to help your create an Receive object.
is called when actor is started
is called after ActorContext.stop()
is invoked
is called after Receive
got error. default rise error.
handle reference to an actor, whitch may reside on an actor.
return the context of the actor
return the inside actor.
transfer message to specific actor.
- message. message object.
- sender.
targetActorRef.tell(new Greeting, this.getSelf())
a helper class to store logic and create receive object
- message. message class.
- callback. if matched. pass the message object to the callback.
just like the default in switch
.
return receive object.
mybe you would like to have a look at akka quickstart to get the intuitive sense. and contrast with official api