Visitar URL original
Template of fixture is not updated using signals (in test) · Issue #56863 · angular/angular · GitHub
The Wayback Machine - https://web.archive.org/web/20241130002411/https://github.com/angular/angular/issues/56863
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

Template of fixture is not updated using signals (in test) #56863

Closed
Undestor opened this issue Jul 5, 2024 · 3 comments
Closed

Template of fixture is not updated using signals (in test) #56863

Undestor opened this issue Jul 5, 2024 · 3 comments
Labels
area: core Issues related to the framework runtime area: testing Issues related to Angular testing features, such as TestBed bug core: reactivity Work related to fine-grained reactivity in the core framework cross-cutting: signals P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Milestone

Comments

@Undestor
Copy link

Undestor commented Jul 5, 2024

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

Yes

Description

In the following test scenario:

describe('Test with input', () => {
	@Component({
		standalone: true,
		template: `{{ htmlData }}`,
		changeDetection: ChangeDetectionStrategy.OnPush,
	})
	class TestComponentWithInput {
		data = input<string>()
		htmlData?: string
		constructor() {
			effect(() => {
				this.htmlData = this.data()
			})
		}
	}
	it('#1 should have "foobar" in template', () => {
		const fixture = TestBed.configureTestingModule({
			imports: [TestComponentWithInput],
		}).createComponent(TestComponentWithInput)
		fixture.detectChanges() // <---- This is necessary
		fixture.componentRef.setInput('data', 'foobar')
		fixture.detectChanges()

		expect(fixture.debugElement.nativeElement.textContent).toContain('foobar')
	})

	it('#2 should have "foobar" in template', () => {
		const fixture = TestBed.configureTestingModule({
			imports: [TestComponentWithInput],
		}).createComponent(TestComponentWithInput)
		//fixture.detectChanges() // <---- Template is not updated because of missing detectChanges
		fixture.componentRef.setInput('data', 'foobar')
		fixture.detectChanges()

		expect(fixture.debugElement.nativeElement.textContent).toContain('foobar')
	})
})

Only test no. 1 does work and test no. 2 will fail. Apparently the fixture.detectChanges() before setting the input is needed. Otherwise the template does not get updated (See test no. 2).
Is this the correct behaviour or is this a bug?
If this is the expected behaviour, how can i test a required signal input, like in the following scenario:

describe('Test with required input', () => {
	@Component({
		standalone: true,
		template: `{{ htmlData }}`,
		changeDetection: ChangeDetectionStrategy.OnPush,
	})
	class TestComponentWithRequiredInput {
		data = input.required<string>()
		htmlData?: string
		constructor() {
			effect(() => {
				this.htmlData = this.data()
			})
		}
	}
	it('#1 should have "foobar" in template', () => {
		const fixture = TestBed.configureTestingModule({
			imports: [TestComponentWithRequiredInput],
		}).createComponent(TestComponentWithRequiredInput)
		fixture.detectChanges() // <---- This is necessary but throws error
		fixture.componentRef.setInput('data', 'foobar')
		fixture.detectChanges()

		expect(fixture.debugElement.nativeElement.textContent).toContain('foobar')
	})

	it('#2 should have "foobar" in template (but i am broken)', () => {
		const fixture = TestBed.configureTestingModule({
			imports: [TestComponentWithRequiredInput],
		}).createComponent(TestComponentWithRequiredInput)
		//fixture.detectChanges() // <---- Template is not updated because of missing detectChanges
		fixture.componentRef.setInput('data', 'foobar')
		fixture.detectChanges()

		expect(fixture.debugElement.nativeElement.textContent).toContain('foobar')
	})
})

In this scenario both of the tests will fail. The test no. 1 fails (and throws an the error NG0950: Input is required but no value is available yet) because i need to set the input before using fixture.detectChanges() and the test no. 2 does fail because i have not used fixture.detectChanges before setting the input and therefore the template is not updated.\

I hope i was able to describe my dilemma :)

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 18.0.4
Node: 20.12.2
Package Manager: npm 10.3.0
OS: darwin arm64

Angular: 18.0.3
... animations, cdk, common, compiler, compiler-cli, core, forms
... language-service, material, platform-browser
... platform-browser-dynamic, platform-server, router
... service-worker

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1703.8
@angular-devkit/build-angular   18.0.4
@angular-devkit/core            18.0.4
@angular-devkit/schematics      18.0.4
@angular/cli                    18.0.4
@angular/ssr                    18.0.4
@schematics/angular             18.0.4
ng-packagr                      18.0.0
rxjs                            7.8.1
typescript                      5.4.4
webpack                         5.92.0
zone.js                         0.14.2

Anything else?

No response

@JeanMeche
Copy link
Member

JeanMeche commented Jul 5, 2024 •

