Current section
Files
Jump to
Current section
Files
src/tale@watch.erl
-module(tale@watch).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/tale/watch.gleam").
-export([start/1]).
-export_type([watch_config/0]).
-type watch_config() :: {watch_config, list(binary()), fun(() -> nil)}.
-file("src/tale/watch.gleam", 43).
-spec gather(list(binary()), list(binary())) -> list(binary()).
gather(Paths, Acc) ->
case Paths of
[] ->
Acc;
[Path | Rest] ->
Acc@1 = [Path | Acc],
Contents = case simplifile_erl:is_directory(Path) of
{ok, true} ->
_pipe = simplifile:get_files(Path),
_pipe@1 = gleam@result:map(_pipe, fun(Files) -> Files end),
gleam@result:unwrap(_pipe@1, []);
_ ->
[]
end,
gather(Rest, lists:append(Contents, Acc@1))
end.
-file("src/tale/watch.gleam", 60).
-spec signature(binary()) -> binary().
signature(Path) ->
case simplifile_erl:file_info(Path) of
{ok, Info} ->
<<<<Path/binary, ":"/utf8>>/binary,
(erlang:integer_to_binary(erlang:element(10, Info)))/binary>>;
{error, _} ->
<<Path/binary, ":missing"/utf8>>
end.
-file("src/tale/watch.gleam", 37).
-spec capture(list(binary())) -> list(binary()).
capture(Paths) ->
_pipe = gather(Paths, []),
_pipe@1 = gleam@list:map(_pipe, fun signature/1),
gleam@list:sort(_pipe@1, fun(A, B) -> gleam@string:compare(A, B) end).
-file("src/tale/watch.gleam", 24).
-spec loop(list(binary()), list(binary()), fun(() -> nil)) -> any().
loop(Paths, State, Notify) ->
gleam_erlang_ffi:sleep(1000),
Next = capture(Paths),
case State =:= Next of
true ->
nil;
false ->
logging:log(info, <<"Detected changes, rebuilding site…"/utf8>>),
Notify()
end,
loop(Paths, Next, Notify).
-file("src/tale/watch.gleam", 19).
-spec run(list(binary()), fun(() -> nil)) -> any().
run(Paths, Notify) ->
Snapshot = capture(Paths),
loop(Paths, Snapshot, Notify).
-file("src/tale/watch.gleam", 15).
-spec start(watch_config()) -> gleam@erlang@process:pid_().
start(Config) ->
proc_lib:spawn_link(
fun() -> run(erlang:element(2, Config), erlang:element(3, Config)) end
).