Skip to content

Keep shared codes in the batch and fetch batches wider than one page - #898

Merged
acasazza merged 1 commit into
mainfrom
fix/batch-store-refcount-and-page-size
Oct 29, 2026
Merged

acasazza merged 1 commit into
mainfrom
fix/batch-store-refcount-and-page-size

Conversation

@acasazza

Copy link
Copy Markdown
Member

Fixes #895, #896, #897.

Shared codes stay in the batch (#896)

createBatchStore now reference counts its codes (Map<string, number>). A code leaves the next batch only when its last consumer unregisters. Unregistering still never triggers a refetch.

Batches wider than one page (#895)

usePrices and useSkus fetch the standalone batch with the new fetchInChunks: chunks of 25 codes, each requested with pageSize: 25, run in parallel and merged. If a chunk still overflows its page (several prices per SKU), it logs a console.warn instead of truncating silently.

The batch gets its own SWR key (["prices", "batch", token, codes]), so the chunked fetcher applies only to the batch path. Manual fetchPrices / fetchSkus calls send the parameters they're given, as before.

Checked against the test org: 30 SKU codes in one request without pageSize return 10 of 30. With fetchInChunks all 30 come back.

Tests (#897)

  • New core-components/src/createBatchStore.spec.ts: tests the factory directly, with fake timers and a fresh store per test. It includes the two cases both bugs got through: an unregistered code is left out of the next batch, and a shared code survives one consumer's unmount. The second test fails against the old Set behaviour.
  • pricesBatchStore.test.ts and skusBatchStore.test.ts shrink from 133 lines each to a wiring check.
  • usePrices / useSkus interceptor tests check that 30 registered codes become two requests of 25 and 5 codes with pageSize: 25.

Also renamed StoreEntry.skuCodes to codes, and the factory doc now notes that every flush refetches the whole batch.

Not in this PR

When the SWR key changes, data is undefined until the new response arrives, so standalone prices briefly blank out when a new one mounts (noted in #896). keepPreviousData would fix it, but it also changes what isLoading reports, so it's left for a separate change.

The react-hooks-components live API tests skip locally because the root .env doesn't define VITE_DOMAIN and the sales-channel variables. That's unrelated to this change.

🤖 Generated with Claude Code

The batch store kept registered codes in a Set, so the first of several
components sharing a code removed it on unmount. The next flush dropped the
code from the request and the components still mounted lost their data. Codes
are now reference counted and leave the batch only with their last consumer.

The batched requests in usePrices and useSkus passed no page size, so the API
returned its default of 10 records and every code past the tenth stayed
empty. The batch is now fetched in chunks of 25 codes with pageSize 25, in
parallel, and a chunk that still overflows its page logs a warning.

createBatchStore gets its own spec with fake timers, including the cases
both bugs slipped through. The wrapper store tests shrink to a wiring check.

Fixes #895, #896, #897
@acasazza acasazza added bug Something isn't working core Core package components Components package labels Oct 28, 2026
@acasazza acasazza self-assigned this Oct 28, 2026
@netlify

netlify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for commercelayer-react-components ready!

Name Link
🔨 Latest commit 4fe7d79
🔍 Latest deploy log https://app.netlify.com/projects/commercelayer-react-components/deploys/6ae1d17688e1b316e4885705
😎 Deploy Preview https://deploy-preview-898--commercelayer-react-components.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.

@acasazza
acasazza merged commit 9dd3d4b into main Oct 29, 2026
8 checks passed
@acasazza
acasazza deleted the fix/batch-store-refcount-and-page-size branch October 29, 2026 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working components Components package core Core package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Standalone Price and Sku batches return only the first 10 records

2 participants