-module(illustrious@internal@state_priv). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([wrap_decoder/1, wrap_updater/2]). -spec wrap_decoder( fun((gleam@dynamic:dynamic_()) -> {ok, illustrious@state:illustrious_action(OMG)} | {error, list(gleam@dynamic:decode_error())}) ) -> fun((binary()) -> illustrious@state:illustrious_action(OMG)). wrap_decoder(Decoder) -> fun(Json) -> case gleam@json:decode(Json, gleam@dynamic:list(Decoder)) of {ok, Actions} -> {all, Actions}; _ -> _pipe = gleam@json:decode(Json, Decoder), gleam@result:unwrap(_pipe, noop) end end. -spec wrap_updater( fun((OMJ, OMK) -> {OMJ, lustre@effect:effect(illustrious@state:illustrious_action(OMK))}), fun((gleam@dynamic:dynamic_()) -> {ok, illustrious@state:illustrious_action(OMK)} | {error, list(gleam@dynamic:decode_error())}) ) -> fun((illustrious@state:illustrious_model(OMJ), illustrious@state:illustrious_action(OMK)) -> {illustrious@state:illustrious_model(OMJ), lustre@effect:effect(illustrious@state:illustrious_action(OMK))}). wrap_updater(Update, Decoder) -> Wrapped_decoder = wrap_decoder(Decoder), Loader = fun(Split_path) -> Path = gleam@list:fold( Split_path, <<""/utf8>>, fun(Full_path, Part) -> <<<>/binary, Part/binary>> end ), lustre_http:post( illustrious@internal@api_helpers:get_illustrious_api(), gleam@json:object([{<<"path"/utf8>>, gleam@json:string(Path)}]), lustre_http:expect_text(fun(Result) -> case Result of {ok, Json} -> Wrapped_decoder(Json); _ -> noop end end) ) end, fun(Illustrious_model, Action) -> case Action of {perform, Sub_action} -> Orig_update = Update( erlang:element(3, Illustrious_model), Sub_action ), {erlang:setelement( 3, Illustrious_model, erlang:element(1, Orig_update) ), erlang:element(2, Orig_update)}; {go_to, Path@1} -> Split_path@1 = illustrious@internal@router:parse_path(Path@1), case erlang:element(2, Illustrious_model) =:= Split_path@1 of true -> {Illustrious_model, lustre@effect:none()}; false -> illustrious@internal@router:push_path(Path@1), {erlang:setelement(2, Illustrious_model, Split_path@1), Loader(Split_path@1)} end; {redirect, Path@2} -> Split_path@2 = illustrious@internal@router:parse_path(Path@2), case erlang:element(2, Illustrious_model) =:= Split_path@2 of true -> {Illustrious_model, lustre@effect:none()}; false -> illustrious@internal@router:replace_path(Path@2), {erlang:setelement(2, Illustrious_model, Split_path@2), Loader(Split_path@2)} end; back -> illustrious@internal@router:back(), {Illustrious_model, lustre@effect:none()}; forward -> illustrious@internal@router:forward(), {Illustrious_model, lustre@effect:none()}; {set_path, Split_path@3} -> case erlang:element(2, Illustrious_model) =:= Split_path@3 of true -> {Illustrious_model, lustre@effect:none()}; false -> {erlang:setelement(2, Illustrious_model, Split_path@3), Loader(Split_path@3)} end; noop -> {Illustrious_model, lustre@effect:none()}; {all, Actions} -> {Illustrious_model, lustre@effect:from( fun(Dispatch) -> gleam@list:each(Actions, Dispatch) end )} end end.