Current section

Files

Jump to
bright src bright.erl
Raw

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(OBD, OBE) :: {bright,
OBD,
OBE,
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(OBF, OBG) -> bright(OBF, OBG).
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(OBW, OBX), lustre@effect:effect(OCA)},
fun((OBW) -> {OBW, lustre@effect:effect(OCA)})
) -> {bright(OBW, OBX), lustre@effect:effect(OCA)}.
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(OCG, OCH), lustre@effect:effect(OCK)},
fun((OCG, OCH) -> OCH)
) -> {bright(OCG, OCH), lustre@effect:effect(OCK)}.
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(OCP, OCQ), lustre@effect:effect(OCT)},
fun((OCP, OCQ) -> lustre@effect:effect(OCT))
) -> {bright(OCP, OCQ), lustre@effect:effect(OCT)}.
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", 197).
?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(ODU, ODV)) -> {ODU, ODV}.
unwrap(Bright) ->
{erlang:element(2, Bright), erlang:element(3, Bright)}.
-file("src/bright.gleam", 211).
?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(ODY, any())) -> ODY.
state(Bright) ->
erlang:element(2, Bright).
-file("src/bright.gleam", 225).
?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(), OED)) -> OED.
computed(Bright) ->
erlang:element(3, Bright).
-file("src/bright.gleam", 247).
?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(OEG, OEH), lustre@effect:effect(OEK)},
fun((bright(OEG, OEH)) -> {OEO, lustre@effect:effect(OEK)})
) -> {OEO, lustre@effect:effect(OEK)}.
step(Bright, Next) ->
{Bright@1, Effs} = Bright,
{Model, Effs_} = Next(Bright@1),
{Model, lustre@effect:batch([Effs, Effs_])}.
-file("src/bright.gleam", 282).
-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 => 290}) 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(OBI, OBJ),
fun(({bright(OBI, OBJ), lustre@effect:effect(OBO)}) -> {bright(OBI, OBJ),
lustre@effect:effect(OBO)})
) -> {bright(OBI, OBJ), lustre@effect:effect(OBO)}.
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", 297).
?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) ->
bright_ffi:coerce(A) =:= bright_ffi:coerce(B).
-file("src/bright.gleam", 256).
-spec lazy_wrap(
{bright(OER, OES), lustre@effect:effect(OEV)},
fun((OER) -> OEX),
fun(({bright(OER, OES), lustre@effect:effect(OEV)}, fun((OER, OES) -> OFB)) -> {bright(OER, OES),
lustre@effect:effect(OEV)}),
fun((OER, OES, OEX) -> OFB)
) -> {bright(OER, OES), lustre@effect:effect(OEV)}.
lazy_wrap(Bright, Selector, Setter, Compute_) ->
Selected_data = Selector(erlang:element(2, (erlang:element(1, Bright)))),
Selections = [bright_ffi:coerce(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(OCZ, ODA), lustre@effect:effect(ODD)},
fun((OCZ) -> ODF),
fun((OCZ, ODA, ODF) -> ODA)
) -> {bright(OCZ, ODA), lustre@effect:effect(ODD)}.
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(ODJ, ODK), lustre@effect:effect(ODN)},
fun((ODJ) -> ODP),
fun((ODJ, ODK, ODP) -> lustre@effect:effect(ODN))
) -> {bright(ODJ, ODK), lustre@effect:effect(ODN)}.
lazy_schedule(Bright, Selector, Schedule_) ->
lazy_wrap(Bright, Selector, fun schedule/2, Schedule_).