Quetzakol

Quetzakol

LiveComponent updating itself at regular intervals

Hello! :slightly_smiling_face:

I am playing a bit with Phoenix LiveView and Surface, and I’m struggling to find the right way to do a component.

Basically, I wanted to do a component with LiveComponent, that takes some text as a property. Then the component would display the text progressively, starting from a blank line and adding characters at a regular pace, like dialogs in video games.

Intuitively, I thought my stateful LiveComponent would be able to use Process.send_after and update the text being displayed in an handle_info. As it turns out, LiveComponents run in the process of their parent LiveView so I can’t do that. The only solution I found then is to handle that in the parent LiveView, but that means breaking the encapsulation as most of the component’s behaviour would not be handled by itself.

So I’m curious, how would you handle such a component?

Thanks for your help

First Post!

lud

lud

This looks like a job for JavaScript.

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

We’ve had similar real world situations (components subscribing to external information) and at the moment you simply can’t encapsulate it without using private Phoenix LiveView APIs. We just bit the bullet and have the parent live view do a handle_info to send_update.

However, I believe there are plans to allow send_update to work from external processes which would allow you to ping the component directly.

AndyL

AndyL

I had another look at this, re-implemented as a LiveView, and everything is much simpler.

defmodule MyApp.ClockLive do
  @moduledoc """
  Renders a live clock that updates at a periodic interval.

  The clock update frequency (`interval`) and the output format (`strftime`)
  are configurable session options.

  To call from the parent template:

      <%= live_render(@socket, MyApp.ClockLive, id: 1, session: %{"interval" => 10_000}) %>
      <%= live_render(@socket, MyApp.ClockLive, id: 2, session: %{"strftime" => "%H:%M"}) %>

  """

  use Phoenix.LiveView

  @impl true
  def mount(_params, session, socket) do
    start_timer(session["interval"] || 1000)
    strftime = session["strftime"] || "%H:%M:%S"
    state = [strftime: strftime, date: local_date(strftime)]
    {:ok, assign(socket, state)}
  end

  @impl true
  def render(assigns) do
    ~L"""
    <%= @date %>
    """
  end

  @impl true
  def handle_info(:clock_tick, socket) do
    newdate = socket.assigns.strftime |> local_date()
    {:noreply, update(socket, :date, fn _ -> newdate end)}
  end

  defp start_timer(interval) do
    :timer.send_interval(interval, self(), :clock_tick)
  end

  defp local_date(format) do
    NaiveDateTime.local_now()
    |> Calendar.strftime(format)
  end
end

AndyL

AndyL

I wrote a clock component, and ran into this self-updating issue too!

My solution also requires a handle-info handler in the parent LiveView…

defmodule MyApp.ClockComponent do
  @moduledoc """
  Renders a live clock that updates at a periodic interval.

  The clock update frequency (`interval`) and the output format (`strftime`)
  are configurable options.

  To call from the parent template:
  
      <%= live_component(@socket, MyApp.ClockComponent, id: 1, interval: 10_000 %>
      <%= live_component(@socket, MyApp.ClockComponent, id: 2, strftime: "%H:%M" %>

  To make this work, the parent LiveView needs a handle_info function:

      @impl true
      def handle_info({:tick, assigns}, socket) do
        send_update MyApp.ClockComponent, assigns
        {:noreply, socket}
      end

  The parent `handle_info` is required because there is no `handle_info` callback 
  for LiveComponent.  

  Ben Wilson (@benwilson512) on Elixir Forum says that in the future, there may
  be a `send_update` feature to allow external processes to ping the component
  directly.

  https://forum.elixirforum.com/t/livecomponent-updating-itself-at-regular-intervals/37047/6

  Also the code in the `update` callback is more complex than desired.

  Ideally the timer would start once in the `mount` callback.  This was not
  possible because the timer interval should be configurable in the assigns,
  and the assigns are not available in the `mount` params. (AFAIK)

  Maybe there is a simpler approach for starting the timer...
  """

  use Phoenix.LiveComponent

  @impl true
  def render(assigns) do
    ~L"""
    <%= @date %>
    """
  end

  @impl true
  def mount(socket) do
    {:ok, socket}
  end

  @impl true
  def update(assigns, socket) do
    opts = update_assigns(assigns) 
    unless assigns[:timer], do: start_timer(opts)
    {:ok, assign(socket, opts)}
  end

  defp update_assigns(assigns) do
    strftime = assigns[:strftime] || "%H:%M:%S"
    [
      id: assigns[:id],
      timer: "started",
      interval: assigns[:interval] || 1000,
      strftime: strftime,
      date: local_date(strftime)
    ]
  end

  defp local_date(format) do
    NaiveDateTime.local_now()
    |> Calendar.strftime(format)
  end

  defp start_timer(opts) do
    :timer.send_interval(opts[:interval], self(), {:tick, opts})
  end
end

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement