Current section
Files
Jump to
Current section
Files
src/sprocket@hooks.erl
-module(sprocket@hooks).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([callback/4, client/4, effect/4, handler/3, memo/4, provider/3, reducer/4, state/3]).
-export_type([state_or_dispatch_reducer/2]).
-type state_or_dispatch_reducer(KMG, KMH) :: shutdown |
{state_reducer, gleam@erlang@process:subject(KMG)} |
{dispatch_reducer, fun((KMG, KMH) -> KMG), KMH}.
-spec maybe_trigger_update(
sprocket@context:hook_trigger(),
gleam@option:option(list(gleam@dynamic:dynamic_())),
KMM,
fun(() -> KMM)
) -> {KMM, gleam@option:option(list(gleam@dynamic:dynamic_()))}.
maybe_trigger_update(Trigger, Prev, Value, Updater) ->
case Trigger of
on_mount ->
{Updater(), {some, []}};
on_update ->
{Updater(), none};
{with_deps, Deps} ->
case Prev of
{some, Prev_deps} ->
case sprocket@context:compare_deps(Prev_deps, Deps) of
{changed, New_deps} ->
{Updater(), {some, New_deps}};
unchanged ->
{Value, Prev}
end;
none ->
{Updater(), {some, Deps}}
end
end.
-spec callback(
sprocket@context:context(),
fun(() -> nil),
sprocket@context:hook_trigger(),
fun((sprocket@context:context(), fun(() -> nil)) -> {KMK,
sprocket@context:element()})
) -> {KMK, sprocket@context:element()}.
callback(Ctx, Callback_fn, Trigger, Cb) ->
_assert_subject = sprocket@context:fetch_or_init_hook(
Ctx,
fun() ->
{callback,
sprocket@internal@utils@unique:cuid(erlang:element(8, Ctx)),
Callback_fn,
none}
end
),
{Ctx@1, {callback, Id, Current_callback_fn, Prev}, Index} = case _assert_subject of
{_, {callback, _, _, _}, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"sprocket/hooks"/utf8>>,
function => <<"callback"/utf8>>,
line => 32})
end,
{Callback_fn@1, Deps} = maybe_trigger_update(
Trigger,
begin
_pipe = Prev,
gleam@option:then(
_pipe,
fun(Prev@1) -> erlang:element(2, Prev@1) end
)
end,
Current_callback_fn,
fun() -> Callback_fn end
),
Ctx@2 = sprocket@context:update_hook(
Ctx@1,
{callback, Id, Callback_fn@1, {some, {callback_result, Deps}}},
Index
),
Cb(Ctx@2, Callback_fn@1).
-spec client(
sprocket@context:context(),
binary(),
gleam@option:option(fun((binary(), gleam@option:option(gleam@dynamic:dynamic_()), fun((binary(), gleam@option:option(binary())) -> {ok,
nil} |
{error, nil})) -> nil)),
fun((sprocket@context:context(), fun(() -> sprocket@context:attribute()), fun((binary(), gleam@option:option(binary())) -> {ok,
nil} |
{error, nil})) -> {sprocket@context:context(),
sprocket@context:element()})
) -> {sprocket@context:context(), sprocket@context:element()}.
client(Ctx, Name, Handle_event, Cb) ->
Init = fun() ->
{client,
sprocket@internal@utils@unique:cuid(erlang:element(8, Ctx)),
Name,
Handle_event}
end,
_assert_subject = sprocket@context:fetch_or_init_hook(Ctx, Init),
{Ctx@1, {client, Id, _, _}, Index} = case _assert_subject of
{_, {client, _, _, _}, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"sprocket/hooks"/utf8>>,
function => <<"client"/utf8>>,
line => 105})
end,
Ctx@2 = sprocket@context:update_hook(
Ctx@1,
{client, Id, Name, Handle_event},
Index
),
Bind_hook_attr = fun() -> {client_hook, Id, Name} end,
Dispatch_event = fun(Name@1, Payload) ->
sprocket@context:dispatch_event(Ctx@2, Id, Name@1, Payload)
end,
Cb(Ctx@2, Bind_hook_attr, Dispatch_event).
-spec effect(
sprocket@context:context(),
fun(() -> gleam@option:option(fun(() -> nil))),
sprocket@context:hook_trigger(),
fun((sprocket@context:context()) -> {sprocket@context:context(),
sprocket@context:element()})
) -> {sprocket@context:context(), sprocket@context:element()}.
effect(Ctx, Effect_fn, Trigger, Cb) ->
Init = fun() ->
{effect,
sprocket@internal@utils@unique:cuid(erlang:element(8, Ctx)),
Effect_fn,
Trigger,
none}
end,
_assert_subject = sprocket@context:fetch_or_init_hook(Ctx, Init),
{Ctx@1, {effect, Id, _, _, Prev}, Index} = case _assert_subject of
{_, {effect, _, _, _, _}, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"sprocket/hooks"/utf8>>,
function => <<"effect"/utf8>>,
line => 137})
end,
Ctx@2 = sprocket@context:update_hook(
Ctx@1,
{effect, Id, Effect_fn, Trigger, Prev},
Index
),
Cb(Ctx@2).
-spec handler(
sprocket@context:context(),
fun((gleam@option:option(sprocket@context:callback_param())) -> nil),
fun((sprocket@context:context(), sprocket@context:identifiable_handler()) -> {KMP,
sprocket@context:element()})
) -> {KMP, sprocket@context:element()}.
handler(Ctx, Handler_fn, Cb) ->
_assert_subject = sprocket@context:fetch_or_init_hook(
Ctx,
fun() ->
{handler,
sprocket@internal@utils@unique:cuid(erlang:element(8, Ctx)),
Handler_fn}
end
),
{Ctx@1, {handler, Id, _}, Index} = case _assert_subject of
{_, {handler, _, _}, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"sprocket/hooks"/utf8>>,
function => <<"handler"/utf8>>,
line => 157})
end,
Ctx@2 = sprocket@context:update_hook(
Ctx@1,
{handler, Id, Handler_fn},
Index
),
Cb(Ctx@2, {identifiable_handler, Id, Handler_fn}).
-spec memo(
sprocket@context:context(),
fun(() -> KMQ),
sprocket@context:hook_trigger(),
fun((sprocket@context:context(), KMQ) -> {sprocket@context:context(),
sprocket@context:element()})
) -> {sprocket@context:context(), sprocket@context:element()}.
memo(Ctx, Memo_fn, Trigger, Cb) ->
_assert_subject = sprocket@context:fetch_or_init_hook(
Ctx,
fun() ->
{memo,
sprocket@internal@utils@unique:cuid(erlang:element(8, Ctx)),
gleam@dynamic:from(Memo_fn()),
none}
end
),
{Ctx@1, {memo, Id, Current_memoized, Prev}, Index} = case _assert_subject of
{_, {memo, _, _, _}, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"sprocket/hooks"/utf8>>,
function => <<"memo"/utf8>>,
line => 181})
end,
{Memoized, Deps} = maybe_trigger_update(
Trigger,
begin
_pipe = Prev,
gleam@option:then(
_pipe,
fun(Prev@1) -> erlang:element(2, Prev@1) end
)
end,
Current_memoized,
fun() -> gleam@dynamic:from(Memo_fn()) end
),
Ctx@2 = sprocket@context:update_hook(
Ctx@1,
{memo, Id, Memoized, {some, {memo_result, Deps}}},
Index
),
Cb(Ctx@2, gleam@dynamic:unsafe_coerce(Memoized)).
-spec provider(
sprocket@context:context(),
binary(),
fun((sprocket@context:context(), gleam@option:option(any())) -> {sprocket@context:context(),
sprocket@context:element()})
) -> {sprocket@context:context(), sprocket@context:element()}.
provider(Ctx, Key, Cb) ->
Value = begin
_pipe = erlang:element(9, Ctx),
_pipe@1 = gleam@dict:get(_pipe, Key),
_pipe@2 = gleam@option:from_result(_pipe@1),
gleam@option:map(_pipe@2, fun(V) -> gleam@dynamic:unsafe_coerce(V) end)
end,
Cb(Ctx, Value).
-spec reducer(
sprocket@context:context(),
KMT,
fun((KMT, KMU) -> KMT),
fun((sprocket@context:context(), KMT, fun((KMU) -> nil)) -> {sprocket@context:context(),
sprocket@context:element()})
) -> {sprocket@context:context(), sprocket@context:element()}.
reducer(Ctx, Initial, Reducer, Cb) ->
{context, _, _, _, Render_update, _, _, _, _} = Ctx,
Reducer_init = fun() ->
_assert_subject = begin
_pipe@1 = gleam@otp@actor:start(
Initial,
fun(Message, State) -> case Message of
shutdown ->
{stop, normal};
{state_reducer, Reply_with} ->
gleam@erlang@process:send(Reply_with, State),
gleam@otp@actor:continue(State);
{dispatch_reducer, R, M} ->
_pipe = R(State, M),
gleam@otp@actor:continue(_pipe)
end end
),
gleam@result:map_error(
_pipe@1,
fun(Error) ->
sprocket@internal@logger:error(
<<"hooks.reducer: failed to start reducer actor"/utf8>>
),
Error
end
)
end,
{ok, Reducer_actor} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"sprocket/hooks"/utf8>>,
function => <<"reducer"/utf8>>,
line => 253})
end,
{reducer,
sprocket@internal@utils@unique:cuid(erlang:element(8, Ctx)),
gleam@dynamic:from(Reducer_actor),
fun() -> gleam@erlang@process:send(Reducer_actor, shutdown) end}
end,
_assert_subject@1 = sprocket@context:fetch_or_init_hook(Ctx, Reducer_init),
{Ctx@1, {reducer, _, Dyn_reducer_actor, _}, _} = case _assert_subject@1 of
{_, {reducer, _, _, _}, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"sprocket/hooks"/utf8>>,
function => <<"reducer"/utf8>>,
line => 284})
end,
Reducer_actor@1 = gleam@dynamic:unsafe_coerce(Dyn_reducer_actor),
State@1 = gleam@erlang@process:call(
Reducer_actor@1,
fun(_capture) -> {state_reducer, _capture} end,
1000
),
Dispatch = fun(Msg) ->
gleam@otp@actor:send(Reducer_actor@1, {dispatch_reducer, Reducer, Msg}),
Render_update()
end,
Cb(Ctx@1, State@1, Dispatch).
-spec state(
sprocket@context:context(),
KMX,
fun((sprocket@context:context(), KMX, fun((KMX) -> nil)) -> {sprocket@context:context(),
sprocket@context:element()})
) -> {sprocket@context:context(), sprocket@context:element()}.
state(Ctx, Initial, Cb) ->
{context, _, _, _, Render_update, Update_hook, _, _, _} = Ctx,
Init_state = fun() ->
{state,
sprocket@internal@utils@unique:cuid(erlang:element(8, Ctx)),
gleam@dynamic:from(Initial)}
end,
_assert_subject = sprocket@context:fetch_or_init_hook(Ctx, Init_state),
{Ctx@1, {state, Hook_id, Value}, _} = case _assert_subject of
{_, {state, _, _}, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"sprocket/hooks"/utf8>>,
function => <<"state"/utf8>>,
line => 324})
end,
Setter = fun(Value@1) ->
Update_hook(Hook_id, fun(Hook) -> case Hook of
{state, Id, _} when Id =:= Hook_id ->
{state, Id, gleam@dynamic:from(Value@1)};
_ ->
sprocket@internal@exceptions:throw_on_unexpected_hook_result(
Hook
)
end end),
Render_update()
end,
Cb(Ctx@1, gleam@dynamic:unsafe_coerce(Value), Setter).