2

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.

1
  • I think you must call complete by hand as an observable does not know when it's completed. See rxjs for more information. Else just call the function you want when subscribing to the value delivered by the observable, like got a new value -> call that function.
    – Fussel
    May 16, 2018 at 11:54

2 Answers 2

1

Subjects complete only when you tell them to complete by calling complete().

The question here is what you're trying to achieve. It's possible that you never want the Subject to complete so you'll never call this.ModuleTitle.complete() but you still want to send the complete notification to "consumers" of currentModuleTitle.

This means that you can use take(1) operator that takes just one value and immediately completes the chain (no need to use asObservable).

currentModuleTitle = this.ModuleTitle.take(1);

Now every time you do currentModuleTitle.subscribe() you'll receive one value immediately and complete. Be aware that BehaviorSubject emits its stored value first so all consumers of currentModuleTitle will in fact receive just the cached value and no further updates.

1
  • This is exactly what I need. Thanks. May 16, 2018 at 16:40
0

You are using bad the Observable object. When you use the getModuleId() method for get an item of localStorage you have this value instantly then the Observable not has sense.

 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)
  }

  getModuleId(id) {
    return localStorage.getItem(id);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.