Commit graph

192 commits

Author SHA1 Message Date
Andrew Scott
971981e1df fix(core): TestBed.tick should ensure test components are synchronized (#61382)
This ensures that `TestBed.tick` updates any components created with
`TestBed.createComponent`, regardless of whether autoDetectChanges is
on.

PR Close #61382
2025-05-21 15:02:52 +00:00
Matthieu Riegler
f580318411 docs(docs-infra): Add version of introduction for APIs (#60814)
For new APIs we'll mention since when a particular API is in its current status (experimental, devPreview, stable)

fixes #49668

PR Close #60814
2025-05-02 07:51:33 -07:00
Andrew Scott
e711f99d81 feat(core): move provideExperimentalCheckNoChangesForDebug to provideCheckNoChangesConfig (#60906)
This commit makes several changes changes to the
`provideExperimentalCheckNoChangesForDebug` API:

* Rename it
* Promote to dev preview
* Apply the exhaustive behavior to _all_ checkNoChanges runs
* Remove `useNgZoneOnStable` option. This wasn't found to be generally
  more useful than `interval`

fixes #45612

BREAKING CHANGE: `provideExperimentalCheckNoChangesForDebug` has several
breaking changes:
* It is renamed to `provideCheckNoChangesConfig`
* The behavior applies to _all_ checkNoChanges runs
* The `useNgZoneOnStable` option is removed. This wasn't found to be generally
  more useful than `interval`

PR Close #60906
2025-04-24 13:04:27 -07:00
Andrew Scott
953c4b2580 feat(core): Move zoneless change detection to dev preview (#60748)
This commit moves zoneless from experimental to developer preview.

* Update tag on provider API
* Remove "experimental" from provider name
* Move documentation from "experimental features" to "Best practives ->
  Performance" (at least temporarily until there is a better place)

BREAKING CHANGE: `provideExperimentalZonelessChangeDetection` is
renamed to `provideZonelessChangeDetection` as it is now "Developer
Preview" rather than "Experimental".

PR Close #60748
2025-04-23 11:47:56 +02:00
Matthieu Riegler
1c1ad12ad3 refactor(core): Instantiate the ErrorHandler lazily. (#58984)
This change prevents that user code in a custom `ErrorHandler` to run as part of `ApplicationRef`'s creation.

fixes #58970

PR Close #58984
2025-04-02 19:20:50 +00:00
Vlad Boisa
befa0faaef docs: change the broked link (#60436)
Change the wrong path of the link to a working one.
PR Close #60436
2025-03-18 18:09:31 +01:00
Kristiyan Kostadinov
8be6e3888b refactor(core): replace internal usages of InjectFlags (#60318)
Replaces all the places where we use `InjectFlags` internally with the `InternalInjectFlags` which has the benefit of being a `const` enum and allowing us to consolidate the flags and object literal eventually.

PR Close #60318
2025-03-11 11:33:09 -07:00
Kristiyan Kostadinov
611baaf069 feat(core): remove InjectFlags from public API (#60318)
Removes the deprecated `InjectFlags` symbol from the `@angular/core` public API, as well as all the places that accept it. The previous commit includes an automated migration to switch over to the new way of passing in flags.

BREAKING CHANGE:
* `InjectFlags` has been removed.
* `inject` no longer accepts `InjectFlags`.
* `Injector.get` no longer accepts `InjectFlags`.
* `EnvironmentInjector.get` no longer accepts `InjectFlags`.
* `TestBed.get` no longer accepts `InjectFlags`.
* `TestBed.inject` no longer accepts `InjectFlags`.

PR Close #60318
2025-03-11 11:33:09 -07:00
Andrew Scott
338818ce89 fix(core): Surface errors from ApplicationRef.tick to callsite (#60102)
This commit ensures that errors during `ApplicationRef.tick` are
surfaced to the callsite rather than being caught and reported to the
`ErrorHandler`.

The current catch and report approach was originally
added in e263e19a2a
with the goal of preventing automatic change detection crashes due to
the error happening in the subscription. However, this results in hiding
a public API that can hide errors. Callers cannot assume that the tick
was successful and perform follow-up work.

This change now surfaces errors and adds the error handling directly to
the callsites.

BREAKING CHANGE: `ApplicationRef.tick` will no longer catch and report
errors to the appplication `ErrorHandler`. Errors will instead be thrown out of
the method and will allow callers to determine how to handle these
errors, such as aborting follow-up work or reporting the error and
continuing.

PR Close #60102
2025-03-10 08:17:14 -07:00
Samuel Perez
6dd8cce155 docs: fix all brokens links on the API pages (#59162)
This PR fix is for the broken links on the API
pages mentioned in issue #57591.

PR Close #59162
2025-03-04 20:07:23 +00:00
Andrew Scott
7232ce5b17 fix(core): Catch and report rejections in async function of PendingTasks.run (#60044)
This commit ensures that rejections of the promise of the async function
passed to `PendingTasks.run` are not dangling and get reported to the
application error handler. This prevents what would likely be a common
dangling promise that could end up crashing the node process.

BREAKING CHANGE: `PendingTasks.run` no longer returns the result of the
async function. If this behavior is desired, it can be re-implemented
manually with the `PendingTasks.add`. Be aware, however, that promise rejections
will need to be handled or they can cause the node process to shut down
when using SSR.

PR Close #60044
2025-03-03 17:13:33 +00:00
Andrew Scott
491b0a4ead fix(core): Remove duplicate reporting of errors in CDR.detectChanges (#60056)
This change removes the reporting of errors from the
`ChangeDetectorRef.detectChanges` API. The reporting results in the
error being "handled" in two ways, both by reporting to error handler
and rethrowing the error. This rethrown error generally ends up being
caught further up and again reported to the error handler. The error
handler is meant to be for uncaught errors, and since Angular is not at
the top of the stack of the call of `CDR.detectChanges`, it does not
know what is being done with the rethrown error.

Note that for zone-based applications, this will likely have no effect
other than removing duplicate reporting of the error. If the rethrown
error is not already being caught, it will reach the NgZone's error
trap and still be reported to the application `ErrorHandler`.

PR Close #60056
2025-02-25 12:20:22 -05:00
Miles Malerba
6789c7ef94 fix(core): Defer afterRender until after first CD (#59455) (#59551)
This reverts commit ac2dbe3eb1.

PR Close #59551
2025-02-12 12:20:04 -08:00
hawkgs
b9155b5121 docs: set syntax highlighting to the remaining Markdown code examples blocks (#59088)
There are some code blocks that slipped through the initial Regex-es.

Related to #59026

PR Close #59088
2025-01-14 15:14:02 -05:00
Andrew Kushnir
ac2dbe3eb1 Revert "fix(core): Defer afterRender until after first CD (#58250)" (#59455)
This reverts commit 9870b643bf.

PR Close #59455
2025-01-09 14:30:01 -05:00
Miles Malerba
9870b643bf fix(core): Defer afterRender until after first CD (#58250)
For `afterRender`/`afterNextRender` calls associated with a particular
view, ensure that they are not registered until after the first time the
view is rendered.

Co-authored-by: Alex Rickabaugh <alxhub@users.noreply.github.com>

PR Close #58250
2025-01-08 15:33:38 +00:00
Alex Rickabaugh
039bcad76a refactor(core): remove circular dep in iterable differs. (#59083)
Use `import type` to break a phantom circular import in iterable differs.

PR Close #59083
2024-12-05 16:01:14 -08:00
hawkgs
b8b6c8c77a refactor(docs-infra): convert code-example-s that have only region param to @example-s (#59004)
Replace all <code-example>-s within TS files that contain only a path and a region with JSDoc @example-s.

PR Close #59004
2024-12-04 18:05:59 +01:00
hawkgs
0513fbc9fc docs: set syntax highlighting of code examples MD code blocks (#59026)
Set the syntax highlighting based on the code examples' language.

PR Close #59026
2024-12-04 17:30:28 +01:00
Timon
1b6204dd2a docs(core): fix eventCoalescing comment (#57097)
PR Close #57097
2024-12-02 11:34:26 +01:00
Kristiyan Kostadinov
7eeae9f170 refactor(core): adjust tracing service (#58771)
Adjusts the tracing service based on internal requirements.

PR Close #58771
2024-11-21 16:34:53 +00:00
Alex Rickabaugh
d27f97ddab refactor(core): introduce TracingService for snapshot-based tracing (#58771)
This commit introduces a private API, the `TracingService` DI token. By
providing this token, Angular can be configured to capture tracing snapshots
for certain operations such as change detection notifications, and to run
downstream operations within the context of those snapshots.
`TracingService` abstracts this context propagation and makes it pluggable.

PR Close #58771
2024-11-21 16:34:53 +00:00
Alan Agius
fee9db18a9 refactor: add @__PURE__ next to @pureOrBreakMyCode for improved bundler compatibility (#58297)
Added the `@__PURE__` annotation alongside `@pureOrBreakMyCode` to improve compatibility with third-party bundlers. This refactor follows optimization best practices, ensuring broader support across different tools, as `@pureOrBreakMyCode` was only supported by Closure Compiler.

PR Close #58297
2024-10-28 12:26:05 -07:00
Andrew Scott
8ebbae88ca feat(core): Add rxjs operator prevent app stability until an event (#56533)
This commit adds an operator to help rxjs observables important for rendering
keep the application unstable (and prevent serialization) until there is
an event (observable emits, completes, or errors, or the subscription is
unsubscribed). This helps with SSR for zoneless and also helps for when
operations are intentionally executed outside the Angular Zone but are
still important for SSR (i.e. angularfire and the zoneWrap helper hacks).

PR Close #56533
2024-10-22 14:01:11 -07:00
Alex Rickabaugh
7f0b49d8c4 refactor(core): update effect error handling (#57952)
Previously, effect() would handle errors differently depending on the effect
type. Root effects had a try/catch that would execute them independently and
report errors to `ErrorHandler`, while component effects would "crash" CD.

This commit switches all effects to use the same error handling (errors
always reach the CD error handler).

An additional unrelated refactoring is thrown in which removes the
`pendingTask` machinery from root effects, since they make `ApplicationRef`
dirty and thus trigger the scheduler.

PR Close #57952
2024-10-15 13:02:10 -07:00
Alan Agius
c15ec36bd1 refactor(core): remove deprecated factories Property in KeyValueDiffers (#58064)
BREAKING CHANGE: The deprecated `factories` property in `KeyValueDiffers` has been removed.

PR Close #58064
2024-10-04 13:28:49 +00:00
Joey Perrott
9dbe6fc18b refactor: update license text to point to angular.dev (#57901)
Update license text to point to angular.dev instead of angular.io

PR Close #57901
2024-09-24 15:33:00 +02:00
Andrew Scott
a1f229850a feat(core): migrate ExperimentalPendingTasks to PendingTasks (#57533)
This commit promotes the `ExperimentalPendingTasks` service from
experimental to developer preview and includes a migration schematic for
the rename.

BREAKING CHANGE: `ExperimentalPendingTasks` has been renamed to
`PendingTasks`.

PR Close #57533
2024-09-20 18:26:48 +02:00
Alex Rickabaugh
4e890cc5ac refactor(core): add support for new effect scheduling. (#56501)
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.

The new effect timing is not yet enabled - a future PR will flip the flag.

PR Close #56501
2024-09-18 14:52:25 -07:00
Andrew Scott
36d8d19dc1 refactor(core): removing a pending task delays stability until the next tick (#57570)
This commit updates the public API for pending tasks to schedule an
application tick, effectively making the stability async when the last
task is removed.

PR Close #57570
2024-09-03 08:50:42 -07:00
Andrew Scott
226a67dabb fix(core): Schedulers run in zone above Angular rather than root (#57553)
This change updates the timers used in the coalescing and hybrid mode
schedulers to run in the zone above Angular rather than the root zone.
Running the timers in the root zone makes them impossible to flush when
using `fakeAsync` and also may make them invisible to other zones in the
hierarchy that might have desirable behaviors such as task/perf tracking.

fixes #56767

BREAKING CHANGE: The timers that are used for zone coalescing and hybrid
mode scheduling (which schedules an application state synchronization
when changes happen outside the Angular zone) will now run in the zone
above Angular rather than the root zone. This will mostly affect tests
which use `fakeAsync`: these timers will now be visible to `fakeAsync`
and can be affected by `tick` or `flush`.

PR Close #57553
2024-08-27 15:07:30 -07:00
Alex Rickabaugh
0cebfd7462 fix(elements): switch to ComponentRef.setInput & remove custom scheduler (#56728)
The custom element implementation previously used a custom code path for
setting inputs, which contained bespoke code for writing input properties,
detecting whether inputs actually change, marking the component dirty,
scheduling and running CD, invoking `ngOnChanges`, etc. This custom logic
had several downsides:

 * Its behavior different from how Angular components behave in a normal
   template.

   For example, inputs setters were invoked in `NgZone.run`, which (when
   called from outside the zone) would trigger synchronous CD in the
   component, _without_ calling `ngOnChanges`. Only when the custom rAF-
   scheduled `detectChanges()` call triggered would `ngOnChanges` be called.

 * CD always ran multiple times, because of the above. `NgZone.run` would
   trigger CD, and then separately the scheduler would trigger CD.

 * Signal inputs were not supported, since inputs were set via direct
   property writes.

This change refactors the custom element implementation with two changes:

1. `ComponentRef.setInput` is used instead of a custom code path for
   writing inputs.

This allows us to drop all the custom logic related to managing
`ngOnChanges`, since `setInput` does that under the hood. `ngOnChanges`
behavior now matches how the component would behave when _not_ rendered
as a custom element.

2. The custom rAF-based CD scheduler is removed in favor of the main Angular
   scheduler, which now handles custom elements as necessary.

Running `NgZone.run` is sufficient to trigger CD when zones are used, and
the hybrid zoneless scheduler now ensures CD is scheduled when `setInput` is
called even with no ZoneJS enabled. As a result, the dedicated elements
scheduler is now only used when Angular's built-in scheduler is disabled.

As a concession to backwards compatibility, the element's view is also
marked for refresh when an input changes. Doing this allows CD to revisit
the element even if it becomes dirty during CD, mimicking how it would be
detected by the former elements scheduler unconditionally refreshing the
view a second time.

As a part of this change, the elements tests have been significantly
refactored. Previously all of Angular was faked/spied, which had a number
of downsides. For example, there were tests which asserted that change
detection only happens once when setting multiple inputs. This wasn't
actually the case (because of `NgZone.run` - see logic above) but the test
didn't catch the issue because it was only spying on `detectChanges()` which
isn't called from `ApplicationRef.tick()`. Even the components were fake.

Now, the tests use real Angular components and factories. They've also been
updated to not use `fakeAsync`.

A number of tests have been disabled, which were previously asserting
behavior that wasn't matching what was actually happening (as above). Other
tests were disabled due to real differences with `ngOnChanges` behavior,
where the current behavior could be seen as a bug.

Fixes #53981

BREAKING CHANGE: as part of switching away from custom CD behavior to the
hybrid scheduler, timing of change detection around custom elements has
changed subtly. These changes make elements more efficient, but can cause
tests which encoded assumptions about how or when elements would be checked
to require updating.

PR Close #56728
2024-08-23 13:56:11 -07:00
Alex Rickabaugh
b80af11bbf refactor(core): restructure AfterRenderManager to connect related phases (#57453)
The `afterRender` infrastructure was first implemented around the idea of
independent, singular hooks. It was later updated to support a spec of
multiple hooks that pass values from one to another as they execute, but the
implementation still worked in terms of singular hooks under the hood. This
creates a number of maintenance issues, and a few bugs. For example, when
one hook fails, further hooks in the pipeline should no longer execute, but
this was hard to ensure under the old design.

This refactoring restructures `afterRender` infrastructure significantly to
introduce the concept of a "sequence", a collection of hooks of different
phases that execute together. Overall, the implementation is simplified
while making it more resilient to issues and future use cases, such as the
upcoming `afterRenderEffect`.

As part of this refactoring, the `internalAfterNextRender` concept is
removed, as well as the unused `queueStateUpdate` concept which used it.

PR Close #57453
2024-08-23 10:15:19 -07:00
Alex Rickabaugh
dabfb6de3d refactor(core): track dirtiness bits in ApplicationRef (#57453)
Previously the zoneless scheduler had a concept of whether views needed to
be refreshed or not, based on the notification type that was received. It
tracked this information as a boolean.

This commit refactors things to track dirtiness in `ApplicationRef` itself,
as a `dirtyFlags` field with bits corresponding to either view tree
dirtiness or after-render hooks.

PR Close #57453
2024-08-23 10:15:19 -07:00
Andrew Scott
3b0dca75d6 fix(core): Allow zoneless scheduler to run inside fakeAsync (#56932)
The zoneless scheduler callback was executed in the root zone rather
than simply in `runOutsideAngular` to allow us to land the hybrid mode
change detection (scheduler always enabled, even for zones) without
breaking a ton of existing `fakeAsync` tests that could/would fail with
the "timer(s) still in queue" error. However, this caused another
problem: when a test executes inside `fakeAsync`, it cannot flush the
scheduled time. A similar problem exists with event and run coalescing (#56767).
This change would allow `fakeAsync` to flush the zoneless-scheduled
change detections and minimize breaking existing tests
by flushing pending timers at the end of the test, which actually now
matches what's done internally.

PR Close #56932
2024-08-15 12:32:24 -04:00
Andrew Scott
769b6e1973 fix(core): Allow hybrid CD scheduling to support multiple "Angular zones" (#57267)
This commit updates the inside/outside NgZone detection of the hybrid CD
scheduling to track the actual instance of the NgZone being used rather
than the name "Angular" (how `isInsideAngularZone` works). This allows
the scheduling to work correctly when there are multiple versions of
Angular running on the page.

fixes #57261

PR Close #57267
2024-08-08 10:46:26 -07:00
Andrew Scott
3a63c9ebbe fix(core): errors during ApplicationRef.tick should be rethrown for zoneless tests (#56993)
The behavior of `ComponentFixture` for zoneless tests was decided somewhat through guesswork, trial, and error. In addition, working on the zoneless fixture revealed oddities in the behavior of the zone-based fixture, or behaviors that we felt were counterintuitive. The most consequential difference is how change detection works: `detectChanges` goes through ApplicationRef.tick in zoneless while it is `changeDetectorRef.detectChanges` in the zone fixture.

We felt that running change detection through `ApplicationRef.tick` was important for several reasons:
* Aligning application behavior more closely with the test behavior (almost all views are attached to application ref in reality)
* Ensuring that afterRender* hooks are executed when calling `fixture.detectChanges`
* Ensuring that the change detection runs again if render hooks update state

This change, however, has some noticeable consequences that will break some tests, mostly around how errors are handled. `ApplicationRef.tick` catches errors that happen during change detection and reports them to the ErrorHandler from DI. The default error handler only logs the error to the console. This will break tests which have `expect(() => fixture.detectChanges()).toThrow()`. In addition, it allows tests to pass when there are real errors encountered during change detection.

This change ensures that errors from `ApplicationRef.tick` are rethrown
and will fail the test. We should also do a follow-up investigation to
determine whether we can/should also do this for the zone-based
`ComponentFixture`.

fixes #56977

PR Close #56993
2024-07-29 13:49:00 -07:00
Andrew Scott
8718abce90 fix(core): Deprecate ignoreChangesOutsideZone option (#57029)
This option was introduced out of caution as a way for developers to opt out of the new behavior in v18 which schedule change detection for the above events when they occur outside the Zone. After monitoring the results post-release, we have determined that this feature is working as desired and do not believe it should ever be disabled by setting this option to `true`.

PR Close #57029
2024-07-23 10:10:48 -07:00
Matthieu Riegler
e618521a57 docs: replace @link with absolute link. (#56557)
Currently `@link` doesn't work for cross-package references.

Fixes #56556

PR Close #56557
2024-06-27 15:54:47 +00:00
Kristiyan Kostadinov
bbe39c1738 refactor(core): integrate let instructions into the runtime (#56527)
Adds the implementation of the following new instructions:
* `declareLet` - creation-time instruction that initializes the slot for a let declaration.
* `storeLet` - update-time instruction that stores the current value of a let declaration.
* `readContextLet` - instruction that reads the stored value of a let declaration from a different view.

On top of the instructions, it also introduces a new `LetDeclaration` TNode type.
The new TNode is nececessary for DI to work correctly in pipes inside the let expression,
as well as for proper hydration support.

PR Close #56527
2024-06-26 08:48:31 -07:00
arturovt
39df5fa9d2 refactor(core): missing space in zoneless warning (#56491)
There's currently a missing space in the zoneless warning, showing words together.

PR Close #56491
2024-06-20 08:49:39 -07:00
Andrew Scott
80f472f9f4 refactor(core): provide zone token in prod as well (#56197)
This allows us to make decisions based on whether zone is provided for
when zoneless is the default.

PR Close #56197
2024-05-30 19:24:23 -07:00
Andrew Scott
3b4b05d2f7 refactor(core): Remove hybrid mode flag and move scheduler provider location (#55722)
The flag is not used anymore and, as a result, is easier to move the
scheduler provider.

PR Close #55722
2024-05-23 18:15:53 +02:00
cexbrayat
6024d07559 fix(core): typo in zoneless warning (#55974)
The warning introduced in ae0baa25 mispelled `applications`.

PR Close #55974
2024-05-22 15:22:19 -07:00
Kristiyan Kostadinov
ae0baa2522 fix(core): add warning when using zoneless but zone.js is still loaded (#55769)
Users may be using zoneless, but are still loading Zone.js in which case they won't get the full benefits like reduced bundle size. These changes detect such a case and log a warning.

PR Close #55769
2024-05-20 23:37:12 -07:00
cexbrayat
3d5c3d9fff fix(core): error about provideExperimentalCheckNoChangesForDebug uses wrong name (#55824)
The error about `provideExperimentalCheckNoChangesForDebug` mentions `provideCheckNoChangesForDebug` instead.

PR Close #55824
2024-05-16 09:02:28 -07:00
Andrew Scott
84b2351d50 refactor(core): Update error for both zone and zoneless to be only for apps (#55813)
Developers may want to enable zoneless for all tests by default by
adding the zoneless provider to `initTestEnvironment` and then
temporarily disabling it for individual tests with the zone provider
until they can be made zoneless compatible.

PR Close #55813
2024-05-15 13:27:26 -07:00
Andrew Scott
ca6cdcd269 refactor(core): feature for potential zoneless-compatibility debug check (#55663)
This commit adds a feature that is useful for determining if an
application is zoneless-ready. The way this works is generally only
useful right now when zoneless is enabled. Some version of this may be useful in
the future as a general configuration option to change detection to make
`checkNoChanges` pass always exhaustive as an opt-in to address #45612.

Because this is an experimental, debug-only feature, it is okay to merge
during the RC period.

PR Close #55663
2024-05-07 13:39:13 -07:00
Andrew Scott
abbaf8f639 refactor(core): Throw a runtime error if both zone and zoneless are provided (#55410)
This commit adds a dev-mode error if both the zone and zoneless
providers are used together.

PR Close #55410
2024-05-07 13:37:42 -07:00
Andrew Scott
76e7aa8add refactor(core): Update coalescing to just use patched timers in root zone (#55366)
Rather than attempting to use the native timing functions, this commit
simplifies the logic significantly by using the global timer functions
as they are, either patched or unpatched. When Zone is defined, we run
the timers in the root zone. This has more predictable behavior and
timing than (a) using both patched and unpatched versions of timers in
different places (b) trying to get an unpatched timer and failing due to
environment specifics and patches that aren't ZoneJS.

PR Close #55366
2024-05-02 14:43:10 -07:00