Current section

Files

Jump to
sprocket src sprocket@hooks.erl
Raw

src/sprocket@hooks.erl

-module(sprocket@hooks).
-compile([no_auto_import, nowarn_unused_vars]).
-export([dep/1, compare_deps/2]).
-export_type([hook_trigger/0, effect_result/0, callback_result/0, hook/0, compared/1]).
-type hook_trigger() :: on_mount |
on_update |
{with_deps, list(gleam@dynamic:dynamic())}.
-type effect_result() :: {effect_result,
gleam@option:option(fun(() -> nil)),
gleam@option:option(list(gleam@dynamic:dynamic()))}.
-type callback_result() :: {callback_result,
sprocket@internal@identifiable_callback:callback_fn(),
gleam@option:option(list(gleam@dynamic:dynamic()))}.
-type hook() :: {callback,
sprocket@internal@utils@unique:unique(),
sprocket@internal@identifiable_callback:callback_fn(),
hook_trigger(),
gleam@option:option(callback_result())} |
{effect,
sprocket@internal@utils@unique:unique(),
fun(() -> gleam@option:option(fun(() -> nil))),
hook_trigger(),
gleam@option:option(effect_result())} |
{reducer,
sprocket@internal@utils@unique:unique(),
gleam@dynamic:dynamic(),
fun(() -> nil)}.
-type compared(JJB) :: {changed, JJB} | unchanged.
-spec dep(any()) -> gleam@dynamic:dynamic().
dep(Dependency) ->
gleam@dynamic:from(Dependency).
-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, length_mismatch} ->
sprocket@internal@exception: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.