Skip to content

fix: mobile useFeed stale closure and subscription churn - #1413

Open
Benedict315 wants to merge 2 commits into
Epta-Node:mainfrom
Benedict315:fix/mobile-usefeed-stale-closure
Open

fix: mobile useFeed stale closure and subscription churn#1413
Benedict315 wants to merge 2 commits into
Epta-Node:mainfrom
Benedict315:fix/mobile-usefeed-stale-closure

Conversation

@Benedict315

@Benedict315 Benedict315 commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

This commit fixes two critical bugs in apps/mobile/hooks/useFeed.ts:

  1. Stale closure in syncWithNetwork offset calculation:

    • Added postsLengthRef to track current posts length via ref instead of closure
    • Updated loadFromCache to sync postsLengthRef with current state
    • Updated syncWithNetwork to sync postsLengthRef with current state
    • This prevents pagination offsets from being computed from stale closures
  2. Subscription churn on every posts.length change:

    • Changed subscribe effect dependency from [posts.length] to []
    • Subscribe effect now uses postsLengthRef.current instead of posts.length closure
    • This prevents subscriptions from tearing down and re-creating on each pagination

Impact:

  • Pagination offsets now computed from current state, not stale closures
  • Realtime subscriptions are stable across pagination
  • Eliminates redundant re-renders and network activity during scrolling
  • Prevents skipped or duplicated posts during cache-then-network pagination

Tests:

  • Added test for cache-then-network pagination correctness
  • Verifies no duplicates and correct ascending order of post IDs
  • Existing test for cursor retention on network failure still passes

Acceptance criteria met:
✓ Offsets computed from current state, not stale closure ✓ Realtime subscriptions stable across pagination
✓ Test covers cache-then-network pagination correctness

Summary

Type of Change

  • Bug fix
  • New feature
  • Contract change (logic, storage, or API)
  • Documentation update
  • Refactor / chore

Testing Done

  • cargo test passes
  • New tests added for changed behaviour
  • Manually verified on Testnet (if applicable)
  • Database migration tested (if applicable)
  • TypeScript SDK tests passed (if applicable)

Checklist

  • Changes are focused — one concern per PR
  • If a contract function was added or changed, the README API table is updated
  • No unresolved merge conflicts
  • No secrets or private keys committed

Related Issue

Closes #1303

This commit fixes two critical bugs in apps/mobile/hooks/useFeed.ts:

1. Stale closure in syncWithNetwork offset calculation:
   - Added postsLengthRef to track current posts length via ref instead of closure
   - Updated loadFromCache to sync postsLengthRef with current state
   - Updated syncWithNetwork to sync postsLengthRef with current state
   - This prevents pagination offsets from being computed from stale closures

2. Subscription churn on every posts.length change:
   - Changed subscribe effect dependency from [posts.length] to []
   - Subscribe effect now uses postsLengthRef.current instead of posts.length closure
   - This prevents subscriptions from tearing down and re-creating on each pagination

Impact:
- Pagination offsets now computed from current state, not stale closures
- Realtime subscriptions are stable across pagination
- Eliminates redundant re-renders and network activity during scrolling
- Prevents skipped or duplicated posts during cache-then-network pagination

Tests:
- Added test for cache-then-network pagination correctness
- Verifies no duplicates and correct ascending order of post IDs
- Existing test for cursor retention on network failure still passes

Acceptance criteria met:
✓ Offsets computed from current state, not stale closure
✓ Realtime subscriptions stable across pagination
✓ Test covers cache-then-network pagination correctness
@drips-wave

drips-wave Bot commented Aug 29, 2026

Copy link
Copy Markdown

@Benedict315 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@vercel

vercel Bot commented Aug 29, 2026

Copy link
Copy Markdown

@jotel-dev is attempting to deploy a commit to the Jaja's projects Team on Vercel.

A member of the Team first needs to authorize it.

Run cargo fmt to fix formatting issues in:
- packages/contracts/contracts/linkora-contracts/src/lib.rs
- packages/contracts/contracts/linkora-contracts/src/test.rs

Changes include:
- Line wrapping for better readability
- Removed trailing whitespace
- Consistent formatting per rustfmt rules
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.

Mobile: useFeed re-subscribes and recomputes offsets on every posts.length change — stale-closure bug

2 participants