New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(core): decouple effects from change detection #51049
base: main
Are you sure you want to change the base?
Conversation
Previously effects were queued as they became dirty, and this queue was flushed at various checkpoints during the change detection cycle. The result was that change detection _was_ the effect runner, and without executing CD, effects would not execute. This leads a particular tradeoff: * effects are subject to unidirectional data flow (bad for dx) * effects don't cause a new round of CD (good/bad depending on use case) * effects can be used to implement control flow efficiently (desirable) This commit changes the scheduling mechanism. Effects are now scheduled via the microtask queue. This changes the tradeoffs: * effects are no longer limited by unidirectional data flow (easy dx) * effects registered in the Angular zone will trigger CD after they run (same as `Promise.resolve` really) * the public `effect()` type of effect probably isn't a good building block for our built-in control flow, and we'll need a new internal abstraction. As `effect()` is in developer preview, changing the execution timing is not considered breaking even though it may impact current users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent start @alxhub !
I've left some comments as discussion points, let's catch up on those.
| queue.delete(handle); | ||
| this.queuedEffectCount--; | ||
|
|
||
| // TODO: what happens if this throws an error? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent question! I guess it is even broader that this PR and is a more generic question around exception handling in effects. We did answer this question for computed but probably should make a call for effects as well.
Regardless, my first reaction is that one "bad apple" probably should not "break" the whole queue / app. But then, if we catch an error here, what do we do with it? Needs more thinking.
| return; | ||
| } | ||
| if (ngDevMode && isInNotificationPhase()) { | ||
| throw new Error(`Schedulers cannot synchronously execute effects while scheduling.`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.


Previously effects were queued as they became dirty, and this queue was flushed at various checkpoints during the change detection cycle. The result was that change detection was the effect runner, and without executing CD, effects would not execute. This leads a particular tradeoff:
This commit changes the scheduling mechanism. Effects are now scheduled via the microtask queue. This changes the tradeoffs:
Promise.resolvereally)effect()type of effect probably isn't a good building block for our built-in control flow, and we'll need a new internal abstraction.As
effect()is in developer preview, changing the execution timing is not considered breaking even though it may impact current users.