<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="380740" data-post-id="380740">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="garrison" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  garrison
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="garrison" data-post="31" data-topic="73715">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/g/3bc359/48.png" class="avatar"> garrison:</div>
<blockquote>
<p>but it seems to me like a simple <code>MapSet</code> of ids on the server would be enough to fix this</p>
</blockquote>
</aside>
<p>Unfortunately this appears to be wrong. I think in order to make this glitch-free you need to perform reconciliation locally. Because LV can teleport components you never know if a component will be remounted far away in the render cycle, so while you can fix this in most cases you can never provide a guarantee.</p>
<p>Perhaps this is what you were getting at? If so I just didn’t understand. This is actually a very interesting insight, as I thought the teleportation behavior was a neat trick but it would seem it’s actually a bit problematic. I’ll have to keep that in mind.</p>
<p>Unfortunately that means the glitchy behavior is the best we can do. But on the bright side it’s still better than nothing <em>and</em> I think we can hack together a working implementation in userspace…</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="380740" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/are-there-elegant-ways-to-sub-unsub-when-user-navigated-between-routers-in-same-liveview/73715/32">Post #31</a>
	                </div>
	            </div>
              <div id="likers-container-380740" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="380740"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #31"></div>
  </section>
</div>
    <div class="postbit" id="380743" data-post-id="380743">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="garrison" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  garrison
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Root do
  def handle_event("invoke", %{"id" =&gt; id}, socket),
    do: {:noreply, invoke(socket, id)}

  def handle_info({:register, id, callback}, socket) do
    callbacks = Map.put(socket.assigns.callbacks, id, callback)
    {:noreply, assign(socket, :callbacks, callbacks)}
  end

  def handle_info({:unregister, id}), socket),
    do: {:noreply, invoke(socket, id)}

  defp invoke(socket, id) do
    {callback, callbacks} = Map.pop!(socket.assigns.callbacks, id)
    callback.()
    assign(socket, :callbacks, callbacks)
  end
end
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Component do
  def update(%{"product_id" =&gt; new_pid}, socket) do
    if (socket.assigns[:product_id]) != new_pid do
      if cid = socket.assigns.cid, do: send self(), {:invoke, cid}
      cid = to_string(System.unique_integer())
      send self(), {:register, cid, fn -&gt; unsubscribe(new_pid) end}
      subscribe(new_pid)
      {:ok, assign(socket, product_id: new_pid, cid: cid)}
    else
      {:ok, socket}
    end
  end

  def render(assigns) do
    ~H"""&lt;div id={@id} phx-remove={JS.push_event("invoke", %{id: @cid})} /&gt;"""
  end
end
</code></pre>
<p>That is the most elegant general solution I can think of. It’s certainly not as idiomatic as an <code>unmount</code> callback could be, but given the above insights it does appear to be functionally the same!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="380743" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/are-there-elegant-ways-to-sub-unsub-when-user-navigated-between-routers-in-same-liveview/73715/33">Post #32</a>
	                </div>
	            </div>
              <div id="likers-container-380743" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="380743"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>