Skip to content

social: add Client.Unsubscribe for per-handler removal - #20

Open
HashimTheArab wants to merge 1 commit into
lunarfrom
feat/social-per-handler-unsubscribe
Open

social: add Client.Unsubscribe for per-handler removal#20
HashimTheArab wants to merge 1 commit into
lunarfrom
feat/social-per-handler-unsubscribe

Conversation

@HashimTheArab

@HashimTheArab HashimTheArab commented Jul 7, 2026

Copy link
Copy Markdown
Owner

Summary

social.Client.CloseContext unsubscribes the shared RTA subscription and clears every registered handler (c.subscriptionHandlers = nil). A caller that only wants to remove its own handler — e.g. on shutdown — therefore also disables any other Subscribe users sharing the same *xsapi.Client.

Add Client.Unsubscribe(ctx, handler) for precise, per-handler removal.

Behaviour

  • Removes a single handler, matched by identity against the value passed to Subscribe.
  • Tears the shared RTA subscription down only once the last handler is removed; other handlers keep receiving events.
  • No-op if the handler is not currently registered.
  • A never-active subscription (no RTA connection) reports rta.ErrUnavailable on teardown, which is treated as success (nothing to unsubscribe).

Non-comparable handlers

Subscribe/dispatch already support non-comparable handlers, so removal can't rely on == blindly. Unsubscribe checks reflect.TypeOf(h).Comparable() up front and returns an error (rather than panicking) for a non-comparable handler. With a comparable h, the existing == h scan is panic-free: a differing dynamic type compares unequal, and an identical type is comparable.

Tests

  • TestUnsubscribeRemovesOnlyTheGivenHandler — removing one of two handlers leaves the other receiving events, keeps the subscription until the last is gone, then tears it down; a repeat removal is a no-op.
  • TestUnsubscribeRejectsNonComparableHandler — a non-comparable handler returns an error instead of panicking.

Race-clean; full social package passes.


Note

Medium Risk
Changes shared RTA subscription lifecycle and handler list under the same mutex as Subscribe; incorrect teardown could affect other subscribers, though behavior is covered by new tests and mirrors existing CloseContext patterns.

Overview
Adds Client.Unsubscribe(ctx, h) so a caller can drop only its own RTA social subscription handler without affecting other Subscribe users on the same *social.Client—unlike CloseContext, which unsubscribes and clears every handler.

Removal matches handlers by identity (== on the value passed to Subscribe). Non-comparable handlers get an upfront reflect.TypeOf check and an error instead of a panic during lookup. Unregistering an unknown handler is a no-op; when the last handler is removed, the shared RTA subscription is torn down via unsubscriber, treating rta.ErrUnavailable on a never-active subscription as success.

Tests cover selective removal, event delivery to the surviving handler, teardown only after the last handler, repeat unsubscribe as no-op, and rejection of non-comparable handlers.

Reviewed by Cursor Bugbot for commit b780abd. Bugbot is set up for automated code reviews on this repo. Configure here.

Client.CloseContext unsubscribes the shared RTA subscription and clears every
registered handler, so a caller that only wants to remove its own handler (for
example on shutdown) also disables any other Subscribe users on the same client.

Add Client.Unsubscribe(ctx, handler), which removes a single handler by identity
and tears the RTA subscription down only once the last handler is gone. Handlers
must be comparable to be removed; a non-comparable handler returns an error
rather than panicking, preserving support for non-comparable handlers in
Subscribe/dispatch.
@cursor

cursor Bot commented Jul 7, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_088cb957-ff83-48a1-a3ee-5b4105ec807d)

@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6c55425b-208f-431e-b2db-4f8712f62bc9

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/social-per-handler-unsubscribe

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

HashimTheArab added a commit to HashimTheArab/go-mcxboxbroadcast that referenced this pull request Jul 7, 2026
Closing one broadcaster called social CloseContext, which unsubscribes the
shared RTA subscription and clears every handler on the client. That could
disable other Social().Subscribe users on a shared xsapi.Client.

Use the new go-xsapi social Client.Unsubscribe(ctx, handler) to remove only the
broadcaster's own handler; the RTA subscription is torn down only when its last
handler is gone. Bumps the go-xsapi replace to the commit that adds Unsubscribe.

Depends on HashimTheArab/go-xsapi#20; the replace pins that branch commit and
should be re-pinned to lunar once it merges.
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.

1 participant