Theater v0.1.0 Theater.Demo.Counter View Source
A simple demonstration Actor.
This is a simple actor for demonstrating how implementing an Actor works. It is a counter. Counters start at 0 and can be incremented. You can send the current value to a pid, or mark a counter as done and ready to be removed.
When Actors get a new message they will process it with process/3
. They
will be passed the current state of the Actor, the ID that was used to reach
this Actor, and the message being passed to it. The Actor is responsible for
processing any messages sent to it and returning a value indicating its new
state and whether to persist it. See Theater.Actor
for further
documentation.
Link to this section Summary
Functions
Invoked when a specific instance of this Actor is first instantiated
Process a message for a Counter
Invoked to determine how long the Actor should live without receiving any messages
Link to this section Functions
Invoked when a specific instance of this Actor is first instantiated.
Instance id
is being created and sent message
.
Returning {:ok, state}
or {:ok, :persist, state}
will update the Actor’s
state
, keep it running in memory, and save it in the persistence layer.
Returning {:ok, :no_persist, state}
will update the Actor’s state
and
keep it in memory, but will not save it to the persistence layer. These
changes could be lost if the Actor is stopped or moved to another node.
Returning :no_update
will keep the Actor in memory, but will not record any
changes to its state.
Returning :stop
or {:stop, :delete}
will stop the Actor, free up its
memory, and delete it from the persistence layer. Future messages to this
Actor ID will create a brand new Actor as though it didn’t already exist.
Returning {:stop, :persist, state}
will stop the Actor and free up its
memory, but will record state
to the persistence layer. Future calls to
this Actor ID will recreate it from this state
.
Returning {:stop, :no_persist}
will stop the Actor and free up its memory.
No change will be made in the persistence layer, whether it exists or not.
Callback implementation for Theater.Actor.init/2
.
Process a message for a Counter.
New Counters start at 0.
Sending :increment
will increment the counter’s value.
Sending {:get, pid}
will send a message to pid of the form {:counter, id,
value}
.
Sending :done
will stop the counter.
Invoked to determine how long the Actor should live without receiving any messages.
If, after this amount of time, the Actor has received no messages, it will be considered unneeded, it will be stopped, and its memory freed up. Any state that has not been persisted will be lost.
The result can depend on the Actor’s id
and/or state
or it can simply
return a constant. Return value should be time to live in milliseconds.
If this callback is not implemented, the default implementation by use
Theater.Actor
will a value configurable under :theater,
:default_time_to_live. If no value is configured, it will return ten minutes.
Callback implementation for Theater.Actor.time_to_live/2
.