You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, effects can only be ended from the "outside" - i.e., the caller creating the effect. But there are times when it's advantageous to allow an effect to cancel itself, since it knows when it is "done". (In particular, one can have effects decide when to end based on a shared signal, which is the use case I'm working on right now.)
In principle, you can share the dispose callback from the enclosing scope, and perhaps even make a helper function to simplify that process. But since the effect callback is synchronously invoked, this creates an edge case where you can't dispose the effect before the effect() call returns. Basically, you currently need a helper like this:
functiondisposableEffect(fn: (dispose: ()=>void)=>void|(()=>void)){letdispose=function(){// wait for the effect() call to returnPromise.resolve().then(()=>dispose());}returndispose=effect(function(){returnfn.call(this,dispose);});}
This seems like it would be greatly simplified by making what is now _dispose into a public method, so that effect callbacks can just call this.dispose(), or even just return a special value to indicate that they wish to be disposed of.
(Note: while you can certainly stop an effect from having dependencies eventually, you can't actually get it to release without first receiving another callback, and if the reason you're trying to end the effect is because there aren't going to be any more callbacks, simply dropping all dependencies doesn't work to dispose of the reference cycle between the effect and its final dependencies.)
The text was updated successfully, but these errors were encountered:
Currently, effects can only be ended from the "outside" - i.e., the caller creating the effect. But there are times when it's advantageous to allow an effect to cancel itself, since it knows when it is "done". (In particular, one can have effects decide when to end based on a shared signal, which is the use case I'm working on right now.)
In principle, you can share the dispose callback from the enclosing scope, and perhaps even make a helper function to simplify that process. But since the effect callback is synchronously invoked, this creates an edge case where you can't dispose the effect before the
effect()
call returns. Basically, you currently need a helper like this:This seems like it would be greatly simplified by making what is now
_dispose
into a public method, so that effect callbacks can just callthis.dispose()
, or even just return a special value to indicate that they wish to be disposed of.(Note: while you can certainly stop an effect from having dependencies eventually, you can't actually get it to release without first receiving another callback, and if the reason you're trying to end the effect is because there aren't going to be any more callbacks, simply dropping all dependencies doesn't work to dispose of the reference cycle between the effect and its final dependencies.)
The text was updated successfully, but these errors were encountered: