cellium behaviour (cellium v0.1.0)

View Source

This module defines the cellium behaviour, which is the core of a Cellium application.

It's a gen_server that manages the application's state, event handling, and rendering loop. To create an application, you must implement the callback functions defined in this behaviour.

The three main callbacks are:

  • init/1: To set up the initial state of the application (the model).
  • update/2: To handle events and update the application model.
  • render/1: To define the UI by transforming the model into a widget tree.

A simple application would look like this:

-module(my_app).
-behaviour(cellium).

-export([init/1, update/2, render/1]).

init(_Args) ->
    {ok, #{text => <<"Hello, World!">>}}.

update(Model, _Msg) ->
    Model.

render(#{text := Text}) ->
    text:new(my_text, Text).

Summary

Callbacks

Invoked when the application starts. It should initialize the application's state, referred to as the 'model'.

Takes the current application Model and returns a widget tree (Layout) to be rendered on the screen. This function defines the UI of the application.

Handles incoming messages and updates the application state. Msg is the message to process, and Model is the current state. It should return the new state NewModel.

Functions

Sends an event to the application's update/2 callback for processing. This is the primary way to send messages to the running application.

Starts the Cellium application. Args is a map containing configuration, including the module that implements the cellium behaviour.

Stops the Cellium application gracefully.

Callbacks

init(Args)

-callback init(Args :: term()) -> {ok, Model :: term()} | ignore.

Invoked when the application starts. It should initialize the application's state, referred to as the 'model'.

render(Model)

-callback render(Model :: term()) -> Layout :: term().

Takes the current application Model and returns a widget tree (Layout) to be rendered on the screen. This function defines the UI of the application.

update(Model, Msg)

-callback update(Model :: term(), Msg :: term()) -> NewModel :: term().

Handles incoming messages and updates the application state. Msg is the message to process, and Model is the current state. It should return the new state NewModel.

Functions

code_change(OldVsn, State, Extra)

handle_call(Msg, From, State)

handle_cast/2

handle_event(Event)

-spec handle_event(Event :: term()) -> {ok, done}.

Sends an event to the application's update/2 callback for processing. This is the primary way to send messages to the running application.

handle_info(Msg, State)

init(Args)

render_caller(Module, Model)

start(Args)

-spec start(Args :: map()) -> {ok, pid()} | {error, any()}.

Starts the Cellium application. Args is a map containing configuration, including the module that implements the cellium behaviour.

stop()

-spec stop() -> ok.

Stops the Cellium application gracefully.

terminate(Reason, State)