Skip to content

fix(topic): tie subscribe lifecycle to consumer-read, not send() - #32

Merged
jimmy-phantom merged 3 commits into
mainfrom
fix-topic-subscribe-consumer-lifecycle
May 22, 2026
Merged

jimmy-phantom merged 3 commits into
mainfrom
fix-topic-subscribe-consumer-lifecycle

Conversation

@jimmy-phantom

@jimmy-phantom jimmy-phantom commented May 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

TopicQuery's intuitive contract is that subscribe/unsubscribe to a topic tracks consumer-read lifecycle: while any consumer is reading the relay, the topic is subscribed on the wire; when the last consumer goes away, it's unsubscribed. Pre-fulfillment and cache are orthogonal optimizations for data freshness and should not affect the subscribe lifecycle.

Today the subscribe is wired to the fetch path: TopicQueryAdapter.send calls adapter.subscribe(topic), and the default TopicQuery.getConfig.subscribe only defines the cleanup half (depending on _topicAdapter being set inside send). So whenever send short-circuits (pre-fulfilled topic via fulfillTopic, or cache within staleTime), subscribe never fires, but the cleanup still fires on consumer unmount and tears down adapter state that was never registered.

Fix

  1. Set ctx._topicAdapter eagerly in QueryResult.getOrCreateExecutionContext so the default subscribe can reach the adapter without depending on send having run.
  2. Implement both halves of the default TopicQuery.getConfig.subscribe, and remove the now-redundant subscribe call from send. reconcileSubscription also runs unconditionally on activation so subscribe wires up before the first fetch awaits.

Test plan

  • New test asserts subscribe and unsubscribe both fire (counts balanced) on the pre-fulfilled path.
  • Full unit suite passes (1139 tests); tsc --noEmit clean.

jimmy-phantom and others added 2 commits May 21, 2026 18:41
The default TopicQuery.getConfig.subscribe only defined the cleanup half
and depended on _topicAdapter being set inside TopicQueryAdapter.send.
Whenever send short-circuited (pre-fulfilled topic via fulfillTopic, or
in-memory cache within staleTime), subscribe never fired but unsubscribe
still fired on consumer unmount, tearing down adapter state that was
never registered.

Set the adapter on the execution context eagerly in
QueryResult.getOrCreateExecutionContext, implement both halves of the
default subscribe, and drop the now-redundant subscribe call from send.
reconcileSubscription is also called unconditionally on activation so
that subscribe runs before the first fetch awaits.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Drop the internal-mechanism sentence and the restatement; CHANGELOG
readers don't have the internal symbols and the first sentence already
covers what changes for them.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jimmy-phantom
jimmy-phantom requested a review from pzuraq May 22, 2026 01:48
@jimmy-phantom
jimmy-phantom merged commit e395061 into main May 22, 2026
1 of 4 checks passed
@jimmy-phantom
jimmy-phantom deleted the fix-topic-subscribe-consumer-lifecycle branch May 22, 2026 22:05
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.

2 participants