Is there a simpler way to get the latest value of a subject, perform some modification and broadcast the changed value?
This is how I currently do it:
private currentMonthStream: Subject<Moment>;
private goNextMonth() {
this.currentMonthStream.first().subscribe((currentMonth) => {
this.currentMonthStream.next(currentMonth.add(1, 'months'));
});
}
Edit: It occurs to me that this really would only make sense for ReplaySubject and BehaviorSubject and have opted to creating my own implementation of ReplaySubject that can do this easily:
export class StreamSubject<T> extends ReplaySubject<T> {
constructor() {
super(1);
}
update(fn: (val: T) => T) {
this.first().subscribe((val) => {
this.next(fn(val));
});
}
}
However, since I'm still new to rx, is there something wrong with needing a function like this?