Rebuild ITwinGrid data loading on a shared infinite query core - #233
Conversation
7ce2ad4 to
5e12253
Compare
|
@alexdunae @ben-polinsky what do you think? It's basically tanstack's |
FYI I don't see this as a huge issue - in fact I kind of like it. What if I mistakenly toggle the incorrect star - I don't want the thing I toggled to instantly disappear - I have no chance to recover. |
|
Before I get too deep into a review, the whole |
In _packages/modules/imodel-browser/src/types.ts:173 we see that the access token can either be a string or a function: export type AccessTokenProvider = string | (() => Promise<string>)And this is also where the "provider" name was taken from as you can see. The previous EDIT: forgot to mention that this refactor actually fixed a bug that causes re-renders and re-fetches. |
Thanks. I understand how
If a caller passes a new inline function on each render, Is this PR intentionally removing the memoization requirement? |
Ah ! Sorry, no, my bad ! We are altering the contract indeed. Instead of relying on the developer to memo, I went a bit more defensive. I was TDD-ing this, and one of the tests was making sure we do not re-fetch and re-render uselessly... so basically not respecting the contract documented in the jsdoc. So, wdyt, should I revert this, or change the jsdoc ? I'd favor changing the jsdoc, and explain it is not required anymore but that we are backward-compatible. But I can also understand if you'd want to revert this. |
No worries. As long as you're confident in the solution, documenting is fine. |
Alright, thanks very much, I'll update the documentation then. |
Yes, seems indeed to be a pre-existing bug, and it also seems to be affecting the MUI version as well. I did a quick investigation and the |
alexdunae
left a comment
There was a problem hiding this comment.
Kind of mind-bendy at first, but after spending some time with the PR it seems pretty solid.
While I'm still in favour of not adding deps like tanstack unless we really need to, it's wild how complicated paginated API fetches are in React.
I think React makes everything complicated 😬 |
|
Alright, I removed |
alexdunae
left a comment
There was a problem hiding this comment.
Makes sense to me and provides a nice foundation for future data fetching in here : )

The
useITwinDatahook used five effects across two hooks to load one paginated list.It now uses two.
The hook is rebuilt on a small reusable core, the idea being that we could rewrite parts of the IModel hooks the same way ; and the 37 existing tests in
useITwinData.test.tspass unchanged.Solves AB#2141645
The new pieces
infiniteQueryReducer: a pure state machine with no React import.useInfiniteQuery: drives that reducer with two effects: one starts the query, one keeps the pending request in flight behind anAbortController. When the query prop moves ahead of the state, it dispatches during render instead of from an effect.useEventCallback: one stable identity that always calls the latest function.iTwinsApi: not a hook. It is the only file that knows the iTwins REST APIBehaviour changes
shouldRefetchFavoritesflip no longer restarts a query that still has more pages to load. That removes a double refetch and a duplicate-page append, but the favorites list no longer refreshes itself after a star toggle while more pages remain.fetchMoreisundefinedrather than defined-but-inert while an access token is required.Cache-Controlheader is sent unless the favorites cache is bypassed.How this was verified
useITwinData.tskeeps one acceptedno-pass-data-to-parentwarning.