From 2269b2eb901ed2f1eb0e71d0b5613bfedcd6b40d Mon Sep 17 00:00:00 2001 From: tyson Date: Thu, 3 Feb 2022 15:37:37 +0800 Subject: [PATCH] logic fix in debounce --- lib/slack_logger_backend/producer.ex | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/slack_logger_backend/producer.ex b/lib/slack_logger_backend/producer.ex index e587974..046a905 100644 --- a/lib/slack_logger_backend/producer.ex +++ b/lib/slack_logger_backend/producer.ex @@ -35,6 +35,10 @@ defmodule SlackLoggerBackend.Producer do %{state | event_map: event_map} else # handle new event + unless is_nil(state.debounce) do + Process.send_after(self(), :process_demand, state.debounce * 1000 + 1) + end + queue = :queue.in({time, event}, state.queue) event_map = Map.put(state.event_map, event, 1) %{state | event_map: event_map, queue: queue} @@ -62,7 +66,7 @@ defmodule SlackLoggerBackend.Producer do {:noreply, events, state} {{:value, {time, event}}, queue} -> - if is_nil(state.debounce) or state.debounce > System.monotonic_time(:second) - time do + if is_nil(state.debounce) or state.debounce <= System.monotonic_time(:second) - time do count = Map.get(state.event_map, event) event_map = Map.delete(state.event_map, event) state = %{state | demand: demand - 1, queue: queue, event_map: event_map}