Platform

Processes

A BEAM program: one process owns the model and handles one message at a time (init / update / subscriptions, without a view).

Use worker for headless long-lived processes (MCP/ACP hosts, background pollers, test helpers). HTTP servers use Server.application instead, which still has init / update / subscriptions beside handle. Browser apps use Browser.element / Browser.application on the JS target.

type alias Model =
    { n : Int }

type Msg
    = Tick Time.Posix

main : Platform.Program () Model Msg
main =
    Platform.worker
        { init = \_ -> ( { n = 0 }, Cmd.none )
        , update =
            \msg model ->
                case msg of
                    Tick _ ->
                        ( { model | n = model.n + 1 }, Cmd.none )
        , subscriptions = \_ -> Time.every 1000 Tick
        }

Put another program under your server supervisor with supervised. exit stops the whole BEAM node (Mix-style CLI tasks). Commands and subscriptions are [Platform.Cmd] and [Platform.Sub].

Types

Program

type Program flags model msg = Program

How a program turns flags into a model and reacts to messages.

Examples

Platform.worker { init = init, update = update, subscriptions = subscriptions } : Program flags model msg

Values

worker

worker : { init : flags -> (model, Cmd msg) , update : msg -> model -> (model, Cmd msg) , subscriptions : model -> Sub msg } -> Program flags model msg

Run a headless TEA program. flags is often ().

Examples

Platform.worker
    { init = \_ -> ( model, Cmd.none )
    , update = update
    , subscriptions = subscriptions
    }

supervised

supervised : Program () model msg -> ChildSpecification (Subject msg)

Supervise another program. The child's subject receives that program's messages. Add the result with Supervisor.add.

Examples

Supervisor.add (Platform.supervised workerProgram) tree

exit

exit : Int -> Cmd msg

Stop the BEAM node with the given exit status. Used by Mix-style TEA CLI tasks (selm increment, and so on).

Examples

Platform.exit 0