Consolidate AgentServer subscribe API into subscribe/3 + unsubscribe/3#94
Merged
Conversation
- document better for discoverability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
The
AgentServerpub/sub API had grown into four parallel functions —subscribe/1,unsubscribe/1,subscribe_debug/1,unsubscribe_debug/1— each hard-coding both the channel (:mainvs:debug) and the subscriber pid (alwaysself()). That left no way to subscribe a foreign pid (e.g. a bridge GenServer proxying events to another transport) without dropping down to the low-levelSagents.PublisherAPI and hand-resolving the via-tuple. It also made the entry-point hierarchy hard to discover: nothing inPublisher's docs pointed callers at the higher-level wrappers, so new code tended to callPublisher.subscribe/3directly even whenAgentServer.subscribe/1would have been the right choice.Solution
Collapse the four functions into two parameterized ones:
AgentServer.subscribe(agent_id, channel \\ :main, subscriber_pid \\ nil)AgentServer.unsubscribe(agent_id, channel \\ :main, subscriber_pid \\ nil)Both default to the previous behaviour (
:mainchannel,self()as the subscriber whensubscriber_pidisnil), so existing single-arg call sites keep working unchanged. The new shape additionally supports:debugand explicit foreign-pid subscriptions in a single call.Sagents.Subscribernow routes throughAgentServer.subscribe/3instead of callingPublisher.subscribe/3with a hand-built via-tuple — eating its own dog food and keeping the via-tuple resolution in one place.Sagents.Publisher's @moduledoc grew a 'Typical entry points' section that explicitly steers callers toAgentServer.subscribe/3,FileSystemServer.subscribe/1, orSagents.Subscriber, and explains when reaching forPublisher.subscribe/3directly is actually appropriate (only when implementing a new producer module).Changes
Testing
`mix test` covers the migration: every existing call site was updated and the existing debug-channel tests still pass under the new API. The new `subscribe/3` describe blocks specifically exercise the previously-unreachable behaviours — foreign-pid delivery, idempotent re-subscribe returning the same monitor_ref, and `unsubscribe/3` cleanly stopping delivery on the `:debug` channel (including the not-running case).