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, compute/2, guard/2, view/2, step/2, return/1, lazy_compute/3, lazy_guard/3, update/3]).
-export_type([bright/2]).
-opaque bright(AHGB, AHGC) :: {bright,
AHGB,
AHGC,
list(gleam@dynamic:dynamic_()),
list(gleam@dynamic:dynamic_()),
list(gleam@dynamic:dynamic_())}.
-file("/Users/doctor/Workspace/bright/src/bright.gleam", 16).
-spec are_referentially_equal(any(), any()) -> boolean().
are_referentially_equal(A, B) ->
gleam_stdlib:identity(A) =:= gleam_stdlib:identity(B).
-file("/Users/doctor/Workspace/bright/src/bright.gleam", 34).
-spec init(AHGH, AHGI) -> bright(AHGH, AHGI).
init(Data, Computed) ->
{bright, Data, Computed, [], [], []}.
-file("/Users/doctor/Workspace/bright/src/bright.gleam", 80).
-spec compute(bright(AHGY, AHGZ), fun((AHGY, AHGZ) -> AHGZ)) -> bright(AHGY, AHGZ).
compute(Bright, Compute_) ->
_pipe = Compute_(erlang:element(2, Bright), erlang:element(3, Bright)),
(fun(Computed) -> erlang:setelement(3, Bright, Computed) end)(_pipe).
-file("/Users/doctor/Workspace/bright/src/bright.gleam", 102).
-spec guard(
bright(AHHE, AHHF),
fun((AHHE, AHHF) -> lustre@effect:effect(any()))
) -> bright(AHHE, AHHF).
guard(Bright, Guard_) ->
_pipe = Guard_(erlang:element(2, Bright), erlang:element(3, Bright)),
_pipe@1 = gleam_stdlib:identity(_pipe),
_pipe@2 = gleam@list:prepend(erlang:element(6, Bright), _pipe@1),
(fun(Effects) -> erlang:setelement(6, Bright, Effects) end)(_pipe@2).
-file("/Users/doctor/Workspace/bright/src/bright.gleam", 179).
-spec view(bright(AHIC, AHID), fun((AHIC, AHID) -> AHIG)) -> AHIG.
view(Bright, Viewer) ->
Viewer(erlang:element(2, Bright), erlang:element(3, Bright)).
-file("/Users/doctor/Workspace/bright/src/bright.gleam", 204).
-spec step(
{bright(AHIH, AHII), lustre@effect:effect(AHIL)},
fun((bright(AHIH, AHII)) -> {AHIP, lustre@effect:effect(AHIL)})
) -> {AHIP, lustre@effect:effect(AHIL)}.
step(Bright, Next) ->
{Bright@1, Effs} = Bright,
{Model, Effs_} = Next(Bright@1),
{Model, lustre@effect:batch([Effs, Effs_])}.
-file("/Users/doctor/Workspace/bright/src/bright.gleam", 214).
-spec return(AHIS) -> {AHIS, lustre@effect:effect(any())}.
return(A) ->
{A, lustre@effect:none()}.
-file("/Users/doctor/Workspace/bright/src/bright.gleam", 218).
-spec lazy_wrap(
bright(AHIU, AHIV),
fun((AHIU) -> any()),
fun((bright(AHIU, AHIV), fun((AHIU, AHIV) -> AHJB)) -> bright(AHIU, AHIV)),
fun((AHIU, AHIV) -> AHJB)
) -> bright(AHIU, AHIV).
lazy_wrap(Bright, Selector, Setter, Compute_) ->
Selected_data = Selector(erlang:element(2, Bright)),
Selections = [gleam_stdlib:identity(Selected_data) |
erlang:element(4, Bright)],
Bright@1 = erlang:setelement(4, Bright, Selections),
case erlang:element(5, Bright@1) of
[] ->
Setter(Bright@1, Compute_);
[Value | Past_selections] ->
_pipe = erlang:setelement(5, Bright@1, Past_selections),
case are_referentially_equal(Value, Selected_data) of
true ->
fun gleam@function:identity/1;
false ->
fun(_capture) -> Setter(_capture, Compute_) end
end(_pipe)
end.
-file("/Users/doctor/Workspace/bright/src/bright.gleam", 131).
-spec lazy_compute(
bright(AHHM, AHHN),
fun((AHHM) -> any()),
fun((AHHM, AHHN) -> AHHN)
) -> bright(AHHM, AHHN).
lazy_compute(Bright, Selector, Compute_) ->
lazy_wrap(Bright, Selector, fun compute/2, Compute_).
-file("/Users/doctor/Workspace/bright/src/bright.gleam", 160).
-spec lazy_guard(
bright(AHHT, AHHU),
fun((AHHT) -> any()),
fun((AHHT, AHHU) -> lustre@effect:effect(any()))
) -> bright(AHHT, AHHU).
lazy_guard(Bright, Selector, Guard_) ->
lazy_wrap(Bright, Selector, fun guard/2, Guard_).
-file("/Users/doctor/Workspace/bright/src/bright.gleam", 240).
-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 => 248}) end
)
end
).
-file("/Users/doctor/Workspace/bright/src/bright.gleam", 51).
-spec update(
bright(AHGL, AHGM),
fun((AHGL) -> {AHGL, lustre@effect:effect(AHGP)}),
fun((bright(AHGL, AHGM)) -> bright(AHGL, AHGM))
) -> {bright(AHGL, AHGM), lustre@effect:effect(AHGP)}.
update(Bright, Update_, Next) ->
Old_computations = erlang:element(5, Bright),
{Data, Effs} = Update_(erlang:element(2, Bright)),
Bright@1 = erlang:setelement(2, Bright, Data),
New_data = Next(Bright@1),
All_effects = begin
_pipe = gleam_stdlib:identity(erlang:element(6, New_data)),
_pipe@1 = bright_ffi:coerce(_pipe),
lists:reverse(_pipe@1)
end,
panic_if_different_computations_count(
Old_computations,
erlang:element(4, New_data)
),
Past_selections = lists:reverse(erlang:element(4, New_data)),
_pipe@2 = erlang:setelement(
6,
erlang:setelement(
4,
erlang:setelement(5, New_data, Past_selections),
[]
),
[]
),
gleam@pair:new(
_pipe@2,
lustre@effect:batch([Effs, lustre@effect:batch(All_effects)])
).