Visitar URL original
refactor(core): decouple effects from change detection by alxhub · Pull Request #51049 · angular/angular · GitHub
The Wayback Machine - https://web.archive.org/web/20230721202231/https://github.com/angular/angular/pull/51049
Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alxhub
Copy link
Member

@alxhub alxhub commented Jul 15, 2023

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.

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.
Copy link
Member

@pkozlowski-opensource pkozlowski-opensource left a 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.

packages/core/test/render3/reactivity_spec.ts Show resolved Hide resolved
queue.delete(handle);
this.queuedEffectCount--;

// TODO: what happens if this throws an error?

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.`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@jessicajaniuk jessicajaniuk added the area: core Issues related to the framework runtime label Jul 17, 2023
@ngbot ngbot bot added this to the Backlog milestone Jul 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: core Issues related to the framework runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants