Actor

Processes

Build and start a process that handles one message at a time.

Pipe new → onMessage → optional named → start. The handler returns continue nextState or stop. Most apps use higher-level wrappers ([Database.Repo], [Platform], [Jobs] supervised helpers) instead of calling `Actor` directly; reach for this when you need a custom long-lived process.

type Msg
    = Incr
    | Get (Process.Subject Int)

name : Process.Name Msg
name =
    Process.newName "counter"

handle : Int -> Msg -> Actor.Next Int Msg
handle n msg =
    case msg of
        Incr ->
            Actor.continue (n + 1)

        Get reply ->
            -- send the reply, then keep the same count
            Actor.continue n

Actor.new 0
    |> Actor.onMessage handle
    |> Actor.named name
    |> Actor.start

Return continue or stop from every branch. See [Process] for subjects and names. For TEA-shaped background work, prefer [Platform] `worker` and `supervised`.

Types

Builder

type Builder state message a = Builder

An actor that is still being configured.

Examples

Actor.new initialState : Builder state message (Subject message)

Next

type Next state message = Next

What an actor does after handling one message.

Examples

Actor.continue (n + 1)

Started

type Started a = Started Pid a

A process that started and produced a (usually a Subject).

Examples

Ok (Actor.Started pid subject)

StartError

type StartError
= InitTimeout
| InitFailed String

Why starting a process failed.

Examples

Err Actor.InitTimeout

Values

new

new : state -> Builder state message (Subject message)

Start an actor builder from its initial state.

Examples

Actor.new 0

onMessage

onMessage : (state -> message -> Next state message) -> Builder state message a -> Builder state message a

Handle each message with the current state. Return continue or stop.

Examples

Actor.onMessage handle builder

named

named : Name message -> Builder state message a -> Builder state message a

Register the actor under a process name so others can look it up.

Examples

Actor.named name builder

start

start : Builder state message a -> Result StartError (Started a)

Start the actor and return its pid plus the builder's result (often a Subject).

Examples

Actor.start builder

continue

continue : state -> Next state message

Keep the actor running with the next state.

Examples

Actor.continue nextState

stop

stop : Next state message

Stop the actor after this message.

Examples

Actor.stop