Skip to content
diggn.de
← all posts Angular & TypeScript · · 2 minutes

Signals instead of RxJS, three months on

I moved a mid-sized part of an app from RxJS streams to signals. The code got shorter, but it didn't get better everywhere.

It started with a feature where six observables met in a combineLatest to fill a table. It worked. But if I hadn't looked at it for two weeks, I couldn't follow it any more, and on a project with four other people working on it, two weeks isn't long.

What got better straight away

Derived state now reads like a formula rather than a pipeline. A chain of map, filter and shareReplay turns into a single computed you can read top to bottom:

table.component.tsTypeScript
readonly visibleRows = computed(() => {
  const term = this.searchTerm().trim().toLowerCase();
  if (!term) return this.rows();
  return this.rows().filter((row) => row.title.toLowerCase().includes(term));
});

There's nothing to subscribe to, nothing to tear down and no async pipe in the template. What I like most, though, is that the value is always there. I don't have to think about whether anything has been emitted by the first render.

Signals describe state, RxJS describes events over time. Things usually go wrong when you make one do the other's job.

Where I stuck with RxJS

Anything to do with time: debouncing input with debounceTime, requests that should cancel each other, retries with backoff. You can rebuild all of that with signals, but it gets longer and harder to read, because you end up writing out by hand what switchMap does in one word.

These days RxJS stays at the edges of the app and the core uses signals. In between there's a toSignal, in exactly one place.

What I underestimated

I used effect() far too often at first. Three times I wrote an effect that just set another signal, which brought back exactly the mess I'd been trying to get rid of. If a value follows from other values, it belongs in a computed. Effects are for anything that goes outside, such as logging or localStorage.

Next time I'll start with the computed values and only then look at where I still need a stream.

#angular#typescript#signals

Related