-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(MNJ, MNK) :: {bright, MNJ, MNK, 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(MNL, MNM) -> bright(MNL, MNM). 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(MOC, MOD), lustre@effect:effect(MOG)}, fun((MOC) -> {MOC, lustre@effect:effect(MOG)}) ) -> {bright(MOC, MOD), lustre@effect:effect(MOG)}. 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(MOM, MON), lustre@effect:effect(MOQ)}, fun((MOM, MON) -> MON) ) -> {bright(MOM, MON), lustre@effect:effect(MOQ)}. 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(MOV, MOW), lustre@effect:effect(MOZ)}, fun((MOV, MOW) -> lustre@effect:effect(MOZ)) ) -> {bright(MOV, MOW), lustre@effect:effect(MOZ)}. 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(MQA, MQB)) -> {MQA, MQB}. 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(MQE, any())) -> MQE. 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(), MQJ)) -> MQJ. 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(MQM, MQN), lustre@effect:effect(MQQ)}, fun((bright(MQM, MQN)) -> {MQU, lustre@effect:effect(MQQ)}) ) -> {MQU, lustre@effect:effect(MQQ)}. 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(MNO, MNP), fun(({bright(MNO, MNP), lustre@effect:effect(MNU)}) -> {bright(MNO, MNP), lustre@effect:effect(MNU)}) ) -> {bright(MNO, MNP), lustre@effect:effect(MNU)}. 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) -> gleam_stdlib:identity(A) =:= gleam_stdlib:identity(B). -file("src/bright.gleam", 256). -spec lazy_wrap( {bright(MQX, MQY), lustre@effect:effect(MRB)}, fun((MQX) -> MRD), fun(({bright(MQX, MQY), lustre@effect:effect(MRB)}, fun((MQX, MQY) -> MRH)) -> {bright(MQX, MQY), lustre@effect:effect(MRB)}), fun((MQX, MQY, MRD) -> MRH) ) -> {bright(MQX, MQY), lustre@effect:effect(MRB)}. 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(MPF, MPG), lustre@effect:effect(MPJ)}, fun((MPF) -> MPL), fun((MPF, MPG, MPL) -> MPG) ) -> {bright(MPF, MPG), lustre@effect:effect(MPJ)}. 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(MPP, MPQ), lustre@effect:effect(MPT)}, fun((MPP) -> MPV), fun((MPP, MPQ, MPV) -> lustre@effect:effect(MPT)) ) -> {bright(MPP, MPQ), lustre@effect:effect(MPT)}. lazy_schedule(Bright, Selector, Schedule_) -> lazy_wrap(Bright, Selector, fun schedule/2, Schedule_).