الانتقال إلى المحتوى
FrontendAngular· 7 دقائق للقراءة

Signals vs RxJS: State vs Events, Not Old vs New

A signal holds the latest value; an Observable delivers every event in time. Most apps need both.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • Angular 22
محتويات المقال

After upgrading to Angular 22, a team adopts a rule: no Observables in components. The product search, built on debounceTime(300) and switchMap, becomes an effect that calls the API whenever query changes. Within a week, two bugs come in. Typing "laptop" sends a request for nearly every letter, and on a slow connection the results for "lap" sometimes arrive last and replace the results for "laptop".

The common misconception

The belief: signals are Angular's new reactivity and RxJS is the old one, so a modern app converts every Observable to a signal and stops using operators.

The direction of travel makes it look that way. Angular's newer APIs are signal-based: input(), model(), viewChild(), resource(), and since v22 a stable httpResource(). Signals also remove ceremony: no subscribe(), no unsubscribing, no async pipe. And many Observables in Angular code really were state in disguise: a BehaviorSubject holding the cart, a combineLatest that only existed to compute a total. Converting those is a genuine improvement. The mistake is the word "every". Some Observables were never state.

What actually happens

A signal is a value; an Observable is a sequence of events

A always has a current value, and reading it is synchronous. When it changes, its consumers learn that they're stale, and each one reads whatever value is current when it runs. Two things follow. Setting a value equal to the current one, by Object.is by default, notifies nobody. And values set between two reads are never seen: the interop guide sets a signal to 1, 2 and 3 in a row, and toObservable() emits only 3.

An pushes every emission to its subscribers, in order, and it has a notion of time: it can complete or fail, and operators can wait (debounceTime), cancel (switchMap), merge or retry. So the useful question isn't "old or new?" but "state or event?". The current query, the selected product and the cart are state: only the latest value matters. A keystroke you want to wait out, a request that a newer one supersedes, a WebSocket message or a click you must count are events: each occurrence, and when it happened, matters.

The search box is both

The query is state, so a signal is the right home for it. "Fetch results for the latest query, but not on every keystroke, and ignore responses that arrive too late" is about events over time. An effect has no timing operators, and nothing cancels the previous request when a new one starts.

TypeScript product-search.ts
import { Component, effect, inject, signal } from '@angular/core';@Component({  selector: 'app-product-search',  templateUrl: './product-search.html',})export class ProductSearch {  private readonly api = inject(ProductApi);  readonly query = signal('');  readonly results = signal<Product[]>([]);  constructor() {    // A request per keystroke, and a slow old response can win    effect(() => {      this.api.search(this.query()).subscribe((r) => this.results.set(r));    });  }}

toObservable() turns the query into a stream. debounceTime(300) is a : it emits only after typing pauses for 300 ms. switchMap unsubscribes from the previous request when a new query arrives, and with HttpClient that unsubscription aborts the request, so a stale response can't overwrite a newer one. That's the gone. toSignal() subscribes once, unsubscribes when the component is destroyed, and the template reads results() like any other signal. One behavior to plan for: if the stream errors, toSignal() throws that error every time results() is read, and an Observable that has errored emits nothing more. A single failed request inside switchMap would break the search box for good, so catch errors inside the switchMap callback, for example by returning of([]) from catchError. If you don't need the debounce, rxResource() and httpResource() also cancel an outdated request and expose the result, the loading state and the error as signals.

Where signals do replace RxJS

State that only lived in a BehaviorSubject so templates could read it through the async pipe is where signals clearly win:

TypeScript cart-store.ts
import { Service, computed, signal } from '@angular/core';@Service()export class CartStore {  // Was: a BehaviorSubject<CartLine[]> read with the async pipe  private readonly lines = signal<CartLine[]>([]);  readonly count = computed(() => this.lines().length);  add(line: CartLine): void {    this.lines.update((all) => [...all, line]);  }}

Reads are synchronous, count is memoized instead of rebuilt by combineLatest and map, and there's no subscription to forget. Templates that read count() are updated when it changes, with nothing to wire up.

What you haveBetter fitWhy
A BehaviorSubject holding current statesignal()Synchronous reads, no subscription to manage
combineLatest + map to derive a valuecomputed()Memoized, and read like any other value
An Observable whose latest value a template showstoSignal()One subscription, cleaned up on destroy
Debouncing, cancellation, retriesRxJS operatorsSignals have no notion of time
Messages or clicks where each one countsAn ObservableA signal keeps only the latest distinct value

How to apply it in your project

  1. Sort your ObservablesFor each one, ask: does anyone care about anything but the latest value? If not, it's state. If every emission or its timing matters, it's an event stream.
  2. Move state to signalsReplace BehaviorSubject state containers with signal(), and values derived with combineLatest with computed().
  3. Convert at the edgeWhere a template needs an Observable's latest value, call toSignal() once, in a field initializer, with an initialValue, or requireSync: true for sources that emit synchronously, like a BehaviorSubject.
  4. Keep pipelines for time and cancellationLeave debouncing, switchMap, retries and event streams in RxJS, and feed them from signals with toObservable().

Key takeaways

  • A signal answers "what's the value now?"; an Observable answers "what happened, and when?".
  • Signals skip intermediate values and ignore equal ones: right for state, wrong for events.
  • Move state to signal() and computed(); keep debouncing, cancellation and event streams in RxJS.
  • Bridge the two with toSignal() and toObservable() instead of rewriting a pipeline as an effect.

📚 المصادر

  1. RxJS interop with Angular signals — Angular
  2. toSignal — Angular API
  3. Angular Signals — Angular
  4. Making HTTP requests — Angular
  5. httpResource — Angular API

التعليقات (0)