Skip to content

Conversation

jaspervanbrian
Copy link

Issue

#5759 states that issue #4514 wasn't addressed properly. Checking the docs for Phoenix.Presence.get_by_key/2, it does have inconsistencies between its return value and the typespec it's being referred to.

Improvements and Changes

Going by the given problem and proposed solution from #4514, I have made the changes:

  • Improve and fix the presence typespec that returns a map with the :metas.
  • If no presence found under the supplied key in socket_or_topic scope, return nil. Pros of having a nil return value instead of [] is that it's easier to evaluate not being a truthy value.
  • Change the documented return value of Phoenix.Presence.get_by_key/2 to return a map instead of a list that contains the metas information, along with some additional info that might be returned on the Phoenix.Presence.fetch/2 callback.
  • Change test specs on test/phoenix/presence_test.exs to check with is_nil instead of [] if the returned value on untracked key is empty.

- Change presence typespec, and use presence type for presences
  typespec.
- Return `nil` for empty presence on `Presence.get_by_key/2`.
- Assert is_nil instead of empty array on `Presence.get_by_key/2` test.
- Improve documentation of `Presence.get_by_key/2` callback.
@jaspervanbrian jaspervanbrian changed the title Improve Phoenix.Presence.get_by_key/2 documentation, return value, and typespec. Improve Phoenix.Presence.get_by_key/2 documentation, return value, and typespec. Jan 8, 2025
@SteffenDE SteffenDE requested a review from chrismccord January 8, 2025 13:58
@bkilshaw
Copy link
Contributor

bkilshaw commented May 5, 2025

Just ran into this; it feels bit odd to get back either a map or an empty list, 'nil` feels like a good solution.

@SteffenDE
Copy link
Contributor

Changing the return value is a breaking change, so I'm not sure we can do this (cc @josevalim).

@jaspervanbrian jaspervanbrian moved this from In Progress to Being Reviewed in @jaspervanbrian's Open Source Contributions Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants