src/lightspeed@component@helpers.erl

-module(lightspeed@component@helpers).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/component/helpers.gleam").
-export([make/3, html_with_fingerprint/2, no_effect/1, dispatch/1, push/1, navigate/1, patch/2, subscribe/1, unsubscribe/1, shutdown/1, map_command/2, map_commands/2]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

?MODULEDOC(" Ergonomic helpers for Lightspeed components.\n").

-file("src/lightspeed/component/helpers.gleam", 7).
?DOC(" Construct a component from init/update/render functions.\n").
-spec make(
    fun(() -> HKD),
    fun((HKD, HKE) -> {HKD, list(lightspeed@component:command(HKE))}),
    fun((HKD) -> lightspeed@component:rendered())
) -> lightspeed@component:component(HKD, HKE).
make(Init, Update, Render) ->
    {component, Init, Update, Render}.

-file("src/lightspeed/component/helpers.gleam", 16).
?DOC(" Render markup with an explicit fingerprint.\n").
-spec html_with_fingerprint(binary(), binary()) -> lightspeed@component:rendered().
html_with_fingerprint(Markup, Fingerprint) ->
    {rendered, Markup, Fingerprint}.

-file("src/lightspeed/component/helpers.gleam", 24).
?DOC(" Return an update result with no commands.\n").
-spec no_effect(HKJ) -> {HKJ, list(lightspeed@component:command(any()))}.
no_effect(Model) ->
    {Model, []}.

-file("src/lightspeed/component/helpers.gleam", 29).
?DOC(" Construct a dispatch command.\n").
-spec dispatch(HKN) -> lightspeed@component:command(HKN).
dispatch(Msg) ->
    {dispatch, Msg}.

-file("src/lightspeed/component/helpers.gleam", 34).
?DOC(" Construct a pushed ISA instruction command.\n").
-spec push(lightspeed@agent@isa:instruction()) -> lightspeed@component:command(any()).
push(Instruction) ->
    {push, Instruction}.

-file("src/lightspeed/component/helpers.gleam", 39).
?DOC(" Construct a navigate command.\n").
-spec navigate(binary()) -> lightspeed@component:command(any()).
navigate(To) ->
    push({navigate, To}).

-file("src/lightspeed/component/helpers.gleam", 44).
?DOC(" Construct a patch command.\n").
-spec patch(binary(), binary()) -> lightspeed@component:command(any()).
patch(Target, Html) ->
    push({patch, Target, Html}).

-file("src/lightspeed/component/helpers.gleam", 49).
?DOC(" Construct a subscription command.\n").
-spec subscribe(binary()) -> lightspeed@component:command(any()).
subscribe(Topic) ->
    push({subscribe, Topic}).

-file("src/lightspeed/component/helpers.gleam", 54).
?DOC(" Construct an unsubscription command.\n").
-spec unsubscribe(binary()) -> lightspeed@component:command(any()).
unsubscribe(Topic) ->
    push({unsubscribe, Topic}).

-file("src/lightspeed/component/helpers.gleam", 59).
?DOC(" Construct a shutdown command.\n").
-spec shutdown(binary()) -> lightspeed@component:command(any()).
shutdown(Reason) ->
    push({shutdown, Reason}).

-file("src/lightspeed/component/helpers.gleam", 64).
?DOC(" Map the message type inside a component command.\n").
-spec map_command(lightspeed@component:command(HLB), fun((HLB) -> HLD)) -> lightspeed@component:command(HLD).
map_command(Command, With) ->
    case Command of
        no_command ->
            no_command;

        {dispatch, Inner} ->
            {dispatch, With(Inner)};

        {push, Instruction} ->
            {push, Instruction}
    end.

-file("src/lightspeed/component/helpers.gleam", 76).
?DOC(" Map message type for a list of commands.\n").
-spec map_commands(list(lightspeed@component:command(HLF)), fun((HLF) -> HLI)) -> list(lightspeed@component:command(HLI)).
map_commands(Commands, With) ->
    case Commands of
        [] ->
            [];

        [Command | Rest] ->
            [map_command(Command, With) | map_commands(Rest, With)]
    end.