1

I have a simple scenario:

service.ts:

  private showComposeBoxAction = new BehaviorSubject<boolean>(null);
  showComposeBox = this.showComposeBoxAction.asObservable();


  openComposeBox(event:boolean) {
    console.log("in openComposeBox");
    this.showComposeBoxAction.next(event);
  }

Component.ts:

 constructor(
    private _service: Service,

  ) {

    this.subscriptions.add(
      this._mailingService.showComposeBox.subscribe(event => {
        if (event) {
          this.displayCompose = true;
          console.log("showComposeBox displayCompose", this.displayCompose);
        }
      })
    );

}

component2.ts:

  showComposeBox() {

    if (this.count === 0) {
      this._service.openComposeBox(true);
    }
  }

I have logged a msg within openComposeMsg(). Problem I am facing is that first time I am correctly subscribing to showComposeBox observable but 2nd time when subscribing even when next is not called because msg "in openComposeBox" does not log into console.

Unable to understand behavior of BehaviorSubject. What am I doing wrong?

Always_a_learner
  • 4,585
  • 13
  • 63
  • 112
  • Is your service a singleton ? – Suren Srapyan Jul 13 '18 at 06:36
  • How to know that? @SurenSrapyan Sorry I am not sure. – Always_a_learner Jul 13 '18 at 06:38
  • It is used by multiple components though. – Always_a_learner Jul 13 '18 at 06:39
  • It seems you expect your console.log("in openComposeBox") to execute when you subscribe() to your observable. Why would that happen? The console.log() is executed when the openComposeBox() method is called. Not when you subscribe to an observable. – JB Nizet Jul 13 '18 at 06:40
  • @JBNizet I have console.logged on both openComposeBox and when subscribed. The problem is when console within openComposeBox is not logged even then console.log logged after subscription logging. which is unexpected. – Always_a_learner Jul 13 '18 at 06:42
  • 1
    @JBNizet Hi is using BehaviorSubject. It saves the current data and passes to the subscribers as soon as they subscribe – Suren Srapyan Jul 13 '18 at 06:42
  • 4
    @Simer well, that's the whole point of a BehaviorSubject: it has a current value, and every time you subscribe, you're immediately notified with the current value. If that's not what you want, you shouldn't be using a BehaviorSubject in the first place. – JB Nizet Jul 13 '18 at 06:45
  • 1
    Should I use Subject? probably, yes. Read the documentation. – JB Nizet Jul 13 '18 at 06:50
  • 1
    Yes, it is. And if you read it *before* using the stuff it documents, you would save **weeks**, making the deadline much easier to respect. – JB Nizet Jul 13 '18 at 06:58

1 Answers1

2

I solved my problem with guidance of JB Nizel and Suren Srapyan. By replacing behavior subject with subject. As observable is subscribed in constructor it triggered and uses current saved value of behavior subject which was set to true previously by other function.

Took reference from This SO But now facing another issue that openComposeBox is called and msg is logged even than observable is not subscribed. I will update answer when I get solution.

Always_a_learner
  • 4,585
  • 13
  • 63
  • 112