defmodule Whisper.Counter do use Agent def start_link(_), do: Agent.start_link(fn -> %{} end, name: __MODULE__) def next(ref) do Agent.get_and_update(__MODULE__, fn state -> current = Map.get(state, ref, 0) + 1 {current, Map.put(state, ref, current)} end) end def reset(ref) do Agent.update(__MODULE__, &Map.delete(&1, ref)) end end