Current section
Files
Jump to
Current section
Files
src/bright.erl
-module(bright).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([init/2, update/2, compute/2, schedule/2, unwrap/1, state/1, computed/1, step/2, start/2, lazy_compute/3, lazy_schedule/3]).
-export_type([bright/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.
-opaque bright(MUA, MUB) :: {bright,
MUA,
MUB,
list(gleam@dynamic:dynamic_()),
list(gleam@dynamic:dynamic_())}.
-file("src/bright.gleam", 21).
?DOC(
" Creates the initial `Bright`. `state` & `computed` should be initialised with\n"
" their correct empty initial state.\n"
).
-spec init(MUC, MUD) -> bright(MUC, MUD).
init(State, Computed) ->
{bright, State, Computed, [], []}.
-file("src/bright.gleam", 60).
?DOC(
" Update state & effects during update cycle. Use it a way to update your state\n"
" stored in `Bright`, and chain them with other `bright` calls.\n"
"\n"
" ```gleam\n"
" pub fn update(model: Bright(state, computed), msg: Msg) {\n"
" use model <- bright.start(model)\n"
" // Run an update, and returns #(Bright(state, computed), Effect(msg)).\n"
" bright.update(model, update_state(_, msg))\n"
" }\n"
" ```\n"
).
-spec update(
{bright(MUT, MUU), lustre@effect:effect(MUX)},
fun((MUT) -> {MUT, lustre@effect:effect(MUX)})
) -> {bright(MUT, MUU), lustre@effect:effect(MUX)}.
update(Bright, Update_) ->
{Bright@1, Effects} = Bright,
{State, Effect} = Update_(erlang:element(2, Bright@1)),
{begin
_record = Bright@1,
{bright,
State,
erlang:element(3, _record),
erlang:element(4, _record),
erlang:element(5, _record)}
end,
lustre@effect:batch([Effects, Effect])}.
-file("src/bright.gleam", 83).
?DOC(
" Derives data from the `data` state, and potentially the current `computed`\n"
" state. `compute` will run **at every render**, so be careful with computations\n"
" as they can block paint or actors.\n"
"\n"
" ```gleam\n"
" pub fn update(model: Bright(state, computed), msg: Msg) {\n"
" use model <- bright.start(model)\n"
" model\n"
" |> bright.update(update_state(_, msg))\n"
" |> bright.compute(fn (d, c) { Computed(..c, field1: computation1(d)) })\n"
" |> bright.compute(fn (d, c) { Computed(..c, field2: computation2(d)) })\n"
" |> bright.compute(fn (d, c) { Computed(..c, field3: computation3(d)) })\n"
" }\n"
" ```\n"
).
-spec compute(
{bright(MVD, MVE), lustre@effect:effect(MVH)},
fun((MVD, MVE) -> MVE)
) -> {bright(MVD, MVE), lustre@effect:effect(MVH)}.
compute(Bright, Compute_) ->
gleam@pair:map_first(
Bright,
fun(Bright@1) ->
_pipe = Compute_(
erlang:element(2, Bright@1),
erlang:element(3, Bright@1)
),
(fun(Computed) -> _record = Bright@1,
{bright,
erlang:element(2, _record),
Computed,
erlang:element(4, _record),
erlang:element(5, _record)} end)(_pipe)
end
).
-file("src/bright.gleam", 109).
?DOC(
" Plugs in existing `state` and `computed` state, to issue some side-effects,\n"
" when your application needs to run side-effects depending on the current state.\n"
"\n"
" ```gleam\n"
" pub fn update(model: Bright(state, computed), msg: Msg) {\n"
" use model <- bright.start(model)\n"
" model\n"
" |> bright.update(update_state(_, msg))\n"
" |> bright.schedule(model, fn (state, computed) {\n"
" use dispatch <- effect.from\n"
" case state.field == 10 {\n"
" True -> dispatch(my_msg)\n"
" False -> Nil\n"
" }\n"
" })\n"
" }\n"
" ```\n"
).
-spec schedule(
{bright(MVM, MVN), lustre@effect:effect(MVQ)},
fun((MVM, MVN) -> lustre@effect:effect(MVQ))
) -> {bright(MVM, MVN), lustre@effect:effect(MVQ)}.
schedule(Bright, Schedule_) ->
{Bright@1, Effects} = Bright,
Effect = Schedule_(erlang:element(2, Bright@1), erlang:element(3, Bright@1)),
{Bright@1, lustre@effect:batch([Effects, Effect])}.
-file("src/bright.gleam", 196).
?DOC(
" Extracts `state` & `computed` states from `Bright`.\n"
"\n"
" ```gleam\n"
" pub fn view(model: Bright(state, computed)) {\n"
" let #(state, computed) = bright.unwrap(model)\n"
" html.div([], [\n"
" // Use state or computed here.\n"
" ])\n"
" }\n"
" ```\n"
).
-spec unwrap(bright(MWR, MWS)) -> {MWR, MWS}.
unwrap(Bright) ->
{erlang:element(2, Bright), erlang:element(3, Bright)}.
-file("src/bright.gleam", 210).
?DOC(
" Extracts `state` state from `Bright`.\n"
"\n"
" ```gleam\n"
" pub fn view(model: Bright(state, computed)) {\n"
" let state = bright.state(model)\n"
" html.div([], [\n"
" // Use state here.\n"
" ])\n"
" }\n"
" ```\n"
).
-spec state(bright(MWV, any())) -> MWV.
state(Bright) ->
erlang:element(2, Bright).
-file("src/bright.gleam", 224).
?DOC(
" Extracts `computed` state from `Bright`.\n"
"\n"
" ```gleam\n"
" pub fn view(model: Bright(state, computed)) {\n"
" let computed = bright.computed(model)\n"
" html.div([], [\n"
" // Use computed here.\n"
" ])\n"
" }\n"
" ```\n"
).
-spec computed(bright(any(), MXA)) -> MXA.
computed(Bright) ->
erlang:element(3, Bright).
-file("src/bright.gleam", 246).
?DOC(
" Allows to run multiple `update` on multiple `Bright` in the same update cycle.\n"
" Every call to step with compute a new `Bright`, and will let you chain the\n"
" steps.\n"
"\n"
" ```gleam\n"
" pub type Model {\n"
" Model(\n"
" fst_bright: Bright(state, computed),\n"
" snd_bright: Bright(state, computed),\n"
" )\n"
" }\n"
"\n"
" fn update(model: Model, msg: Msg) {\n"
" use fst_bright <- bright.step(update_fst(model.fst_bright, msg))\n"
" use snd_bright <- bright.step(update_snd(model.snd_bright, msg))\n"
" #(Model(fst_bright:, snd_bright:), effect.none())\n"
" }\n"
" ```\n"
).
-spec step(
{bright(MXD, MXE), lustre@effect:effect(MXH)},
fun((bright(MXD, MXE)) -> {MXL, lustre@effect:effect(MXH)})
) -> {MXL, lustre@effect:effect(MXH)}.
step(Bright, Next) ->
{Bright@1, Effs} = Bright,
{Model, Effs_} = Next(Bright@1),
{Model, lustre@effect:batch([Effs, Effs_])}.
-file("src/bright.gleam", 281).
-spec panic_if_different_computations_count(list(any()), list(any())) -> nil.
panic_if_different_computations_count(Old_computations, Computations) ->
Count = erlang:length(Old_computations),
gleam@bool:guard(
Count =:= 0,
nil,
fun() ->
Is_same_count = Count =:= erlang:length(Computations),
gleam@bool:guard(
Is_same_count,
nil,
fun() -> erlang:error(#{gleam_error => panic,
message => <<"Memoized computed should be consistent over time, otherwise memo can not work."/utf8>>,
module => <<"bright"/utf8>>,
function => <<"panic_if_different_computations_count"/utf8>>,
line => 289}) end
)
end
).
-file("src/bright.gleam", 38).
?DOC(
" Start the Bright update cycle. Use it as a way to trigger the start of `Bright`\n"
" computations, and chain them with other `bright` calls. `start` handles all\n"
" of the hard work, of turning a `Bright(state, computed)` into a\n"
" `#(Bright(state, computed), Effect(msg))`, and will take care that your\n"
" `Bright(state, computed)` is always consistent over multiple update cycles.\n"
"\n"
" ```gleam\n"
" pub fn update(model: Bright(state, computed), msg: Msg) {\n"
" // Starts the update cycle, and returns #(Bright(state, computed), Effect(msg)).\n"
" use model <- bright.start(model)\n"
" bright.update(model, update_state(_, msg))\n"
" }\n"
" ```\n"
).
-spec start(
bright(MUF, MUG),
fun(({bright(MUF, MUG), lustre@effect:effect(MUL)}) -> {bright(MUF, MUG),
lustre@effect:effect(MUL)})
) -> {bright(MUF, MUG), lustre@effect:effect(MUL)}.
start(Bright, Next) ->
Old_computations = erlang:element(5, Bright),
gleam@pair:map_first(
Next({Bright, lustre@effect:none()}),
fun(New_data) ->
panic_if_different_computations_count(
Old_computations,
erlang:element(4, New_data)
),
Past_selections = lists:reverse(erlang:element(4, New_data)),
_record = New_data,
{bright,
erlang:element(2, _record),
erlang:element(3, _record),
[],
Past_selections}
end
).
-file("src/bright.gleam", 296).
?DOC(
" Optimization on JS, to ensure that two data sharing the referential equality\n"
" will shortcut the comparison. Useful when performance are a thing in client\n"
" browser. Otherwise, rely on Erlang equality.\n"
).
-spec are_dependencies_equal(any(), any()) -> boolean().
are_dependencies_equal(A, B) ->
gleam_stdlib:identity(A) =:= gleam_stdlib:identity(B).
-file("src/bright.gleam", 255).
-spec lazy_wrap(
{bright(MXO, MXP), lustre@effect:effect(MXS)},
fun((MXO) -> MXU),
fun(({bright(MXO, MXP), lustre@effect:effect(MXS)}, fun((MXO, MXP) -> MXY)) -> {bright(MXO, MXP),
lustre@effect:effect(MXS)}),
fun((MXO, MXP, MXU) -> MXY)
) -> {bright(MXO, MXP), lustre@effect:effect(MXS)}.
lazy_wrap(Bright, Selector, Setter, Compute_) ->
Selected_data = Selector(erlang:element(2, (erlang:element(1, Bright)))),
Selections = [gleam_stdlib:identity(Selected_data) |
erlang:element(4, (erlang:element(1, Bright)))],
Compute_@1 = fun(Data, Computed) ->
Compute_(Data, Computed, Selected_data)
end,
Bright@1 = {begin
_record = erlang:element(1, Bright),
{bright,
erlang:element(2, _record),
erlang:element(3, _record),
Selections,
erlang:element(5, _record)}
end,
erlang:element(2, Bright)},
case erlang:element(5, (erlang:element(1, Bright@1))) of
[] ->
Setter(Bright@1, Compute_@1);
[Value | Past_selections] ->
_pipe = {begin
_record@1 = erlang:element(1, Bright@1),
{bright,
erlang:element(2, _record@1),
erlang:element(3, _record@1),
erlang:element(4, _record@1),
Past_selections}
end,
erlang:element(2, Bright@1)},
case are_dependencies_equal(Value, Selected_data) of
true ->
fun gleam@function:identity/1;
false ->
fun(_capture) -> Setter(_capture, Compute_@1) end
end(_pipe)
end.
-file("src/bright.gleam", 142).
?DOC(
" Derives data like [`compute`](#compute) lazily. `lazy_compute` accepts a\n"
" selector as second argument. Each time the selector returns a different data\n"
" than previous run, the computation will run. Otherwise, nothing happens.\n"
" The computation function will receive `state`, `computed` and the selected\n"
" data (i.e. the result from your selector function), in case accessing the\n"
" selected data is needed.\n"
"\n"
" ```gleam\n"
" pub fn update(model: Bright(state, computed), msg: Msg) {\n"
" use model <- bright.start(model)\n"
" model\n"
" |> bright.update(update_state(_, msg))\n"
" // Here, selected is always the result state.field / 10 (the result from selector).\n"
" |> bright.lazy_compute(selector, fn (d, c, selected) { Computed(..c, field1: computation1(d, selected)) })\n"
" |> bright.lazy_compute(selector, fn (d, c, selected) { Computed(..c, field2: computation2(d, selected)) })\n"
" |> bright.lazy_compute(selector, fn (d, c, selected) { Computed(..c, field3: computation3(d, selected)) })\n"
" }\n"
"\n"
" /// Use it with lazy_compute to recompute only when the field when\n"
" /// { old_state.field / 10 } != { state.field / 10 }\n"
" fn selector(d, _) {\n"
" d.field / 10\n"
" }\n"
" ```\n"
).
-spec lazy_compute(
{bright(MVW, MVX), lustre@effect:effect(MWA)},
fun((MVW) -> MWC),
fun((MVW, MVX, MWC) -> MVX)
) -> {bright(MVW, MVX), lustre@effect:effect(MWA)}.
lazy_compute(Bright, Selector, Compute_) ->
lazy_wrap(Bright, Selector, fun compute/2, Compute_).
-file("src/bright.gleam", 178).
?DOC(
" Plugs in existing `state` like [`schedule`](#schedule) lazily. `lazy_schedule` accepts\n"
" a selector as second argument. Each time the selector returns a different data\n"
" than previous run, the computation will run. Otherwise, nothing happens.\n"
" The scheduling function will receive `state`, `computed` and the selected\n"
" data (i.e. the result from your selector function), in case accessing the\n"
" selected data is needed.\n"
"\n"
" ```gleam\n"
" pub fn update(model: Bright(state, computed), msg: Msg) {\n"
" use model <- bright.start(model)\n"
" model\n"
" |> bright.update(update_state(_, msg))\n"
" // selected is equal to state.field / 10 (the result from selector).\n"
" |> bright.lazy_schedule(selector, fn (state, computed, selected) {\n"
" use dispatch <- effect.from\n"
" case selected == 10 {\n"
" True -> dispatch(my_msg)\n"
" False -> Nil\n"
" }\n"
" })\n"
" }\n"
"\n"
" /// Use it with lazy_schedule to recompute only when the field when\n"
" /// { old_state.field / 10 } != { state.field / 10 }\n"
" fn selector(state, _) {\n"
" state.field / 10\n"
" }\n"
" ```\n"
).
-spec lazy_schedule(
{bright(MWG, MWH), lustre@effect:effect(MWK)},
fun((MWG) -> MWM),
fun((MWG, MWH, MWM) -> lustre@effect:effect(MWK))
) -> {bright(MWG, MWH), lustre@effect:effect(MWK)}.
lazy_schedule(Bright, Selector, Schedule_) ->
lazy_wrap(Bright, Selector, fun schedule/2, Schedule_).