Improve Phoenix.Presence.get_by_key/2 documentation, return value, and typespec.
#6042
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
presencetypespec that returns a map with the:metas.presencefound under the suppliedkeyinsocket_or_topicscope, returnnil. Pros of having anilreturn value instead of[]is that it's easier to evaluate not being a truthy value.Phoenix.Presence.get_by_key/2to return a map instead of a list that contains themetasinformation, along with some additional info that might be returned on thePhoenix.Presence.fetch/2callback.test/phoenix/presence_test.exsto check withis_nilinstead of[]if the returned value on untrackedkeyis empty.