I'm using Angular. I have service which I use to store and get data from localstorage. I'm saving BehaviorSubject asObservable(). Code below:
import { Injectable} from "@angular/core";
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class NavigationStorageService {
ModuleTitle = new BehaviorSubject<string>("");
currentModuleTitle = this.ModuleTitle.asObservable();
constructor() {}
setModuleId(ModuleId) {
localStorage.setItem('subModuleId', ModuleId)
this.ModuleTitle.next(ModuleId);
}
getModuleId() {
this.ModuleTitle.next(localStorage.getItem('subModuleId'));
return this.currentModuleTitle;
}
}
Next I'm getting this data using code below:
getModuleId() {
this._navigationStorageService.getModuleId()
.subscribe(
subModuleId => {
this.subModuleId = subModuleId
},
error => {
console.log(error)
},
() => console.log("getModuleId"));
}
My problem is I want to execute other function when observable is completed but this observable is never completed so I can't do that.
() => console.log("getModuleId") is not showing the message.
How can I solve this?
At the other part of my App I'm using Observable to get HttpClient request and it works fine. I mean onComplete fires as it should to.
I have tried using .finally()
getModuleId() {
this._navigationStorageService.getModuleId()
.finally(() => console.log("getModuleId"))
.subscribe(
subModuleId => {
this.subModuleId = subModuleId
},
error => {
console.log(error)
},
() => console.log("getModuleId"));
}
but it doesn't help.