Theater v0.1.0 Theater.Demo.Order View Source

A more complex demonstration Actor.

This is a simple actor for demonstrating how implementing an Actor works. It represents an order and goes through several stages like a state machine. It can also interact with other Actors.

Items can be added to an order and when payment is received the order is closed and ready for shipping. Once an order is shipped it is done and we close it. The cutomer name can be set at any point, and can be copied to another order.

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

Create a new order

Process a message for a Counter

Invoked to determine how long the Actor should live without receiving any messages

Link to this section Functions

Create a new order.

Orders start with no items and no customer name, but then process messages as normal.

Process a message for a Counter.

Sending {:add, item} will add item to the order.

Sending :pay will stop accepting items and mark it ready to be shipped if it is open.

Sending :ship will close out the order if it is ready to be shipped.

Sending {:set_name, name} will set the customer name to name.

Sending {:copy_name_to, id} will copy the customer name from this order to order id.

Sending {:get, pid} will send a message to pid of the form {:order, id, items, name}.

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.