@@ -154,14 +154,20 @@ defmodule LiveViewEvents.Notify do
154
154
- A PID.
155
155
- A tuple of the form `{Module, "id"}` to send a message to a [`LiveView.Component`](`Phoenix.LiveView.Component`) in the same process.
156
156
- A tuple of the form `{pid, Module, "id"}` to send a message to a [`LiveView.Component`](`Phoenix.LiveView.Component`) in a different process.
157
+
158
+ The event send will take the form of `{message, %{}}`.
157
159
"""
158
160
def notify_to ( nil , _message ) , do: nil
159
- def notify_to ( :self , message ) , do: notify_to ( self ( ) , message )
160
- def notify_to ( pid , message ) when is_pid ( pid ) , do: send ( pid , message )
161
+ def notify_to ( :self , message ) , do: notify_to ( self ( ) , normalize_message ( message ) )
162
+ def notify_to ( pid , message ) when is_pid ( pid ) , do: send ( pid , normalize_message ( message ) )
161
163
162
164
def notify_to ( target , message ) when is_tuple ( target ) do
163
165
{ pid , module , id } = process_tuple ( target )
164
- Phoenix.LiveView . send_update ( pid , module , % { :id => id , @ assign_name_for_event => message } )
166
+
167
+ Phoenix.LiveView . send_update ( pid , module , % {
168
+ :id => id ,
169
+ @ assign_name_for_event => normalize_message ( message )
170
+ } )
165
171
end
166
172
167
173
@ doc """
@@ -182,6 +188,9 @@ defmodule LiveViewEvents.Notify do
182
188
} )
183
189
end
184
190
191
+ defp normalize_message ( { _message_name , % { } = _params } = message ) , do: message
192
+ defp normalize_message ( message_name ) , do: { message_name , % { } }
193
+
185
194
defp process_tuple ( { module , id } ) , do: { self ( ) , module , id }
186
195
defp process_tuple ( { pid , _module , _id } = target ) when is_pid ( pid ) , do: target
187
196
end
0 commit comments