From abdc01dad3d8bcfe8eeb0dd2a270bd8b1ea3927d Mon Sep 17 00:00:00 2001 From: Michael Rundle <129297573+MikeRund@users.noreply.github.com> Date: Wed, 9 Oct 2024 13:04:59 +0100 Subject: [PATCH] Update README.md Context: Adding Presence Replaced `Math.delete` for `Math.pop/3` in description Replaced `Math.pop!` for `Math.pop` --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ae2dd148..3b735fd7 100644 --- a/README.md +++ b/README.md @@ -1588,10 +1588,11 @@ but not here) so the rest of the code goes into Notice that since we populate the socket's state in the `mount/3` callback, and compute the Presence there, we need to remove the connected client from the joins in the `handle_info` callback. - We use `Map.delete` to remove the client from the joins. + We use `Map.pop/3` to remove the client from the joins (note: `Math.pop/3` returns + a default map we parse in if `key` is not present in the map). This works because the client is identified by the socket's `id` and Presence process returns a map whose key value is the `socket.id`. -3. We publish the additional data to the client in `render` +4. We publish the additional data to the client in `render` ```diff defmodule CounterWeb.Counter do @@ -1638,7 +1639,7 @@ defmodule CounterWeb.Counter do + %{event: "presence_diff", payload: %{joins: joins, leaves: leaves}}, + %{assigns: %{present: present}} = socket + ) do -+ {_, joins} = Map.pop!(joins, socket.id, %{}) ++ {_, joins} = Map.pop(joins, socket.id, %{}) + new_present = present + map_size(joins) - map_size(leaves) + + {:noreply, assign(socket, :present, new_present)}