From 5e42ff9c3d5a186413e86ed5d4a439293b81091a Mon Sep 17 00:00:00 2001 From: Burmaja Milan Date: Fri, 20 Oct 2023 10:28:58 +0100 Subject: [PATCH] Allow Listener.process_push/2 to return 'stop' --- CHANGELOG.md | 5 +++++ lib/listener.ex | 22 +++++++++++++++++----- mix.exs | 2 +- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d4e53a..2424bfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# Changelog for extreme v1.0.4 + +- Listener.process_push callback can return `:stop`, meaning subscription should be stopped + and pushes that are already in mailbox should be purged. + # Changelog for extreme v1.0.3 - Add subscribe/unsubscribe and auto_subscribe? option for starting `Extreme.Listener` diff --git a/lib/listener.ex b/lib/listener.ex index edc00f6..20f9d70 100644 --- a/lib/listener.ex +++ b/lib/listener.ex @@ -72,17 +72,29 @@ defmodule Extreme.Listener do end @impl true + def handle_call({:on_event, _push}, _from, %{subscription: nil, mode: :live} = state), + do: {:reply, :ok, state} + def handle_call( {:on_event, push}, _from, %{subscription: subscription, mode: :live} = state - ) - when not is_nil(subscription) do - {:ok, event_number} = process_push(push, state.stream_name) - {:reply, :ok, %{state | last_event: event_number}} + ) do + push + |> process_push(state.stream_name) + |> case do + {:ok, event_number} -> + {:reply, :ok, %{state | last_event: event_number}} + + :stop -> + _unsubscribe(state) + end end - def handle_call(:unsubscribe, from, state) do + def handle_call(:unsubscribe, _from, state), + do: _unsubscribe(state) + + defp _unsubscribe(state) do Logger.info( "#{__MODULE__} unsubscribed from #{state.stream_name}. Last processed event: #{state.last_event}" ) diff --git a/mix.exs b/mix.exs index 6ce66a3..b1b9853 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Extreme.Mixfile do def project do [ app: :extreme, - version: "1.0.3", + version: "1.0.4", elixir: "~> 1.11", elixirc_paths: _elixirc_paths(Mix.env()), source_url: "https://github.com/exponentially/extreme",