title: How to convert an Observable into a BehaviorSubject?
body: <p>I'm trying to convert an Observable into a BehaviorSubject. Like this:</p>
  <pre><code>a$ = new Observable()
  b$ = BehaviorSubject.create(new BehaviorSubject(123), a$)
  // 
  </code></pre>
  <p>I have also tried:</p>
  <pre><code>a$ = new Observable()
  b$ = new BehaviorSubject(a$, 123)
  // 
  </code></pre>
  <p>And:</p>
  <pre><code>a$ = new Observable()
  b$ = a$.asBehaviorSubject(123)
  // 
  </code></pre>
  <p>And:</p>
  <pre><code>a$ = new Observable()
  b$ = a$.pipe(
    toBehaviorSubject(123)
  )
  // 
  </code></pre>
  <p>But none of these works. For now I have to implement like this:</p>
  <pre><code>a$ = new Observable()
  b$ = new BehaviorSubject(123)
  a$.subscribe(b$)
  // 
  </code></pre>
  <p>This would be a little bit ugly in a class:</p>
  <pre><code>class Foo() {
    a$ = new Observable() // Actually, a$ is more complicated than this.
    b$ = new BehaviorSubject(123)
  
    constructor() {
      this.a$.subscribe(this.b$)
    }
  }
  </code></pre>
  <p>So, is there a simpler way to convert an Observable to a BehaviorSubject without using class constructor?</p>
  <hr />
  <p>This is my real case:</p>
  <pre><code>export class Foo {
    autoCompleteItems$ = new BehaviorSubject&lt;string[]&gt;(null)
    autoCompleteSelected$ = new BehaviorSubject&lt;number&gt;(-1)
    autoCompleteSelectedChange$ = new Subject&lt;'up'|'down'&gt;()
  
    constructor() {
      this.autoCompleteItems$.pipe(
        switchMap((items) =&gt; {
          if (!items) return EMPTY
          return this.autoCompleteSelectedChange$.pipe(
            startWith('down'),
            scan&lt;any, number&gt;((acc, value) =&gt; {
              if (value === 'up') {
                if (acc &lt;= 0) {
                  return items.length - 1
                } else {
                  return acc - 1
                }
              } else {
                if (acc &gt;= items.length - 1) {
                  return 0
                } else {
                  return acc + 1
                }
              }
            }, -1)
          )
        })
      ).subscribe(this.autoCompleteSelected$)
    }
  
    doAutoComplete = () =&gt; {
      const item = this.autoCompleteItems$.value[this.autoCompleteSelected$.value]
      // do something with `item`
    }
  }
  </code></pre>