I would like to merge multiple subjects (fooSubject$, barSubject$) into one observable (filterChanged$) and return the (initial) values (_foo, _bar), when I subscribe to merged observable (filterChanged$) and when one of the values change.
I already used BehaviorSubject (for both subjects) but then filterChanged$ will emit twice on subscribe. How can I immediately return the values (_foo, _bar) when subscribe to observable filterChanged$ without using BehaviorSubject?
export interface IFilterData {
foo: string;
bar: string;
}
@Injectable({
providedIn: 'root'
})
export class FilterService {
public foo$: Observable<string>;
public bar$: Observable<string>;
public filterChanged$: Observable<IFilterData>;
private fooSubject$: Subject<string>;
private barSubject$: Subject<string>;
private _foo: string;
private _bar: string;
constructor() {
this._foo = 'InitialFoo';
this._bar = 'InitialBar';
this.foo$ = this.fooSubject$.asObservable();
this.bar$ = this.barSubject$.asObservable();
this.filterChanged$ = merge(this.fooSubject$, this.barSubject$).pipe(
switchMap(() => {
return of({
foo: this._foo,
bar: this._bar
});
})
);
}
set foo(value: string) {
this._foo = value;
this.fooSubject$.next(value);
}
get foo(): string {
return this._foo;
}
set bar(value: string) {
this._bar = value;
this.barSubject$.next(value);
}
get bar(): string {
return this._bar;
}
}