title: RXJS - BehaviorSubject: proper use of .value
body: <p>I'm wondering if there is actually a proper use for <code>behaviorSubject.value</code>. According to to <a href="https://stackoverflow.com/a/45227115/1858357">this answer</a>, we should ONLY get values via subscribing.</p>
  <p>A case where it seems okay to me is where I'm using the <code>.value</code> in order to determine the next value to push through the stream, like when I'm toggling a simple boolean:</p>
  <pre><code>myBoolSubject = new BehaviorSubject(false);
  
  toggle() {
    this.myBoolSubject.next(!this.myBoolSubject.value);
  }
  </code></pre>
  <p>The alternative using <code>subscribe()</code> would look like:</p>
  <pre><code>toggle() {
     this.myBoolSubject.pipe(take(1)).subscribe(
        val =&gt; this.myBoolSubject.next(!val)
     );
  }
  </code></pre>
  <p>From looking at the <a href="https://github.com/ReactiveX/rxjs/blob/master/src/internal/BehaviorSubject.ts#L32" rel="nofollow noreferrer">rxjs source</a> and the aforementioned answer, the difference between these two approaches are that <code>.value</code> will throw when:</p>
  <ul>
  <li>the subject has been completed</li>
  <li>there has been an error</li>
  </ul>
  <p>In this simple case, I'm not going to complete the subject and I don't think errors are a concern since I'm just pushing simple boolean values through this subject.</p>
  <p>Is this a valid use case for <code>behaviorSubject.value</code>?  Are there others?</p>
  <p>Another case where it seems okay to use <code>.value</code> is when constructing a new object from the previously emitted value:</p>
  <pre><code>private state = new BehaviorSubject&lt;State&gt;(INITIAL_STATE);
  public state$ = this.state.asObservable();
  
  public updateState(changes: Partial&lt;State&gt;){
      const newState = {...this.state.value, ...changes};
      this.state.next(newState);
  }
  
  </code></pre>
  <p>The alternative would be to cache the latest state emission in another variable, something like this:</p>
  <pre><code>private _state = INITIAL_STATE;
  private state = new BehaviorSubject&lt;State&gt;(INITIAL_STATE);
  public state$ = this.state.asObservable();
  
  public updateState(changes: Partial&lt;State&gt;){
      const newState = {...this._state, ...changes};
      this.state.next(this._state = newState);
  }
  </code></pre>
  <p>Are there any concerns I'm overlooking?</p>