-module(radiate). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([new/0, add_dir/2, on_reload/2, set_initializer/2, start_state/1, start/1]). -export_type([no_directories/0, has_directories/0, no_initializer/0, has_initializer/0, no_callback/0, has_callback/0, builder/4, module_/0, what/0]). -type no_directories() :: any(). -type has_directories() :: any(). -type no_initializer() :: any(). -type has_initializer() :: any(). -type no_callback() :: any(). -type has_callback() :: any(). -opaque builder(HTF, HTG, HTH, HTI) :: {builder, list(binary()), fun((HTF, binary()) -> HTF), gleam@option:option(fun(() -> HTF))} | {gleam_phantom, HTG, HTH, HTI}. -type module_() :: any(). -type what() :: any(). -spec new() -> builder(nil, no_directories(), no_initializer(), no_callback()). new() -> _pipe = fun(State, Path) -> gleam@io:println( <<<<"Change in "/utf8, Path/binary>>/binary, ", reloading."/utf8>> ), State end, {builder, [], _pipe, none}. -spec add_dir(builder(HTN, any(), HTP, HTQ), binary()) -> builder(HTN, has_directories(), HTP, HTQ). add_dir(Builder, Dir) -> {builder, [Dir | erlang:element(2, Builder)], erlang:element(3, Builder), erlang:element(4, Builder)}. -spec on_reload( builder(HTZ, HUA, HUB, no_callback()), fun((HTZ, binary()) -> HTZ) ) -> builder(HTZ, HUA, HUB, has_callback()). on_reload(Builder, Callback) -> {builder, erlang:element(2, Builder), Callback, erlang:element(4, Builder)}. -spec set_initializer(builder(HUK, HUL, no_initializer(), HUM), fun(() -> HUK)) -> builder(HUK, HUL, has_initializer(), HUM). set_initializer(Builder, Initializer) -> {builder, erlang:element(2, Builder), erlang:element(3, Builder), {some, Initializer}}. -spec start_state(builder(any(), has_directories(), has_initializer(), any())) -> {ok, gleam@erlang@process:subject(filespy:change(nil))} | {error, gleam@otp@actor:start_error()}. start_state(Builder) -> _pipe = filespy:new(), _pipe@1 = filespy:set_initializer( _pipe, fun() -> _assert_subject = erlang:element(4, Builder), {some, Init} = case _assert_subject of {some, _} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail, module => <<"radiate"/utf8>>, function => <<"start_state"/utf8>>, line => 112}) end, Init() end ), _pipe@2 = filespy:add_dirs(_pipe@1, erlang:element(2, Builder)), _pipe@5 = filespy:set_actor_handler(_pipe@2, fun(Msg, State) -> case Msg of {custom, _} -> gleam@otp@actor:continue(State); {change, Path, Events} -> _pipe@4 = gleam@list:fold( Events, State, fun(State@1, Event) -> case Event of closed -> case gleam@string:ends_with( Path, <<".gleam"/utf8>> ) of true -> Build_result = shellout:command( <<"gleam"/utf8>>, [<<"build"/utf8>>], <<"."/utf8>>, [] ), case Build_result of {ok, _} -> Mods = code:modified_modules( ), gleam@list:each( Mods, fun code:purge/1 ), _ = code:atomic_load(Mods), (erlang:element(3, Builder))( State@1, Path ); {error, {_, Message}} -> _pipe@3 = Message, gleam@io:print_error( _pipe@3 ), State@1 end; _ -> State@1 end; modified -> case gleam@string:ends_with( Path, <<".gleam"/utf8>> ) of true -> Build_result = shellout:command( <<"gleam"/utf8>>, [<<"build"/utf8>>], <<"."/utf8>>, [] ), case Build_result of {ok, _} -> Mods = code:modified_modules( ), gleam@list:each( Mods, fun code:purge/1 ), _ = code:atomic_load(Mods), (erlang:element(3, Builder))( State@1, Path ); {error, {_, Message}} -> _pipe@3 = Message, gleam@io:print_error( _pipe@3 ), State@1 end; _ -> State@1 end; _ -> State@1 end end ), gleam@otp@actor:continue(_pipe@4) end end), filespy:start(_pipe@5). -spec start(builder(nil, has_directories(), no_initializer(), any())) -> {ok, gleam@erlang@process:subject(filespy:change(nil))} | {error, gleam@otp@actor:start_error()}. start(Builder) -> _pipe = Builder, _pipe@1 = set_initializer(_pipe, fun() -> nil end), start_state(_pipe@1).