View Source Love.Events behaviour (love_ex v0.2.0)

Send and receive event messages between views and components.

The use Love.View and use Love.Component macros automatically use this behaviour.

Link to this section Summary

Link to this section Types

@type destination() :: pid() | {module(), String.t() | atom()}

Link to this section Callbacks

Link to this callback

handle_message( name, source, payload, socket )

View Source (optional)
@callback handle_message(
  name :: atom(),
  source :: any(),
  payload :: any(),
  socket :: LiveView.Socket.t()
) :: socket :: LiveView.Socket.t()

Handle an event message sent by Love.Component.emit/3 or send_message/4.

When sent from a component via Love.Component.emit/3, the source takes the form {module, id}.

example

Example

# Emitted from a component
def handle_message(:on_selected, {MyComponent, _id}, %{profile_id: profile_id}, socket), do: ...

Link to this section Functions

Link to this function

send_message(destination, name, payload, opts \\ [])

View Source
@spec send_message(
  destination :: destination(),
  name :: atom(),
  payload :: any(),
  opts :: keyword()
) ::
  :ok

Sends an event message to a Love.View or a Love.Component.

To send to a Love.View (or any other process), specify a pid (usually self()) as the destination. To send to a Love.Component, specify {module, id} as the destination.

It can be handled by the Love.Events.handle_message/4 callback.

When sending to an arbitrary process, the message will be an Love.Events.Message struct.

options

Options

  • :source - where the event originated from; defaults to nil

examples

Examples

send_message(self(), :on_selected, %{profile_id: 123})
# => def handle_message(:on_selected, _source, %{profile_id: id}, socket), do: ...

send_message({MyComponent, "my-id"}, :on_selected, %{profile_id: 123})
# => def handle_message(:on_selected, _source, %{profile_id: id}, socket), do: ...