Consider an Angular component using Subject from RxJS. The Subject emits values before a subscriber subscribes. What will the subscriber receive?
import { Subject } from 'rxjs'; const subject = new Subject<number>(); subject.next(1); subject.next(2); subject.subscribe(value => console.log('Subscriber received:', value)); subject.next(3);
Think about whether Subject stores past values for new subscribers.
A Subject does not replay past values. Subscribers only receive values emitted after they subscribe. So the subscriber only gets the value 3 emitted after subscription.
What will be logged when subscribing to a BehaviorSubject that was created with an initial value and has emitted new values before subscription?
import { BehaviorSubject } from 'rxjs'; const behaviorSubject = new BehaviorSubject<number>(0); behaviorSubject.next(1); behaviorSubject.next(2); behaviorSubject.subscribe(value => console.log('Subscriber received:', value));
Remember that BehaviorSubject always emits the latest value to new subscribers.
A BehaviorSubject emits the most recent value to new subscribers immediately upon subscription. Since the last emitted value was 2, the subscriber receives 2.
Given a ReplaySubject with buffer size 2, what values will a late subscriber receive if the subject emitted 4 values before subscription?
import { ReplaySubject } from 'rxjs'; const replaySubject = new ReplaySubject<number>(2); replaySubject.next(10); replaySubject.next(20); replaySubject.next(30); replaySubject.next(40); replaySubject.subscribe(value => console.log('Subscriber received:', value));
ReplaySubject replays the last bufferSize values to new subscribers.
The ReplaySubject with buffer size 2 stores the last two emitted values. So the subscriber receives 30 and 40 immediately upon subscription.
Which option correctly creates a BehaviorSubject holding a string and emits a new value?
Check the constructor syntax and method names for BehaviorSubject.
Option C correctly uses the constructor with an initial value and calls next to emit a new value. Option C uses a non-existent emit method. Option C misses the new keyword. Option C misses the required initial value in constructor.
In this Angular service, a ReplaySubject is created but subscribers do not receive any past values. What is the cause?
import { ReplaySubject } from 'rxjs'; export class DataService { private replaySubject = new ReplaySubject<number>(3); emitValues() { this.replaySubject.next(1); this.replaySubject.next(2); this.replaySubject.next(3); } getValues() { return this.replaySubject.asObservable(); } } // In component const service = new DataService(); service.emitValues(); service.getValues().subscribe(value => console.log('Received:', value));
Check if the ReplaySubject instance is the same when emitting and subscribing.
If the ReplaySubject is recreated or a new instance is used when subscribing, previous values are lost. In this code, if DataService is instantiated multiple times or the ReplaySubject is recreated, subscribers won't get past values.