-
This is general good for preventing effect running infinitely, but sometimes it may be necessary to rerun it when setting in a async function. Is it possible today that we could force rerun the effect in the effect function? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
This is possible, and should be implemented with an abort controller so that you can cancel the current run on cleanup. This is only possible by writing to a writable atom and delaying a microtask inside the writable atom. This effectively bypasses the infinite loop check. Here is an example of how to do this. Alternatively, you could use a while loop inside atomEffect so that it never completes unless something causes it to rerun. This strategy is more readable because it doesn't need to bypass the infinite loop check. This is because the atomEffect is not listening to the atoms it is changing, so there is no infinite read loop. Here is an example using a while loop |
Beta Was this translation helpful? Give feedback.
-
Should atomEffect prevent async infinite loops? atomEffect((get, set) => {
get(countAtom)
setTimeout(() => {
set(countAtom, increment)
}, 1000)
}) |
Beta Was this translation helpful? Give feedback.
-
I have a PR that introduces a new api to support infinite loops out now: #23 |
Beta Was this translation helpful? Give feedback.
-
Here is a codesandbox example that uses the new |
Beta Was this translation helpful? Give feedback.
This is possible, and should be implemented with an abort controller so that you can cancel the current run on cleanup. This is only possible by writing to a writable atom and delaying a microtask inside the writable atom. This effectively bypasses the infinite loop check.
Here is an example of how to do this.
https://codesandbox.io/s/nifty-julien-d3xgsy
Alternatively, you could use a while loop inside atomEffect so that it never completes unless something causes it to rerun. This strategy is more readable because it doesn't need to bypass the infinite loop check. This is because the atomEffect is not listening to the atoms it is changing, so there is no infinite read loop.
Here is an exa…