Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make nil as target be a noop #3

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/elixir-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions lib/live_view_events/notify.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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})

Expand Down
11 changes: 11 additions & 0 deletions test/live_view_events/notify_test.exs
Original file line number Diff line number Diff line change
@@ -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
Loading