S
SolidJS•12mo ago
Mr Void

accessing store multiple time, in batch() causes infinite loop

createEffect(() => {
batch(async () => {
for (const card of collection.cards) {
// some async call ...
const tempCard = {...card}
// updating properties
store.cards.dispatch.updateCard(tempCard)
}
// ...
})
})
createEffect(() => {
batch(async () => {
for (const card of collection.cards) {
// some async call ...
const tempCard = {...card}
// updating properties
store.cards.dispatch.updateCard(tempCard)
}
// ...
})
})
Calling store's function within the loop causes infinite rendering. The batch function is inside createEffect. How could I avoid an infinite loop, while still updating the objects 1 by 1 ?
5 Replies
lxsmnsyc
lxsmnsyc•12mo ago
Are you required to track collection.cards
Mr Void
Mr VoidOP•12mo ago
I am not exactly certain. I have a grid of cards, each card has its own picture which is retrieved by some async call to supabase. I thought about making a request for each card and updating the store in order to cache the generated signedUrl for the picture
foolswisdom
foolswisdom•12mo ago
Not exactly your question, but do you know that the batch only batches up to the first await (after which point the callback/effect has ended from solid's point of view)?
Mr Void
Mr VoidOP•12mo ago
no, didn't know 🤔
foolswisdom
foolswisdom•12mo ago
Solid's global listener stack doesn't really work properly if you have other things executing at the same time as the effect (which is what may happen with async code), so it doesn't even try to handle it. The moment a function returns (which, in an async function, happens at the first await), the effect is over, even though it has returned a promise that can be awaited Anyway, I think that means that every time you update a card, it immediately triggers the effect (before it even continues to the rest of the cards), and isn't caught by the batch BTW, all effects are already wrapped in a batch, so explicitly batching only makes a difference outside effects (like in an event handler)
Want results from more Discord servers?
Add your server