#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 a cell.

    ///|
    fn subscriptions(dispatch : Dispatch[Msg], model : Model) -> @sub.Sub {
    if model.running {
    @sub.every(1000, dispatch(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)])
    }

    #Dispatching messages

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

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

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

    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, or visibility changes.

    batch

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

    Combine multiple subscriptions into one.

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

    every

    fn every(ms : Int, cmd :
    Cmd
    ) -> Sub

    Repeatedly enqueue cmd every ms milliseconds.

    none

    let none : Sub

    A subscription that does nothing.

    on_animation_frame

    fn on_animation_frame(msg :
    Dispatch
    [Double]) -> Sub

    Subscribe to requestAnimationFrame ticks.

    The handler receives the browser timestamp for each frame while the subscription is active.

    on_key_down

    Subscribe to document keydown events.

    The handler receives a normalized Keyboard value from @common.

    on_key_up

    Subscribe to document keyup events.

    The handler receives a normalized Keyboard value from @common.

    on_mouse_move

    Subscribe to document mousemove events.

    The handler receives a normalized Mouse value from @common.

    on_resize

    Subscribe to window resize events.

    The handler receives the latest viewport width and height.

    on_scroll

    Subscribe to window scroll events.

    This reports page-level scrolling on window, not scrolling of an inner element. The handler receives the current scroll offset and document scroll size as a Scroll value.

    on_visibility_change

    fn on_visibility_change(msg :
    Dispatch
    [Bool]) -> Sub

    Subscribe to document visibility changes.

    The handler receives true when the document is hidden, and false when it becomes visible again.

    Source Files