Commit graph

28 commits

Author SHA1 Message Date
Paul Gschwendtner
866271a1c6 refactor(core): EventEmitter implements OutputRef. (#54650)
An `EventEmitter` is a construct owned by Angular that should be
used for outputs as of right now.

As we are introducing the new `OutputRef` interface for the new output
function APIs, we also think `EventEmitter` should implement
`OutputRef`— ensuring all "known" outputs follow the same contract.

This commit ensures `EventEmitter` implements an `OutputRef`

Note: An output ref captures the destroy ref from the current injection
context for clean-up purposes. This is also done for `EventEmitter` in a
backwards compatible way:

- not requiring an injection context. EventEmitter may be used
  elsewhere.
- not cleaning up subscriptions/completing the emitter when the
  directive/component is destroyed. This would be a change in behavior.

Note 2: The dependency on `DestroyRef` causes it to be retained in all
bundling examples because ironically `NgZone` uses `EventEmitter`- not
for outputs. The code is pretty minimal though, so that should be
acceptable.

`EventEmitter` will now always retain `NgZone. This increases the
payload size slightly around 800b for AIO. Note that the other increases
were coming from previous changes. This commit just pushed it over the
threshold.

PR Close #54650
2024-03-06 12:34:39 +01:00
Paul Gschwendtner
aff65fd1f4 feat(core): introduce outputToObservable interop helper (#54650)
This commit introduces an addition to `output()` and
`outputFromObservable`()` —called `outputToObservable()`.

The helper lives in the RxJS interop package and allows agnostic
programmatic subscriptions to `OutputRef`s by converting the output
to an observable with `.pipe` etc.

The function is ideally used in all places where you subscribe to an
output programmatically. Those outputs in the future, with the new APIs,
may not be actual RxJS constructs, but abstract `OutputRef`'s that
simply expose a `.subscribe` method. The helper allows you to
agnostically convert outputs to RxJS observables that you can safely
interact with.

The observables are also completed automatically, if possible, when the
owning directive/component is destroyed— Something that is not
guaranteed right now.

PR Close #54650
2024-03-06 12:34:38 +01:00
Paul Gschwendtner
c809069f21 feat(core): introduce outputFromObservable() interop function (#54650)
Introduces a second API in addition to the new `output()` function.

The new function `outputFromObservable()` can be used to declare outputs
using the new `OutputRef` API and `output()` API, while using a custom
RxJS observable as data source.

This is something that is currently possible in Angular and we would
like to keep possible- even though we never intended to support custom
observables aside from RxJS-based `EventEmitter`.

The interop bridges the gap and allows you to continue using
`Subject`, `ReplaySubject`, `BehaivorSubjct,` - or cold custom
observables for outputs. You can still trigger logic only when
the output is subscribed- unlike with imperative `emit`s of
`EventEmitter` or the new `OutputEmitterRef`.

A notable difference is that you need two class members where you
previously could access the `Subject` directly. This is an intentional
trade-off we've made to ensure that all new outputs implement the
`OutputRef` interface and we are exposing a minimal API surface to
consumers of components that currently access the output
programmatically.

PR Close #54650
2024-03-06 12:34:38 +01:00
Swami
b3220795d5 refactor(core): remove unused EffectRef import statement (#53863)
Remove unused, leftover imports.

PR Close #53863
2024-02-29 14:54:50 +01:00
Jeremy Elbourn
91f250dab7 build: configure cross-pkg resolution for api extraction (#52499)
This commit adds path mapping and source dependencies necessary to fully
resolve types during api doc extraction.

PR Close #52499
2024-01-05 11:27:34 -08:00
Alex Rickabaugh
7bb3ffb77f fix(core): add rejectErrors option to toSignal (#52474)
By default, `toSignal` transforms an `Observable` into a `Signal`, including
the error channel of the Observable. When an error is received, the signal
begins throwing the error.

`toSignal` is intended to serve the same purpose as the `async` pipe, but
the async pipe has a different behavior with errors: it rejects them
outright, throwing them back into RxJS. Rx then propagates the error into
the browser's uncaught error handling logic. In the case of Angular, the
error is then caught by zone.js and reported via the application's
`ErrorHandler`.

This commit introduces a new option for `toSignal` called `rejectErrors`.
With that flag set, `toSignal` copies the async pipe's behavior, allowing
for easier migrations.

Fixes #51949

PR Close #52474
2023-10-31 14:59:26 -07:00
Jeremy Elbourn
fcc000e803 build: add targets for api doc generation (#52034)
This adds `generate_api_docs` targets to all of the packages for which we publish api reference docs. One known issue here is that any type information that comes from another package (e.g. router depending on core) currently resolve to `any` because the other sources are not available in the program. This can be tackled in a follow-up commit.

This commit also updates the install patch for `@angular/build-tools` to use the local version of compiler-cli.

PR Close #52034
2023-10-10 16:18:50 -07:00
Alex Rickabaugh
5411864c2e fix(core): adjust toSignal types to handle more common cases (#51991)
This commit cleans up the signatures of `toSignal` to better handle the
types of situations that it might be used in, and produce better type
inference results.

Fixes #50687
Fixes #50591

Co-authored-by: Andrew Scott <atscott@google.com>

PR Close #51991
2023-10-10 11:17:30 -07:00
Alex Rickabaugh
8f5cbcc845 refactor: move signals code into primitives package (#51986)
This commit reorganizes the Angular code a bit, and moves signals into a
newly defined `@angular/core/primitives` location. This will be used inside
g3 to allow non-Angular targets to depend on the signals core without
incurring a dependency on the whole framework.

PR Close #51986
2023-10-06 15:12:00 -07:00
Paul Gschwendtner
ced66d4007 revert: fix(core): allow toSignal in reactive contexts (#52049)
Revert (with improvements of): dcf18dc74c

We recently landed a change that allows `toSignal` to be called
from within reactive contexts (e.g. `effect`/`computed`). After
more thorough investigatio and consideration with the team, we
feel like allowing `toSignal` to be called in such contexts is
encouraging non-ideal / hard-to-notice code patterns.

e.g. a new subscription to an observable is made every time `toSignal`
is invoked. There is no caching done here. Additionally, multiple new
subscriptions can trigger unintended side-effects- that may slow down
the app, result in incorrect/unexpected behavior or perform unnecessary
work.

Users should instead move the `toSignal` call outside of the `computed`
or `effect` and then read the signal values from within their `computed`. e.g.

```ts
computed(() => {
  const smth = toSignal(coldObservable$)
  return smth() + 2;
}
```

--> should instead be:

```ts
const smth = toSignal(coldObsverable$);
computed(() => smth() + 2);
```

In cases where a new subscription for each invocation is actually intended, a manual
subscription can be made. That way it's also much more obvious to users
that they are triggering side-effects every time, or causing new
subscriptions.

PR Close #52049
2023-10-05 11:08:05 -07:00
Pawel Kozlowski
dcf18dc74c fix(core): allow toSignal calls in reactive context (#51831)
This PR moves the Observable subscription of toSignal outside of the
reactive context. As the result the toSignal calls are allowed in the
computed, effect and all other reactive consumers.

This is based on the reasoning that we already allow signals creation
in a reactive context. Plus a similar change was done to the async pipe
in the https://github.com/angular/angular/pull/50522

Fixes #51027

PR Close #51831
2023-09-22 09:47:19 -07:00
Paul Gschwendtner
dbffdc09c2 fix(core): avoid duplicated code between entry-points (primary, testing, rxjs-interop) (#51500)
Fixes that there was code duplication between the primary entry-point,
the testing entry-point and the rxjs-interop entry-point.

This code duplication resulted in additional code size (really
neglibible here because rxjs-interop did not duplicate large parts of
core, and `testing` is not used in production).

On the other hand though, the duplication resulted in a subtle JIT
dependency tracking issue due to the `depsTracker` no longer being a
singleton. This caused test failures as in:
https://github.com/angular/angular/pull/51415.

PR Close #51500
2023-08-29 17:55:34 +00:00
Matthieu Riegler
0c9c825b14 docs: Add links to the injection context guide (#51129)
PR Close #51129
2023-07-21 16:46:42 +00:00
Matthieu Riegler
394ca3b49a docs: Improve documentation about Injection context / inject (#49782)
Injection context has gain public visibility with the exposure of `inject`. Lets provide some insights.

Closes  #49774

PR Close #49782
2023-07-10 07:09:31 -07:00
David-Emmanuel DIVERNOIS
1ad4d55d91 fix(core): extend toSignal to accept any Subscribable (#50162)
Extend toSignal to accept any Subscribable (instead of only Observable)
for consistency with AsyncPipe and for broader compatibility with any
observable library (that is compatible with the Subscribable interface).
This is only a type change as the implementation does not use anything
else than the Subscribable interface anyway.

PR Close #50162
2023-06-14 10:49:56 +02:00
Alex Rickabaugh
cc89766cee refactor(core): finalize rxjs-interop options & docs (#50071)
This commit introduces an interface for `toSignal` options to mirror that of
`toObservable`, and adjusts docs for both symbols. It also adds the ability
for `toSignal` to manually specify `DestroyRef` (similarly to
`toObservable` accepting an injector) or for `toSignal` automatic cleanup to
be disabled (in which case the subscription persists until the Observable
completes). Either option allows `toSignal` to be used outside of a DI
context, like `toObservable`.

PR Close #50071
2023-04-28 16:08:47 -07:00
Andrew Scott
02a539cb14 refactor(core): Synchronously emit the current signal value in toObservable (#49894)
As described in
https://github.com/angular/angular/discussions/49681#discussioncomment-5628930,
if an `Observable` created from a signal with `toObservable` is
subscribed to in a template, it will initially have `null` as the value.
Immediately after the template is done executing, effects are flushed
and this results in the `AsyncPipe` getting a new value before the
`checkNoChanges` pass, resulting in `ExpressionChanged` error.

```
template: '{{obs$ | async}}'
...
obs$ = toObservable(signal(0));
```

Instead, this commit updates the `toObservable` to synchronously emit
the initial value to the Observable stream.

Side note here: We don't exactly encourage this pattern. Instead of
using `AsyncPipe`, the template should just read signals.

PR Close #49894
2023-04-25 09:27:38 -07:00
skrtheboss
c029c678d9 fix(core): ensure takeUntilDestroyed unregisters onDestroy listener on unsubscribe (#49901)
The takeUntilDestroyed must always remove the onDestroy listener,
in the teardown logic.

PR Close #49901
2023-04-19 19:55:09 +00:00
Andrew Scott
90166bed25 fix(core): Fix capitalization of toObservableOptions (#49832)
The capitalization of the interface should be pascal case.

PR Close #49832
2023-04-13 19:12:54 +00:00
Andrew Kushnir
7fd0b678f3 refactor(core): avoid deep links into @angular/core (#49823)
This commit updates the code to avoid deep links into the `@angular/core`, which triggers a build issue in apps when a code is referenced.

PR Close #49823
2023-04-13 14:03:27 +00:00
Andrew Scott
1dddb78786 fix(core): toObservable should allow writes to signals in the effect (#49769)
`toObservable` creates an `effect` that watches for updates to the
source signal. We should allow writes to signals in this effect, which
would be consumed by downstream observers.

PR Close #49769
2023-04-12 09:35:55 -07:00
Andrew Scott
53d019ab7d fix(core): catch errors from source signals outside of .next (#49769)
From Ben:

> When dealing with any reactive function call you don't control
> like `observer.next()` (or anything similar), you want to catch the error
> in the producer call, in this case `signal()`. You don't want to catch errors
> in the `observer.next` call itself.

PR Close #49769
2023-04-12 09:35:55 -07:00
Andrew Scott
f1d5896ff3 refactor(core): rename from[Observable/Signal] => to[Signal/Observable] (#49769)
Based on feedback in the RFC, most would prefer `toSignal` and
`toObservable`.

PR Close #49769
2023-04-12 09:35:55 -07:00
Andrew Scott
5c415e9dae refactor(core): Update signal signature with respect to initial values (#49769)
The initial value used for signals by default is now `undefined`. In
addition, there is a new option to express that the signal should emit a
value synchronously (`requireSync: true`). When this value is specified,
the function will throw _on creation_ if the subscribing to the
`Observable` does not result in a synchronous emit.

PR Close #49769
2023-04-12 09:35:55 -07:00
Alex Rickabaugh
9d96487d3c test(core): fix rxjs-interop tests which fail due to collision (#49651)
The change in effect timing collided with the introduction of these
rxjs-interop tests, resulting in them failing after the merge.

This commit adjusts the tests to rely on the real effect timing.

PR Close #49651
2023-03-30 11:06:48 -07:00
Alex Rickabaugh
e883198460 feat(core): implement takeUntilDestroyed in rxjs-interop (#49154)
This commit implements an RxJS operator `takeUntilDestroyed` which
terminates an Observable when the current context (component, directive,
etc) is destroyed. `takeUntilDestroyed` will inject the current `DestroyRef`
if none is provided, or use one provided as an argument.

PR Close #49154
2023-03-30 09:44:38 -07:00
Alex Rickabaugh
8997bdc03b feat(core): prototype implementation of @angular/core/rxjs-interop (#49154)
This commit adds the basic sketch for the implementation of `fromObservable`
and `fromSignal`, the two basic primitives which form the RxJS interop layer
with signals.

PR Close #49154
2023-03-30 09:44:38 -07:00
Alex Rickabaugh
8b7707d920 build(core): introduce @angular/core/rxjs-interop entrypoint (#49154)
This commit adds the infrastructure for `@angular/core/rxjs-interop`, a new
core entrypoint which provides bidirectional interoperability between
Angular's built-in reactivity system of synchronous signals, and the RxJS
`Observable` abstraction.

The new entrypoint is set up as an empty shell in this commit, with its
implementation to follow in a followup.

PR Close #49154
2023-03-30 09:44:38 -07:00