I'm working on some legacy code and there is a issue with wrong status updates of the device. Problem is, that the old - legacy state update is done using long polling and it has concurrency issues on the BE which won't be fixed (BE deprecated). Sometimes it forgets to update the state. The messages arrives but it's incorrect.
I can retrieve the correct state requesting it from the new BE (the app is a nasty hybrid right now) but I'm not able to do it correctly. My first idea was to use whitLatestFrom
but it doesn't work for obvious reasons.
legacyService.getState<LegacyState>()
.pipe(
subscribeOn(queue),
withLatestFrom(this.realService.getRealStatus()), // request to the new BE
map(([legacy, real]) => {
// This if statement is a must. getRealStatus() returns only partial state, not the whole DeviceState
// If there is a difference in the state from two sources, I create a new object with correct state
if (legacy.state === DeviceState.INITIALIZED && !real.stable) {
return new LegacyState(
legacy.instrument,
legacy.db,
DeviceState.UNSTABLE,
);
}
return event; // states are same, returning state from legacy source
})
)
.subscribe(event => {
this._deviceStateUpdated$.next(event);
});
Works on app restart/reload but later the real
state is not updated as no new call is made and it just returns previous value. Same happens with combineLatest
. First one (from polling) is updated, second just returns previous value.
Question is: How can I combine two observables in such way, that when the first one is updated I force the update of the new value for the second observable also? And of course, I'm able to process both of them, as the second observable returns just a partial state. I tried multiple maps (swtichMap, concatMap, ...
) but without success.