Skip to content

feat: move async store out of react component#14

Merged
quolpr merged 4 commits into
mainfrom
quolpr/async-store
Jul 4, 2026
Merged

feat: move async store out of react component#14
quolpr merged 4 commits into
mainfrom
quolpr/async-store

Conversation

@quolpr

@quolpr quolpr commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Added async selector store support, including cached and uncached options for subscriptions outside React.
    • useAsyncSelector now uses the shared async selector store behavior and supports improved refetch and cleanup handling.
  • Documentation

    • Expanded guides and quick-start docs with clearer selector-store selection, async subscription examples, and updated React integration notes.
    • Updated documentation navigation to reflect the new async selector store APIs.

@codesandbox

codesandbox Bot commented Jul 4, 2026

Copy link
Copy Markdown

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

@netlify

netlify Bot commented Jul 4, 2026

Copy link
Copy Markdown

Deploy Preview for hyperdb ready!

Name Link
🔨 Latest commit ef8ff80
🔍 Latest deploy log https://app.netlify.com/projects/hyperdb/deploys/6a48d0627e98e100088e4d0c
😎 Deploy Preview https://deploy-preview-14--hyperdb.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@quolpr, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 2 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2d63c52a-5f1b-4ee9-9198-a4e68d0f3ee1

📥 Commits

Reviewing files that changed from the base of the PR and between c77909c and ef8ff80.

📒 Files selected for processing (21)
  • .changeset/metal-cows-search.md
  • .changeset/sync-dispatch-hook.md
  • README.md
  • TODO.md
  • packages/hyperdb-doc/astro.config.mjs
  • packages/hyperdb-doc/src/content/docs/database/actions.md
  • packages/hyperdb-doc/src/content/docs/database/reading-data.md
  • packages/hyperdb-doc/src/content/docs/database/selectors-reactivity.md
  • packages/hyperdb-doc/src/content/docs/database/selectors.md
  • packages/hyperdb-doc/src/content/docs/integrations/react.md
  • packages/hyperdb-doc/src/content/docs/runtime/db.md
  • packages/hyperdb-doc/src/content/docs/runtime/drivers.md
  • packages/hyperdb-doc/src/content/docs/start/how-it-works.md
  • packages/hyperdb-doc/src/content/docs/start/llm-cheat-sheet.md
  • packages/hyperdb-doc/src/content/docs/start/quickstart.md
  • packages/hyperdb-doc/src/content/docs/start/why.md
  • packages/hyperdb-doc/summary.md
  • packages/hyperdb/src/hyperdb/commands/selector/async-selector-store.test.ts
  • packages/hyperdb/src/hyperdb/commands/selector/async-selector-store.ts
  • packages/hyperdb/src/react/hooks.test.ts
  • packages/hyperdb/src/react/hooks.ts
📝 Walkthrough

Walkthrough

This PR adds a new async selector store module (createAsyncSelectorStore, createCachedSelectorStoreAsync) to hyperdb, with public types, internal state management, DB subscription, rerun coordination, and error handling. React's useAsyncSelector is rewritten to use the cached store via useSyncExternalStore, replacing its prior internal state machine. Documentation is updated accordingly.

Changes

Async selector store core

Layer / File(s) Summary
Store types and initial state
packages/hyperdb/src/hyperdb/commands/selector/async-selector-store.ts
Defines AsyncSelectorStatus, AsyncSelectorFetchStatus, AsyncSelectorRefetchOptions, state/store input types, and internal helpers to derive initial query state and snapshot flags.
Store engine and factories
packages/hyperdb/src/hyperdb/commands/selector/async-selector-store.ts, packages/hyperdb/src/hyperdb/index.ts
Implements listener management, DB subscription/rerun coordination, run-token guarded execution, error/stale handling, and exports createAsyncSelectorStore/createCachedSelectorStoreAsync; re-exports the module from the package index.
Store test suite
packages/hyperdb/src/hyperdb/commands/selector/async-selector-store.test.ts
Adds tests covering sync/async resolution, range-filtered subscriptions, rerun collapsing, destroy handling, refetch success/error, and cached vs uncached routing.

React useAsyncSelector rewrite

Layer / File(s) Summary
Hook implementation
packages/hyperdb/src/react/hooks.ts
Replaces the custom async state machine in useAsyncSelector with a createCachedSelectorStoreAsync-backed subscription via useSyncExternalStore, updates type re-exports and dependency wiring.
Hook test updates
packages/hyperdb/src/react/hooks.test.ts
Updates mocks/assertions to use selectCachedMaybeAsync, adds an addCleanup helper, and removes obsolete internal ref assertions.

Documentation updates

Layer / File(s) Summary
Docs and README
README.md, packages/hyperdb-doc/src/content/docs/database/reading-data.md, packages/hyperdb-doc/src/content/docs/database/selectors-reactivity.md, packages/hyperdb-doc/src/content/docs/integrations/react.md, packages/hyperdb-doc/src/content/docs/start/llm-cheat-sheet.md, packages/hyperdb-doc/summary.md
Documents the new async selector store helpers, adds a store-helper matrix and usage examples, and updates the doc navigation summary.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Component
  participant useAsyncSelector
  participant CachedSelectorStoreAsync
  participant SubscribableDB

  Component->>useAsyncSelector: render
  useAsyncSelector->>CachedSelectorStoreAsync: createCachedSelectorStoreAsync(db, input)
  useAsyncSelector->>CachedSelectorStoreAsync: useSyncExternalStore(subscribe, getSnapshot)
  CachedSelectorStoreAsync->>SubscribableDB: subscribe to relevant ranges
  CachedSelectorStoreAsync-->>useAsyncSelector: initial snapshot (pending/success)
  SubscribableDB-->>CachedSelectorStoreAsync: notify overlapping DB change
  CachedSelectorStoreAsync->>CachedSelectorStoreAsync: rerun selector, update state
  CachedSelectorStoreAsync-->>useAsyncSelector: notify snapshot update
  useAsyncSelector-->>Component: re-render with result
  Component->>useAsyncSelector: unmount
  useAsyncSelector->>CachedSelectorStoreAsync: destroy()
