#Subscriptions

    The sub package models long-lived external signals that should feed back into your TEA update loop.

    Unlike Cmd, which runs once, a Sub stays installed until your app stops returning it.

    #Basic shape

    In Rabbita, subscriptions are usually returned from the subscriptions callback of create_state.

    ///|
    fn subscriptions(model : Model, emit : Emit[Msg]) -> @sub.Sub {
    if model.running {
    @sub.every(1000, emit(Tick))
    } else {
    @sub.none
    }
    }

    #Building subscriptions

    Use none when nothing should be active:

    ///|
    test "sub none" {
    let _ : Sub = @sub.none
    }

    Use batch to combine multiple subscriptions:

    ///|
    test "sub batch" {
    let _ : Sub = @sub.batch([none, every(1000, @cmd.none)])
    }

    #Emitting messages

    Event-based subscriptions usually work with emit.map(...), just like HTML event handlers.

    ///|
    enum Msg {
    Resized(ViewPort)
    MouseMoved(Mouse)
    }

    ///|
    fn subscriptions(_model : Model, emit : Emit[Msg]) -> @sub.Sub {
    @sub.batch([
    @sub.on_resize(v => emit(Resized(v))),
    @sub.on_mouse_move(m => emit(MouseMoved(m))),
    ])
    }

    RunningSub

    pub(all) struct RunningSub {
    unload : (&
    Scheduler
    ) -> Unit
    update_tagger : (Error) -> Unit
    }

    Scope

    pub(all) enum Scope {
    Local
    Global
    }

    Sub

    type Sub

    A long-lived subscription managed by the Rabbita runtime.

    Use subscriptions to listen to external signals such as timers, window resize, scrolling, keyboard input, visibility changes, or WebSocket events.

    SubLoader

    pub(all) struct SubLoader((Error, &
    Scheduler
    ) -> RunningSub?)

    Custom subscriptions loader.

    batch

    fn batch(xs : Array[Sub]) -> Sub

    Combine multiple subscriptions into one.

    If multiple subscriptions use the same internal key, the later one wins.

    custom_sub

    fn custom_sub(key : String, scope : Scope, payload : Error, loader : SubLoader) -> Sub

    Create a custom subscription

    none

    let none : Sub

    A subscription that does nothing.

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io