Sub topic 8
When toObservable emits and why values can be skipped
This chapter assumes you can create and read signals, know that effect() runs after signal changes, and understand basic RxJS subscription. You should also know that toObservable needs an injection context, which an earlier chapter covered.
How toObservable decides when to emit
toObservable(signal) returns an observable. It does not emit on every set() call. Internally it creates an effect that reads the signal, and that effect pushes the value into a ReplaySubject with a buffer size of one.
This design has three consequences:
- Emission is asynchronous. The effect runs during change detection or a flush of pending effects, not at the moment you call
set(). - Only the latest value counts. If you set a signal several times before the effect runs, the effect reads the signal once and sees the final value.
- A new subscriber receives the most recent value, because of the replay buffer.
The effect is created when you call toObservable, so it lives as long as the injection context that owns it. When that context is destroyed, the observable completes.
Seeing skipped values
The next component sets a signal twice in a row and logs what a subscriber sees.
import { Component, signal } from '@angular/core';
import { toObservable } from '@angular/core/rxjs-interop';
@Component({
selector: 'app-demo',
template: ``,
})
export class Demo {
n = signal(0);
constructor() {
toObservable(this.n).subscribe((v) => console.log(v));
this.n.set(1);
this.n.set(2);
// The subscriber logs 2. The value 1 is never emitted,
// because the effect has not run between the two set() calls.
}
}The signal held 0, then 1, then 2, all inside one synchronous block. The effect has not run yet, so it only ever reads 2. The value 1 is gone.
This is not a bug. Signals describe current state, not a history of changes. An observable built from a signal inherits that meaning: it reports the state at each point where the effect runs.
The same applies when a signal is set back to a value it already had. Signals compare values with Object.is by default, so setting 0 when the value is already 0 does not notify anyone.
Timeline of what happens
The diagram below shows the order of events for the example above.
What this means for your code
Treat toObservable as a way to observe state over time, not a way to log every write. Choose based on what the downstream code needs.
| You need | Use toObservable? | Reason |
|---|---|---|
| React to the current value after changes settle | Yes | Skipped intermediate values do not matter. |
| A debounced or switched stream built from a signal | Yes | The operators work on the latest values. |
| Every individual write, in order | No | Writes between effect runs are lost. |
| A synchronous reaction at the moment of `set()` | No | Emission is deferred to the effect. |
If you need every event, such as a stream of user actions, model it as a Subject or an output event, not as a signal. A signal is the wrong source for an event log.
The initial emission is also deferred
The first value is not emitted synchronously when you subscribe in a constructor. The effect has to run once first. So code that expects the value right away may see nothing until the first flush.
If a template or downstream code needs a value before that first emission, put a starting value into the pipeline with startWith, or convert back with toSignal and an initialValue. The debounced search chapter that follows uses the second option.
import { Component, signal } from '@angular/core';
import { toObservable } from '@angular/core/rxjs-interop';
import { startWith } from 'rxjs';
@Component({
selector: 'app-start',
template: ``,
})
export class Start {
n = signal(5);
constructor() {
toObservable(this.n)
.pipe(startWith(-1))
.subscribe((v) => console.log(v));
// Logs -1 immediately, then 5 when the effect runs.
}
}Common mistakes
- Expecting one emission per
set(). Count emissions per effect run instead. - Mutating a signal inside a
subscribecallback and assuming it is processed in the same tick. Writes there schedule another effect run. - Using
toObservableto track changes to an object signal that you mutate in place. The signal never notifies, so nothing is emitted. Create a new object withset()orupdate().
Exercise
A component runs s.set(1); s.set(2); s.set(2); in one synchronous block, after calling toObservable(s).subscribe(log) in the constructor. The signal started at 0. What does log receive?
Show answer
It receives 2 once. The effect has not run during the set() calls, so it reads only the final value. The later set(2) is also ignored by the signal because it equals the current value. The value 1 is never emitted.
Exercise
You need to send an analytics event for every click count the user reaches, in order, and the count is stored in a signal that can be updated several times per tick. Why is toObservable a poor fit, and what would you use?
Show answer
Updates made between effect runs are collapsed, so some counts would never be reported. Use a Subject (or an output event) that emits on each click and send the analytics call from that stream.