Packages

Bringing debounce and throttle utilities to Lustre for the erlang target.

Current section

Files

Jump to
lustre_limiter src lustre_limiter@internals.erl
Raw

src/lustre_limiter@internals.erl

-module(lustre_limiter@internals).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([emit_after/2, emit/1]).
-export_type([state/0, mode/1, msg/1, limiter/1]).
-type state() :: open | close.
-type mode(OLM) :: {debounce, integer(), list(OLM)} | {throttle, integer()}.
-type msg(OLN) :: {emit, OLN} |
{emit_if_settled, integer()} |
none |
reopen |
{push, OLN}.
-type limiter(OLO) :: {limiter, fun((msg(OLO)) -> OLO), mode(OLO), state()}.
-spec emit_after(OLP, integer()) -> lustre@effect:effect(OLP).
emit_after(Message, Delay) ->
lustre@effect:from(
fun(Dispatch) ->
_ = (gleam@erlang@process:start(
fun() ->
gleam_erlang_ffi:sleep(Delay),
Dispatch(Message),
gleam@erlang@process:send_exit(erlang:self())
end,
true
)),
nil
end
).
-spec emit(OLR) -> lustre@effect:effect(OLR).
emit(Message) ->
lustre@effect:from(fun(Dispatch) -> Dispatch(Message) end).