Loading

Possibly related PRs

  • will-be-done/hyperdb#5: Both PRs directly modify the async selector/React integration codepaths, notably useAsyncSelector behavior around "maybe async" selector execution.
  • will-be-done/hyperdb#6: Both PRs change useAsyncSelector's async hook contract and state handling.
  • will-be-done/hyperdb#8: Both PRs touch the cached selector execution path (runCachedSelectorMaybeAsync) that the new async selector stores rely on.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: extracting the async store implementation from React into a framework-agnostic module.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch quolpr/async-store

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
packages/hyperdb/src/hyperdb/commands/selector/async-selector-store.test.ts (1)

42-413: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Suite reads well and matches current behavior; false-positive static analysis hints noted but ignored.

The execSync flagged by OpenGrep at multiple lines is the DB test helper imported from ../../db, not Node's child_process.execSync — not an actual injection risk.

Consider adding regression coverage for the two lifecycle/concurrency issues raised in async-selector-store.ts: (1) unsubscribe-then-resubscribe with no intervening DB write while a run is still in-flight (currently no test exercises started reset), and (2) calling refetch() with different options while another run is already in-flight.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/hyperdb/src/hyperdb/commands/selector/async-selector-store.test.ts`
around lines 42 - 413, Add regression coverage in createCachedSelectorStoreAsync
tests for the two missing async-store edge cases: a subscriber unsubscribing and
then resubscribing while a selector run is still in-flight, and a refetch() call
with different options while another run is already pending. Use the existing
createCachedSelectorStoreAsync, store.subscribe/unsubscribe, and store.refetch
paths to verify the started state resets correctly and that overlapping
runs/options behave as expected.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/hyperdb/src/hyperdb/commands/selector/async-selector-store.ts`:
- Around line 508-559: The selector store can get stuck after a full
unsubscribe/resubscribe because the cleanup in subscribe never resets started,
so ensureStarted() and the runRevision check can both skip a needed refresh.
Update the last-listener teardown in subscribe to reset started alongside the
existing run state, and also clear any pending stale timer via clearStaleTimer()
so no background work survives after the store is fully unsubscribed. Keep the
fix centered around run(), ensureStarted(), ensureDBSubscription(), and the
subscribe cleanup path.
- Around line 364-377: The shared in-flight path in AsyncSelectorStore’s
run/refetch logic is reusing the original promise, so a later caller’s
throwOnError option can be ignored. Update the isRunning branch in
async-selector-store.ts so piggybacking refetch() calls still respect the
current caller’s options, especially when options.throwOnError is true, rather
than always inheriting the first request’s behavior. Use the run function and
inFlightResult handling to locate the change and ensure concurrent callers can
get a rejecting promise when requested.

In `@packages/hyperdb/src/react/hooks.ts`:
- Around line 258-295: The store in hooks.ts is being recreated whenever
reference-unstable option values change, because the `storeInput` memo in the
selector hook depends directly on raw `defaultValue`, `initialData`, and
`placeholderData` references. Update the memoization around `storeInput` and the
`createCachedSelectorStoreAsync` call so these options are derived through a
stable key or otherwise normalized, and reference the existing `useMemo` blocks
and `storeInput`/`store` symbols to keep the store stable across unrelated
rerenders.

---

Nitpick comments:
In `@packages/hyperdb/src/hyperdb/commands/selector/async-selector-store.test.ts`:
- Around line 42-413: Add regression coverage in createCachedSelectorStoreAsync
tests for the two missing async-store edge cases: a subscriber unsubscribing and
then resubscribing while a selector run is still in-flight, and a refetch() call
with different options while another run is already pending. Use the existing
createCachedSelectorStoreAsync, store.subscribe/unsubscribe, and store.refetch
paths to verify the started state resets correctly and that overlapping
runs/options behave as expected.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 6b8ad2ef-b427-49a0-9dd6-8875326d3257

📥 Commits

Reviewing files that changed from the base of the PR and between ac152b4 and c77909c.

📒 Files selected for processing (11)
  • README.md
  • packages/hyperdb-doc/src/content/docs/database/reading-data.md
  • packages/hyperdb-doc/src/content/docs/database/selectors-reactivity.md
  • packages/hyperdb-doc/src/content/docs/integrations/react.md
  • packages/hyperdb-doc/src/content/docs/start/llm-cheat-sheet.md
  • packages/hyperdb-doc/summary.md
  • packages/hyperdb/src/hyperdb/commands/selector/async-selector-store.test.ts
  • packages/hyperdb/src/hyperdb/commands/selector/async-selector-store.ts
  • packages/hyperdb/src/hyperdb/index.ts
  • packages/hyperdb/src/react/hooks.test.ts
  • packages/hyperdb/src/react/hooks.ts

Comment thread packages/hyperdb/src/react/hooks.ts Outdated
@quolpr quolpr merged commit 2d6655f into main Jul 4, 2026
8 checks passed
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