forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): effects wait for ngOnInit for their first run (angular#52473)
When an effect is created in a component constructor, it might read signals which are derived from component inputs. These signals may be unreliable or (in the case of the proposed input signals) may throw if accessed before the component is first change detected (which is what makes required inputs available). Depending on the scenario involved, the effect may or may not run before this initialization takes place, which isn't a great developer experience. In particular, effects created during CD (e.g. via control flow) work fine, as do effects created in bootstrap thanks to the sync CD it performs. When an effect is created through dynamic component creation outside of CD though (such as on router navigations), it runs before the component is first CD'd, causing the issue. In fact, in the signal components RFC we described how effects would wait until ngOnInit for their first execution for exactly this reason, but this behavior was never implemented as it was thought our effect scheduling design made it unnecessary. This is true of the regular execution of effects but the above scenario shows that *creation* of the effect is still vulnerable. Thus, this logic is needed. This commit makes effects sensitive to their creation context, by injecting `ChangeDetectorRef` optionally. An effect created with an injector that's tied to a component will wait until that component is initialized before initially being scheduled. TestBed effect flushing is also adjusted to account for the additional interaction with change detection. PR Close angular#52473
- Loading branch information
Showing
7 changed files
with
231 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters