-module(sprocket@context). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([has_id/2, dep/1, compare_deps/2, new/5, prepare_for_reconciliation/1, fetch_or_init_hook/2, update_hook/3, push_event_handler/2, get_event_handler/2, emit_event/4, provider/3]). -export_type([identifiable_handler/0, attribute/0, element/0, updater/1, effect_result/0, callback_result/0, memo_result/0, hook_id/0, hook/0, compared/1, component_wip/0, context/0]). -type identifiable_handler() :: {identifiable_handler, sprocket@internal@utils@unique:unique(hook_id()), fun((gleam@dynamic:dynamic_()) -> nil)}. -type attribute() :: {attribute, binary(), gleam@dynamic:dynamic_()} | {event, binary(), identifiable_handler()} | {client_hook, sprocket@internal@utils@unique:unique(hook_id()), binary()}. -type element() :: {element, binary(), list(attribute()), list(element())} | {component, fun((context(), gleam@dynamic:dynamic_()) -> {context(), element()}), gleam@dynamic:dynamic_()} | {fragment, list(element())} | {debug, binary(), gleam@option:option(gleam@dynamic:dynamic_()), element()} | {keyed, binary(), element()} | {ignore_update, element()} | {provider, binary(), gleam@dynamic:dynamic_(), element()} | {text, binary()} | {custom, binary(), binary()}. -type updater(JIY) :: {updater, fun((JIY) -> {ok, nil} | {error, nil})}. -type effect_result() :: {effect_result, gleam@option:option(fun(() -> nil)), gleam@option:option(list(gleam@dynamic:dynamic_()))}. -type callback_result() :: {callback_result, gleam@option:option(list(gleam@dynamic:dynamic_()))}. -type memo_result() :: {memo_result, gleam@option:option(list(gleam@dynamic:dynamic_()))}. -type hook_id() :: any(). -type hook() :: {callback, sprocket@internal@utils@unique:unique(hook_id()), fun(() -> nil), gleam@option:option(callback_result())} | {memo, sprocket@internal@utils@unique:unique(hook_id()), gleam@dynamic:dynamic_(), gleam@option:option(memo_result())} | {effect, sprocket@internal@utils@unique:unique(hook_id()), fun(() -> gleam@option:option(fun(() -> nil))), list(gleam@dynamic:dynamic_()), gleam@option:option(effect_result())} | {handler, sprocket@internal@utils@unique:unique(hook_id()), fun((gleam@dynamic:dynamic_()) -> nil)} | {reducer, sprocket@internal@utils@unique:unique(hook_id()), gleam@dynamic:dynamic_(), fun(() -> nil)} | {state, sprocket@internal@utils@unique:unique(hook_id()), gleam@dynamic:dynamic_()} | {client, sprocket@internal@utils@unique:unique(hook_id()), binary(), gleam@option:option(fun((binary(), gleam@option:option(gleam@dynamic:dynamic_()), fun((binary(), gleam@option:option(binary())) -> {ok, nil} | {error, nil})) -> nil))}. -type compared(JIZ) :: {changed, JIZ} | unchanged. -type component_wip() :: {component_wip, sprocket@internal@utils@ordered_map:ordered_map(integer(), hook()), integer(), boolean()}. -type context() :: {context, element(), component_wip(), list(identifiable_handler()), fun(() -> nil), fun((sprocket@internal@utils@unique:unique(hook_id()), fun((hook()) -> hook())) -> nil), fun((sprocket@internal@utils@unique:unique(hook_id()), binary(), gleam@option:option(binary())) -> {ok, nil} | {error, nil}), gleam@erlang@process:subject(ids@cuid:message()), gleam@dict:dict(binary(), gleam@dynamic:dynamic_())}. -file("/home/runner/work/sprocket/sprocket/src/sprocket/context.gleam", 104). -spec has_id(hook(), sprocket@internal@utils@unique:unique(hook_id())) -> boolean(). has_id(Hook, Hook_id) -> case Hook of {callback, Id, _, _} when Id =:= Hook_id -> true; {memo, Id@1, _, _} when Id@1 =:= Hook_id -> true; {effect, Id@2, _, _, _} when Id@2 =:= Hook_id -> true; {handler, Id@3, _} when Id@3 =:= Hook_id -> true; {state, Id@4, _} when Id@4 =:= Hook_id -> true; {client, Id@5, _, _} when Id@5 =:= Hook_id -> true; _ -> false end. -file("/home/runner/work/sprocket/sprocket/src/sprocket/context.gleam", 122). -spec dep(any()) -> gleam@dynamic:dynamic_(). dep(Dependency) -> gleam_stdlib:identity(Dependency). -file("/home/runner/work/sprocket/sprocket/src/sprocket/context.gleam", 126). -spec compare_deps( list(gleam@dynamic:dynamic_()), list(gleam@dynamic:dynamic_()) ) -> compared(list(gleam@dynamic:dynamic_())). compare_deps(Prev_deps, Deps) -> case gleam@list:strict_zip(Prev_deps, Deps) of {error, _} -> sprocket@internal@exceptions:throw_on_unexpected_deps_mismatch( {<<"compare_deps"/utf8>>, Prev_deps, Deps} ); {ok, Zipped_deps} -> case gleam@list:all( Zipped_deps, fun(Z) -> {A, B} = Z, A =:= B end ) of true -> unchanged; _ -> {changed, Deps} end end. -file("/home/runner/work/sprocket/sprocket/src/sprocket/context.gleam", 168). -spec new( element(), gleam@erlang@process:subject(ids@cuid:message()), gleam@option:option(fun((binary(), binary(), gleam@option:option(binary())) -> {ok, nil} | {error, nil})), fun(() -> nil), fun((sprocket@internal@utils@unique:unique(hook_id()), fun((hook()) -> hook())) -> nil) ) -> context(). new(View, Cuid_channel, Emitter, Render_update, Update_hook) -> {context, View, {component_wip, sprocket@internal@utils@ordered_map:new(), 0, true}, [], Render_update, Update_hook, fun(Id, Name, Payload) -> case Emitter of {some, Emitter@1} -> Emitter@1( sprocket@internal@utils@unique:to_string(Id), Name, Payload ); none -> {error, nil} end end, Cuid_channel, maps:new()}. -file("/home/runner/work/sprocket/sprocket/src/sprocket/context.gleam", 192). -spec prepare_for_reconciliation(context()) -> context(). prepare_for_reconciliation(Ctx) -> erlang:setelement(4, Ctx, []). -file("/home/runner/work/sprocket/sprocket/src/sprocket/context.gleam", 196). -spec fetch_or_init_hook(context(), fun(() -> hook())) -> {context(), hook(), integer()}. fetch_or_init_hook(Ctx, Init) -> Index = erlang:element(3, erlang:element(3, Ctx)), Hooks = erlang:element(2, erlang:element(3, Ctx)), case sprocket@internal@utils@ordered_map:get(Hooks, Index) of {ok, Hook} -> {erlang:setelement( 3, Ctx, erlang:setelement(3, erlang:element(3, Ctx), Index + 1) ), Hook, Index}; {error, nil} -> case erlang:element(4, erlang:element(3, Ctx)) of true -> nil; false -> sprocket@internal@logger:error( <<<<" Hook not found for index: "/utf8, (erlang:integer_to_binary(Index))/binary>>/binary, ". This indicates a hook was dynamically created since first render which is not allowed. "/utf8>> ), erlang:error(#{gleam_error => panic, message => <<"`panic` expression evaluated."/utf8>>, module => <<"sprocket/context"/utf8>>, function => <<"fetch_or_init_hook"/utf8>>, line => 223}) end, Hook@1 = Init(), {erlang:setelement( 3, Ctx, {component_wip, sprocket@internal@utils@ordered_map:insert( erlang:element(2, erlang:element(3, Ctx)), Index, Hook@1 ), Index + 1, erlang:element(4, erlang:element(3, Ctx))} ), Hook@1, Index} end. -file("/home/runner/work/sprocket/sprocket/src/sprocket/context.gleam", 246). -spec update_hook(context(), hook(), integer()) -> context(). update_hook(Ctx, Hook, Index) -> erlang:setelement( 3, Ctx, erlang:setelement( 2, erlang:element(3, Ctx), sprocket@internal@utils@ordered_map:update( erlang:element(2, erlang:element(3, Ctx)), Index, Hook ) ) ). -file("/home/runner/work/sprocket/sprocket/src/sprocket/context.gleam", 256). -spec push_event_handler(context(), identifiable_handler()) -> {context(), sprocket@internal@utils@unique:unique(hook_id())}. push_event_handler(Ctx, Identifiable_cb) -> {identifiable_handler, Id, Cb} = Identifiable_cb, {erlang:setelement( 4, Ctx, [{identifiable_handler, Id, Cb} | erlang:element(4, Ctx)] ), Id}. -file("/home/runner/work/sprocket/sprocket/src/sprocket/context.gleam", 265). -spec get_event_handler( context(), sprocket@internal@utils@unique:unique(hook_id()) ) -> {context(), {ok, identifiable_handler()} | {error, nil}}. get_event_handler(Ctx, Id) -> Handler = gleam@list:find( erlang:element(4, Ctx), fun(H) -> {identifiable_handler, I, _} = H, I =:= Id end ), {Ctx, Handler}. -file("/home/runner/work/sprocket/sprocket/src/sprocket/context.gleam", 278). -spec emit_event( context(), sprocket@internal@utils@unique:unique(hook_id()), binary(), gleam@option:option(binary()) ) -> {ok, nil} | {error, nil}. emit_event(Ctx, Id, Name, Payload) -> (erlang:element(7, Ctx))(Id, Name, Payload). -file("/home/runner/work/sprocket/sprocket/src/sprocket/context.gleam", 287). -spec provider(binary(), any(), element()) -> element(). provider(Key, Value, Element) -> {provider, Key, gleam_stdlib:identity(Value), Element}.