Skip to content

handleSubscriptionCreated/Deleted uses unreadCount for both total and unread deltas #576

@brendanlong

Description

@brendanlong

This is related to #580 and a PR fixing this should target the tanstack-db branch.

Problem

In src/lib/cache/operations.ts:74-79, when a subscription is created, the handler adjusts the global "all" entry count using subscription.unreadCount for both the totalDelta and unreadDelta:

// handleSubscriptionCreated
adjustEntriesCountInCollection(
  collections ?? null,
  "all",
  subscription.unreadCount, // totalDelta — should be total entry count
  subscription.unreadCount  // unreadDelta — correct
);

The same issue exists in handleSubscriptionDeleted (lines 114-119):

adjustEntriesCountInCollection(
  collections ?? null,
  "all",
  -subscription.unreadCount, // totalDelta — should be total entry count
  -subscription.unreadCount  // unreadDelta — correct
);

Impact

  • On subscribe: If the new subscription has any read entries (e.g., from a previous subscription period), the global total count will be undercounted by the number of read entries
  • On unsubscribe: If the subscription had any read entries, the global total count will be undercounted (not enough is subtracted... wait, the sign is negative so it would be overcounted)

In practice, for newly created subscriptions this is likely correct since all entries should be unread at subscription time. But for unsubscribe, it's wrong if any entries were read before unsubscribing.

Possible fix

The subscription_created SSE event could include a total entry count in addition to the unread count. For subscription_deleted, the client could look up the total from the local subscription data before removing it (though the subscription object currently only has unreadCount, not a total entry count).

Alternatively, add a totalCount field to the subscription model/event, or accept this as a known approximation with a code comment explaining the assumption.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions