I have a service which gets two observables, storeValueOne$
and storeValueTwo$
, which are of type number
and update frequently but not at the same time.
I want to add the values of storeValueOne
, storeValueTwo
and store them into another observable which keeps track of the changes.
With ngrx merge
I can combine the two observables together but
it's not clear to me how to do the calulcation as they are updating at different rates, and I also don't know which one changed its value.
import { Injectable } from '@angular/core';
import { forkJoin, map, merge } from 'rxjs';
import { UnstableCompComponentTwoStore } from './components/unstable-comp-two/unstable-comp-two.store';
import { UnstableCompStore } from './components/unstable-comp/unstable-comp.store';
@Injectable({
providedIn: 'root'
})
export class GlobalStateService {
constructor(
private storeUnstableOne: UnstableCompStore,
private storeUnstableTwo: UnstableCompComponentTwoStore
) { }
storeValueOne$ = this.storeUnstableOne.select((state) => state.unstableStateVal);
storeValueTwo$ = this.storeUnstableTwo.select((state) => state.unstableStateValTwo);
combineData(){
const merged = merge(this.storeValueOne$, this.storeValueTwo$)
// merged.forEach(d => console.log(d))
/* value 1 + value 2 */
// mergedState = value1 + value2
}
}