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_hook/2, client/4, effect/4, handler/3, memo/4, provider/3, reducer/4, state/3]).
-file("/home/runner/work/sprocket/sprocket/src/sprocket/hooks.gleam", 58).
-spec maybe_trigger_update(
list(gleam@dynamic:dynamic_()),
gleam@option:option(list(gleam@dynamic:dynamic_())),
LLF,
fun(() -> LLF)
) -> {LLF, gleam@option:option(list(gleam@dynamic:dynamic_()))}.
maybe_trigger_update(Deps, Prev, Value, Updater) ->
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.
-file("/home/runner/work/sprocket/sprocket/src/sprocket/hooks.gleam", 28).
-spec callback(
sprocket@context:context(),
fun(() -> nil),
list(gleam@dynamic:dynamic_()),
fun((sprocket@context:context(), fun(() -> nil)) -> {LLD,
sprocket@context:element()})
) -> {LLD, sprocket@context:element()}.
callback(Ctx, Callback_fn, Deps, 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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"sprocket/hooks"/utf8>>,
function => <<"callback"/utf8>>,
line => 34})
end,
{Callback_fn@1, Deps@1} = maybe_trigger_update(
Deps,
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@1}}},
Index
),
Cb(Ctx@2, Callback_fn@1).
-file("/home/runner/work/sprocket/sprocket/src/sprocket/hooks.gleam", 78).
-spec client_hook(
sprocket@internal@utils@unique:unique(sprocket@context:hook_id()),
binary()
) -> sprocket@context:attribute().
client_hook(Id, Name) ->
{client_hook, Id, Name}.
-file("/home/runner/work/sprocket/sprocket/src/sprocket/hooks.gleam", 87).
-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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"sprocket/hooks"/utf8>>,
function => <<"client"/utf8>>,
line => 97})
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:emit_event(Ctx@2, Id, Name@1, Payload)
end,
Cb(Ctx@2, Bind_hook_attr, Dispatch_event).
-file("/home/runner/work/sprocket/sprocket/src/sprocket/hooks.gleam", 118).
-spec effect(
sprocket@context:context(),
fun(() -> gleam@option:option(fun(() -> nil))),
list(gleam@dynamic:dynamic_()),
fun((sprocket@context:context()) -> {sprocket@context:context(),
sprocket@context:element()})
) -> {sprocket@context:context(), sprocket@context:element()}.
effect(Ctx, Effect_fn, Deps, Cb) ->
Init = fun() ->
{effect,
sprocket@internal@utils@unique:cuid(erlang:element(8, Ctx)),
Effect_fn,
Deps,
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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"sprocket/hooks"/utf8>>,
function => <<"effect"/utf8>>,
line => 130})
end,
Ctx@2 = sprocket@context:update_hook(
Ctx@1,
{effect, Id, Effect_fn, Deps, Prev},
Index
),
Cb(Ctx@2).
-file("/home/runner/work/sprocket/sprocket/src/sprocket/hooks.gleam", 144).
-spec handler(
sprocket@context:context(),
fun((gleam@dynamic:dynamic_()) -> nil),
fun((sprocket@context:context(), sprocket@context:identifiable_handler()) -> {LLJ,
sprocket@context:element()})
) -> {LLJ, 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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"sprocket/hooks"/utf8>>,
function => <<"handler"/utf8>>,
line => 149})
end,
Ctx@2 = sprocket@context:update_hook(
Ctx@1,
{handler, Id, Handler_fn},
Index
),
Cb(Ctx@2, {identifiable_handler, Id, Handler_fn}).
-file("/home/runner/work/sprocket/sprocket/src/sprocket/hooks.gleam", 167).
-spec memo(
sprocket@context:context(),
fun(() -> LLK),
list(gleam@dynamic:dynamic_()),
fun((sprocket@context:context(), LLK) -> {sprocket@context:context(),
sprocket@context:element()})
) -> {sprocket@context:context(), sprocket@context:element()}.
memo(Ctx, Memo_fn, Deps, Cb) ->
_assert_subject = sprocket@context:fetch_or_init_hook(
Ctx,
fun() ->
{memo,
sprocket@internal@utils@unique:cuid(erlang:element(8, Ctx)),
gleam_stdlib:identity(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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"sprocket/hooks"/utf8>>,
function => <<"memo"/utf8>>,
line => 173})
end,
{Memoized, Deps@1} = maybe_trigger_update(
Deps,
begin
_pipe = Prev,
gleam@option:then(
_pipe,
fun(Prev@1) -> erlang:element(2, Prev@1) end
)
end,
Current_memoized,
fun() -> gleam_stdlib:identity(Memo_fn()) end
),
Ctx@2 = sprocket@context:update_hook(
Ctx@1,
{memo, Id, Memoized, {some, {memo_result, Deps@1}}},
Index
),
Cb(Ctx@2, sprocket@internal@utils@unsafe_coerce:unsafe_coerce(Memoized)).
-file("/home/runner/work/sprocket/sprocket/src/sprocket/hooks.gleam", 204).
-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_stdlib:map_get(_pipe, Key),
_pipe@2 = gleam@option:from_result(_pipe@1),
gleam@option:map(
_pipe@2,
fun(V) -> sprocket@internal@utils@unsafe_coerce:unsafe_coerce(V) end
)
end,
Cb(Ctx, Value).
-file("/home/runner/work/sprocket/sprocket/src/sprocket/hooks.gleam", 224).
-spec reducer(
sprocket@context:context(),
{LLN, list(fun((fun((LLO) -> nil)) -> nil))},
fun((LLN, LLO) -> {LLN, list(fun((fun((LLO) -> nil)) -> nil))}),
fun((sprocket@context:context(), LLN, fun((LLO) -> nil)) -> {sprocket@context:context(),
sprocket@context:element()})
) -> {sprocket@context:context(), sprocket@context:element()}.
reducer(Ctx, Initial, Update, Cb) ->
{context, _, _, _, Render_update, _, _, _, _} = Ctx,
Reducer_init = fun() ->
_assert_subject = begin
_pipe = sprocket@internal@reducer:start(
Initial,
Update,
fun(_) -> Render_update() end
),
gleam@result:map_error(
_pipe,
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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"sprocket/hooks"/utf8>>,
function => <<"reducer"/utf8>>,
line => 237})
end,
{reducer,
sprocket@internal@utils@unique:cuid(erlang:element(8, Ctx)),
gleam_stdlib:identity(Reducer_actor),
fun() -> sprocket@internal@reducer:shutdown(Reducer_actor) 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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"sprocket/hooks"/utf8>>,
function => <<"reducer"/utf8>>,
line => 251})
end,
Reducer_actor@1 = sprocket@internal@utils@unsafe_coerce:unsafe_coerce(
Dyn_reducer_actor
),
State = sprocket@internal@reducer:get_state(Reducer_actor@1),
Cb(
Ctx@1,
State,
fun(_capture) ->
sprocket@internal@reducer:dispatch(Reducer_actor@1, _capture)
end
).
-file("/home/runner/work/sprocket/sprocket/src/sprocket/hooks.gleam", 269).
-spec state(
sprocket@context:context(),
LLT,
fun((sprocket@context:context(), LLT, fun((LLT) -> 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_stdlib:identity(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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"sprocket/hooks"/utf8>>,
function => <<"state"/utf8>>,
line => 280})
end,
Setter = fun(Value@1) ->
Update_hook(Hook_id, fun(Hook) -> case Hook of
{state, Id, _} when Id =:= Hook_id ->
{state, Id, gleam_stdlib:identity(Value@1)};
_ ->
sprocket@internal@exceptions:throw_on_unexpected_hook_result(
Hook
)
end end),
Render_update()
end,
Cb(
Ctx@1,
sprocket@internal@utils@unsafe_coerce:unsafe_coerce(Value),
Setter
).