Your unit tests actually highlights an issue around the effect scheduling that the team is looking to address. (see #56501)
Effect should probably run ahead of the templates being updated.

Currently effects in components run after change detecton updates the template.
This is why it needs 2 call to detectChanges.

Here is a repro: https://stackblitz.com/edit/ng-jasmine-tghqjg?file=src%2Fapp%2Fapp.component.spec.ts

@Undestor
Copy link
Author

Undestor commented Jul 5, 2024

Thanks for the clarification !

@pkozlowski-opensource pkozlowski-opensource added area: testing Issues related to Angular testing features, such as TestBed core: reactivity Work related to fine-grained reactivity in the core framework cross-cutting: signals labels Jul 5, 2024
@ngbot ngbot bot modified the milestone: needsTriage Jul 5, 2024
@pkozlowski-opensource pkozlowski-opensource added the area: core Issues related to the framework runtime label Jul 5, 2024
alxhub added a commit to alxhub/angular that referenced this issue Jul 9, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Jul 9, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Jul 12, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Jul 17, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Jul 17, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Jul 17, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
@pkozlowski-opensource pkozlowski-opensource added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent bug labels Jul 24, 2024
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Jul 24, 2024
alxhub added a commit to alxhub/angular that referenced this issue Jul 29, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Aug 7, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Aug 19, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Aug 20, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Aug 23, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Aug 23, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Aug 28, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Aug 29, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Sep 3, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Sep 3, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Sep 4, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Sep 9, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Sep 16, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Sep 17, 2024
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Sep 18, 2024
This commit flips the flag that was added in 4e890cc, putting the new effect
timing into... effect :)

BREAKING CHANGE:

Generally this PR has two implications:

* effects which are triggered outside of change detection run _later_ (as a
  macrotask instead of a microtask).

* effects which are triggered during change detection (e.g. by input
  signals) run _earlier_, before the component's template.

We've seen a few common failure cases:

* Tests which used to rely on the `Promise` timing of effects now need to
  `await whenStable()` or call `.detectChanges()` in order for effects to
  run.

* Tests which use faked clocks may need to fast-forward/flush the clock to
  cause effects to run.

* `effect()`s triggered during CD could rely on the application being fully
  rendered (for example, they could easily read computed styles, etc). With
  the change, they run before the component's updates and can get incorrect
  answers. The recent `afterRenderEffect()` API is a natural replacement for
  this style of effect.

* `effect()`s which synchronize with the forms system are particularly
  timing-sensitive and might need to adjust their initialization timing.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
alxhub added a commit to alxhub/angular that referenced this issue Sep 19, 2024
This commit flips the flag that was added in 4e890cc, putting the new effect
timing into... effect :)

BREAKING CHANGE:

Generally this PR has two implications:

* effects which are triggered outside of change detection run as part of
  the change detection process instead of as a microtask. Depending on the
  specifics of application/test setup, this can result in them executing
  earlier or later (or requiring additional test steps to trigger; see below
  examples).

* effects which are triggered during change detection (e.g. by input
  signals) run _earlier_, before the component's template.

We've seen a few common failure cases:

* Tests which used to rely on the `Promise` timing of effects now need to
  `await whenStable()` or call `.detectChanges()` in order for effects to
  run.

* Tests which use faked clocks may need to fast-forward/flush the clock to
  cause effects to run.

* `effect()`s triggered during CD could rely on the application being fully
  rendered (for example, they could easily read computed styles, etc). With
  the change, they run before the component's updates and can get incorrect
  answers. The recent `afterRenderEffect()` API is a natural replacement for
  this style of effect.

* `effect()`s which synchronize with the forms system are particularly
  timing-sensitive and might need to adjust their initialization timing.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863
and-oli pushed a commit to and-oli/angular that referenced this issue Sep 30, 2024
…es` (angular#57874)

This commit flips the flag that was added in 4e890cc, putting the new effect
timing into... effect :)

BREAKING CHANGE:

Generally this PR has two implications:

* effects which are triggered outside of change detection run as part of
  the change detection process instead of as a microtask. Depending on the
  specifics of application/test setup, this can result in them executing
  earlier or later (or requiring additional test steps to trigger; see below
  examples).

* effects which are triggered during change detection (e.g. by input
  signals) run _earlier_, before the component's template.

We've seen a few common failure cases:

* Tests which used to rely on the `Promise` timing of effects now need to
  `await whenStable()` or call `.detectChanges()` in order for effects to
  run.

* Tests which use faked clocks may need to fast-forward/flush the clock to
  cause effects to run.

* `effect()`s triggered during CD could rely on the application being fully
  rendered (for example, they could easily read computed styles, etc). With
  the change, they run before the component's updates and can get incorrect
  answers. The recent `afterRenderEffect()` API is a natural replacement for
  this style of effect.

* `effect()`s which synchronize with the forms system are particularly
  timing-sensitive and might need to adjust their initialization timing.

Fixes angular#55311
Fixes angular#55808
Fixes angular#55644
Fixes angular#56863

PR Close angular#57874
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Oct 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: core Issues related to the framework runtime area: testing Issues related to Angular testing features, such as TestBed bug core: reactivity Work related to fine-grained reactivity in the core framework cross-cutting: signals P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants