Skip to content

Commit

Permalink
Merge pull request #222 from dwyl/MikeRund-patch-1
Browse files Browse the repository at this point in the history
update function call in README.md from Map.delete to Map.pop/3 fixes #222
  • Loading branch information
ndrean authored Oct 9, 2024
2 parents 6d304a5 + abdc01d commit 87b0bbe
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)}
Expand Down

0 comments on commit 87b0bbe

Please sign in to comment.