I am facing an issue with a BehaviorSubject
variable named saveToLocalStorage
. In one of my methods, the next()
method is called twice, but only one of the calls is being completed while the other one gets overwritten. The service subscribed to these calls is responsible for saving data to local storage and nothing else. To address this issue, I decided to handle the calls within an Observable
with a delay between the calls. Here is an example:
Observable.of(1).pipe(
tap(()=> {if(this.currentWebAppMode !== newMode){
this.saveToLocalStorage.next([DataType.STORED_RESULTS, null])};
}),
debounceTime(100))
.subscribe(result => this.saveToLocalStorage.next([DataType.WEB_APP_MODE, newMode]));
While this solution works, it feels like a hack. I am looking for a more proper way to ensure that both subsequent next()
calls to saveToLocalStorage
can execute properly.
Update: With Deborah's help, I was able to improve my solution by using the concatMap
operator instead of tap
and debounceTime
. Here is the updated code:
Observable.of(1).pipe(
concatMap(()=> {if(this.dataStoreService.currentAppMode !== newMode){
this.saveToLocalStorage.next([DataType.STORED_RESULTS, null])};
return of(true)}))
.subscribe(result => this.saveToLocalStorage.next([DataType.WEB_APP_MODE, newMode]));