mortenlund

mortenlund

Hi!

Suggest to add the ability to do use the components socket as input into Phoenix.LiveView.send_update/3 function to make it easier to send an update to self.

Using @myself as an argument is good, but think it would be easier to grasp when just using the current socket.
I think the cid is present in the socket and the module can be fetched using __MODULE__ is guess? :slight_smile:

Example:

@impl true
def update(%{title: title}, socket) do
  updated_socket = socket
  |> assign(:title, title)

  {:ok, updated_socket}
end

@impl true
def handle_event("update_title", %{"title" => title} = _params, socket) do
  updated_socket = socket
  |> send_update(title: title)

  {:noreply, updated_socket}
end

Showing Posts 1 to 2

steffend

steffend

Phoenix Core Team

You can always write a helper function and import that:

def send_update(%Phoenix.LiveView.Socket{assigns: %{myself: cid}}, assigns) do
  Phoenix.LiveView.send_update(cid, assigns)
end

Your example is also a bit contrived, since you could just assign the value directly. Also note that send_update is asynchronous. It sends a message to the LiveView process, which invokes the update callback as soon as it handles the message. If your code relies on some data wrangling update/2 does, you can just directly call update instead:

@impl true
def handle_event("update_title", %{"title" => title} = _params, socket) do
  {:ok, updated_socket} = update([title: title], socket)

  {:noreply, updated_socket}
end
mortenlund

mortenlund OP

Hi, yes I can write such a function, and I have :slight_smile:
Just thought it would be a nice addition to the standard library :slight_smile:

And yes, you are right, my example was not very good, and after your example I am not so sure why I did not think of that :stuck_out_tongue:

— All posts loaded —

Where Next? Top

Trending in Proposals: Ideas Top

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews