Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update function call in README.md from Map.delete to Map.pop/3 fixes #222 #222

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading