-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]). -export_type([reducer_msg/2]). -type reducer_msg(KYM, KYN) :: shutdown | {get_state, gleam@erlang@process:subject(KYM)} | {reducer_dispatch, fun((KYM, KYN) -> {KYM, list(fun((fun((KYN) -> nil)) -> nil))}), KYN}. -file("/home/runner/work/sprocket/sprocket/src/sprocket/hooks.gleam", 59). -spec maybe_trigger_update( list(gleam@dynamic:dynamic_()), gleam@option:option(list(gleam@dynamic:dynamic_())), KYX, fun(() -> KYX) ) -> {KYX, 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", 29). -spec callback( sprocket@context:context(), fun(() -> nil), list(gleam@dynamic:dynamic_()), fun((sprocket@context:context(), fun(() -> nil)) -> {KYV, sprocket@context:element()}) ) -> {KYV, 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 => 35}) 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", 79). -spec client_hook(sprocket@internal@utils@unique:unique(), binary()) -> sprocket@context:attribute(). client_hook(Id, Name) -> {client_hook, Id, Name}. -file("/home/runner/work/sprocket/sprocket/src/sprocket/hooks.gleam", 88). -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 => 98}) 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). -file("/home/runner/work/sprocket/sprocket/src/sprocket/hooks.gleam", 119). -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 => 131}) 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", 145). -spec handler( sprocket@context:context(), fun((gleam@dynamic:dynamic_()) -> nil), fun((sprocket@context:context(), sprocket@context:identifiable_handler()) -> {KZA, sprocket@context:element()}) ) -> {KZA, 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 => 150}) 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", 168). -spec memo( sprocket@context:context(), fun(() -> KZB), list(gleam@dynamic:dynamic_()), fun((sprocket@context:context(), KZB) -> {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 => 174}) 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", 205). -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) -> sprocket@internal@utils@unsafe_coerce:unsafe_coerce(V) end ) end, Cb(Ctx, Value). -file("/home/runner/work/sprocket/sprocket/src/sprocket/hooks.gleam", 240). -spec reducer( sprocket@context:context(), {KZE, list(fun((fun((KZF) -> nil)) -> nil))}, fun((KZE, KZF) -> {KZE, list(fun((fun((KZF) -> nil)) -> nil))}), fun((sprocket@context:context(), KZE, fun((KZF) -> nil)) -> {sprocket@context:context(), sprocket@context:element()}) ) -> {sprocket@context:context(), sprocket@context:element()}. reducer(Ctx, Initial, Reducer, Cb) -> {context, _, _, _, Render_update, _, _, _, _} = Ctx, Dispatch = fun(Msg, A) -> gleam@otp@actor:send(A, {reducer_dispatch, Reducer, Msg}), Render_update() end, Reducer_init = fun() -> Reducer_actor_init = fun() -> Self = gleam@erlang@process:new_subject(), Selector = gleam@erlang@process:selecting( gleam_erlang_ffi:new_selector(), Self, fun gleam@function:identity/1 ), Initial_model = erlang:element(1, Initial), Initial_cmds = erlang:element(2, Initial), gleam@list:each( Initial_cmds, fun(Cmd) -> Cmd(fun(_capture) -> Dispatch(_capture, Self) end) end ), {ready, {Self, Initial_model}, Selector} end, Reducer_actor_handle_message = fun(Message, State) -> case Message of shutdown -> {stop, normal}; {get_state, Reply_with} -> {_, Model} = State, gleam@erlang@process:send(Reply_with, Model), gleam@otp@actor:continue(State); {reducer_dispatch, R, M} -> {Self@1, Model@1} = State, {Updated_model, Cmds} = R(Model@1, M), gleam@list:each( Cmds, fun(Cmd@1) -> Cmd@1( fun(_capture@1) -> Dispatch(_capture@1, Self@1) end ) end ), gleam@otp@actor:continue({Self@1, Updated_model}) end end, _assert_subject = begin _pipe = gleam@otp@actor:start_spec( {spec, Reducer_actor_init, 1000, Reducer_actor_handle_message} ), 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 => 315}) end, {reducer, sprocket@internal@utils@unique:cuid(erlang:element(8, Ctx)), gleam_stdlib:identity(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 => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail@1, module => <<"sprocket/hooks"/utf8>>, function => <<"reducer"/utf8>>, line => 333}) end, Reducer_actor@1 = sprocket@internal@utils@unsafe_coerce:unsafe_coerce( Dyn_reducer_actor ), State@1 = gleam@erlang@process:call( Reducer_actor@1, fun(_capture@2) -> {get_state, _capture@2} end, 1000 ), Cb( Ctx@1, State@1, fun(_capture@3) -> Dispatch(_capture@3, Reducer_actor@1) end ). -file("/home/runner/work/sprocket/sprocket/src/sprocket/hooks.gleam", 351). -spec state( sprocket@context:context(), KZK, fun((sprocket@context:context(), KZK, fun((KZK) -> 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 => 362}) 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 ).