From d63577f6264993dc246684f4ce88273c40095ea3 Mon Sep 17 00:00:00 2001 From: Sergio Arbeo Date: Fri, 8 Mar 2024 16:00:19 +0100 Subject: [PATCH 1/2] Make nil as target be a noop --- lib/live_view_events/notify.ex | 3 +++ test/live_view_events/notify_test.exs | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/live_view_events/notify.ex b/lib/live_view_events/notify.ex index aa4ef81..2626ec9 100644 --- a/lib/live_view_events/notify.ex +++ b/lib/live_view_events/notify.ex @@ -149,11 +149,13 @@ defmodule LiveViewEvents.Notify do @doc """ `notify_to/2` accepts a target and a message name. The target can be any of: + - `nil` would make it be a noop. - `:self` to send to `self()`. - A PID. - A tuple of the form `{Module, "id"}` to send a message to a [`LiveView.Component`](`Phoenix.LiveView.Component`) in the same process. - A tuple of the form `{pid, Module, "id"}` to send a message to a [`LiveView.Component`](`Phoenix.LiveView.Component`) in a different process. """ + def notify_to(nil, _message), do: nil def notify_to(:self, message), do: notify_to(self(), message) def notify_to(pid, message) when is_pid(pid), do: send(pid, message) @@ -167,6 +169,7 @@ defmodule LiveViewEvents.Notify do In this case, the message sent would be a tuple with the `message` as first element and `params` as the second one. """ + def notify_to(nil, _message, _params), do: nil def notify_to(:self, message, params), do: notify_to(self(), message, params) def notify_to(pid, message, params) when is_pid(pid), do: send(pid, {message, params}) diff --git a/test/live_view_events/notify_test.exs b/test/live_view_events/notify_test.exs index 0646a3d..8038334 100644 --- a/test/live_view_events/notify_test.exs +++ b/test/live_view_events/notify_test.exs @@ -1,2 +1,13 @@ defmodule LiveViewEvents.NotifyTest do + use ExUnit.Case + + alias LiveViewEvents.Notify + + test "notify_to/2 with `nil` target does not break" do + Notify.notify_to(nil, "event") + end + + test "notify_to/3 with `nil` target does not break" do + Notify.notify_to(nil, "event", %{some: :params}) + end end From 66496b03a23bd472de15b4a85fa411846cf0c693 Mon Sep 17 00:00:00 2001 From: Sergio Arbeo Date: Fri, 8 Mar 2024 16:02:16 +0100 Subject: [PATCH 2/2] Update elixir versions --- .github/workflows/elixir-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/elixir-ci.yml b/.github/workflows/elixir-ci.yml index 24cdb36..c4cdebc 100644 --- a/.github/workflows/elixir-ci.yml +++ b/.github/workflows/elixir-ci.yml @@ -13,7 +13,7 @@ jobs: strategy: matrix: - elixir: [1.14.5, 1.15.6] + elixir: [1.15.6, 1.16.1] otp: [25.0, 26.0] phoenix-live-view-version: [0.19.0, 0.20.0] phoenix-version: [1.7.0]