Raxol.Core.Runtime.Directive (Raxol Core v2.6.0)

Copy Markdown View Source

Runtime-essential effect directives that any TEA application can return from update/2.

These directives need no agent context: they run purely against the caller's runtime (Process.send_after, Task.start, runtime pid signal). Agent-specific directives (shell access, inter-agent messaging, multi-message async streams) live in Raxol.Agent.Directive and depend on raxol_agent.

Built-in directives

DirectiveEffect
StopSignal the runtime to stop
ScheduleSend a message after a delay (Process.send_after)
SpawnRun a 0-arg function in a Task; its return value is sent back once

Examples

import Raxol.Core.Runtime.Directive, only: [stop: 0, schedule: 2, spawn_task: 1]

stop()
schedule(1_000, :tick)
spawn_task(fn -> {:result, expensive_op()} end)

Dispatch

Each struct implements Raxol.Core.Runtime.Directive.Executor. The runtime invokes the protocol's execute/2 with a context map containing at minimum :pid and :runtime_pid. Result messages arrive at context.pid as {:command_result, payload} (the message name is historical; both the old Command system and the directive system use it).

External packages register additional directives by defining a struct and implementing Raxol.Core.Runtime.Directive.Executor for it.

Summary

Functions

Construct a Schedule directive. interval_ms must be non-negative.

Construct a Spawn directive with a no-arg function.

Construct a Stop directive.

Types

Functions

schedule(interval_ms, payload)

Construct a Schedule directive. interval_ms must be non-negative.

spawn_task(fun)

@spec spawn_task((-> any())) :: Raxol.Core.Runtime.Directive.Spawn.t()

Construct a Spawn directive with a no-arg function.

stop(reason \\ :normal)

Construct a Stop directive.