Signals In Angular — Is RxJS Doomed?
Tags: angular
Published: 2023-03-01 12:37:00

The Angular team is finally releasing the first comments and prototypes regarding bringing Signals, a new reactive primitive, to the core framework.

PR: [feat(core): add Angular Signals to the public API](https://github.com/angular/angular/pull/49150)

What You Should Know: TL;DR:
- To some extent, Signals is powerful like RxJS but with a simpler syntax.
- If implemented, it might change the way change detection work. Possibly, removing zone.js.
- Signals are not going to replace RxJS any time soon.

## What Are Signals?

> A Signal is a source of value
> Alex Rickabaugh

Signals have been popularized recently by Solid.js which [describes them](https://www.solidjs.com/tutorial/introduction_signals) as follows:

Signals are the cornerstone of reactivity in Solid. They contain values that change over time; when you change a signal’s value, it automatically updates anything that uses it.

If you are familiar with RxJS, this might remind you of BehaviorSubject or, more generally, multicasted values. However, unlike [BehaviorSubject](https://rxjs.dev/api/index/class/BehaviorSubject), Signals would not require the Observers to subscribe to be notified of changes.

With Signals, Subscriptions get created and destroyed automatically under the hood. More or less what happens using the async pipe in RxJS. However, unlike Observables, Signals don’t require a Subscription to be used outside the template.

{% embed https://twitter.com/brandontroberts/status/1628754855857737728?ref_src=twsrc%5Etfw%7Ctwcamp%5Etweetembed%7Ctwterm%5E1628754855857737728%7Ctwgr%5Ec8361a005f7f720c5f55274e2c5a5c2db5182209%7Ctwcon%5Es1_&ref_url=https%3A%2F%2Fcdn.embedly.com%2Fwidgets%2Fmedia.html%3Ftype%3Dtext2Fhtmlkey%3Da19fcc184b9711e1b4764040d3dc5c07schema%3Dtwitterurl%3Dhttps3A%2F%2Ftwitter.com%2Fbrandontroberts%2Fstatus%2F1628754855857737728image%3Dhttps3A%2F%2Fi.embed.ly%2F1%2Fimage3Furl3Dhttps253A252F252Fabs.twimg.com252Ferrors252Flogo46x38.png26key3Da19fcc184b9711e1b4764040d3dc5c07 %}

Signals go to great lengths to hide some of the complexities of RxJS.

As said above, Signals contain values that change over time. So, let’s have a look at that.

## Signals In Angular — Example
Here you can find a clean example of how Signals work ([Signals Example](https://levelup.gitconnected.com/signals-in-angular-is-rxjs-doomed-5b5dac574306#d5c8)).

Next, I'll show a possible implementation in Angular.
Be aware that if you want to use Signals in Angular, you should use [Angular v16.0.0-next.0](https://github.com/angular/angular/releases/tag/16.0.0-next.0) which has just been released.

Needless to say, it is still a prototype and you may not want to use it in production:)

Another way to try it out is by using [StackBlitz](https://stackblitz.com/edit/angular-ednkcj?file=src%2Fmain.ts) and importing the signal folder.

Here is what Signals in Angular could look like:

```
...

@Component({
  selector: 'signals-not-noise',
  standalone: true,
  template: `
    <div>Count: {{ count() }}</div>
    <div>Double: {{ doubledCount() }}</div>
  `
})
export class App {
  count = signal(0);
  doubledCount = computed(() => this.count() * 2);

  constructor() {
    setInterval(() => this.count.set(this.count() + 1), 1000);
  }
}
```

Since `doubleCount` uses `count` that is a Signal, `doubleCount` gets updated every time `count` changes.

In other words, `doubleCount` depends on, or is computed from, `count` and this dependency is reflected automatically whenever the latter changes.

Thanks to computed we can take the value of other Signals and build on them. Moreover, it is memorized, so it doesn’t need to run the computation every time you need the value, but only when it changes.

This leads to fine-grained reactivity that could support better change detection.

What about that function call in the template? [Function calls in templates are not as bad as you might thing](https://levelup.gitconnected.com/signals-in-angular-is-rxjs-doomed-5b5dac574306#19b9).



### Signals and RxJS
It might seem Signals is going to take over and replace RxJS.

However, Signals don’t handle race conditions very well, so RxJS is going to stay and support asynchronous operations.

It is also possible to turn SIgnals into Observables and vice versa.

You can watch an explanation by Joshua Morony.
{% embed https://www.youtube.com/watch?v=4FkFmn0LmLI&ab_channel=JoshuaMorony %}

Using Signals in Angular might facilitate learning Angular, at least in the beginning.

### New Change Detection
This could be a big change for Angular.

Thanks to Signals, we can see a future without zone.js.

Signals allow checking changes at a more granular level rather than checking the whole component tree for changes. In turn, this could be used to improve Angular change detection.

At the moment, different frameworks check what changes in the DOM tree at different levels, for example at

App tree level — Angular
Component tree level — React
Individual elements — Solid
It seems like Angular aims at becoming more granular, and in doing so, they plan to keep Signals interoperable with zone.js.

According to Alex Rickabaugh (the same person behind [The Little-known Story Behind Angular Standalone Components](https://javascript.plainenglish.io/the-little-known-story-behind-angular-standalone-components-2392abc4fcfa) and Technical Lead in the Angular Core Team), you wouldn’t need ngOnChange in a Signal world.

## Good stuff
- Joshua Morony's introduction to Signals in Angular on [YouTube](https://www.youtube.com/watch?v=4FkFmn0LmLI&ab_channel=JoshuaMorony)
- [Stackblitz](https://stackblitz.com/edit/angular-ednkcj?file=src%2Fmain.ts) example by Enea Jahollari
- [Reactivity with Signals is coming to Angular](https://www.youtube.com/watch?v=ov0rSrZT4lk&ab_channel=BrandonRoberts) — Brandon Roberts and Alex Rickabaugh
- [The Evolution of Signals in JavaScript](https://dev.to/this-is-learning/the-evolution-of-signals-in-javascript-8ob) by Ryan Carniato on DEV