Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

javascript - How to convert an Observable into a BehaviorSubject?

I'm trying to convert an Observable into a BehaviorSubject. Like this:

a$ = new Observable()
b$ = BehaviorSubject.create(new BehaviorSubject(123), a$)
// ??

I have also tried:

a$ = new Observable()
b$ = new BehaviorSubject(a$, 123)
// ??

And:

a$ = new Observable()
b$ = a$.asBehaviorSubject(123)
// ??

And:

a$ = new Observable()
b$ = a$.pipe(
  toBehaviorSubject(123)
)
// ??

But none of these works. For now I have to implement like this:

a$ = new Observable()
b$ = new BehaviorSubject(123)
a$.subscribe(b$)
// ??

This would be a little bit ugly in a class:

class Foo() {
  a$ = new Observable() // Actually, a$ is more complicated than this.
  b$ = new BehaviorSubject(123)

  constructor() {
    this.a$.subscribe(this.b$)
  }
}

So, is there a simpler way to convert an Observable to a BehaviorSubject without using class constructor?


This is my real case:

export class Foo {
  autoCompleteItems$ = new BehaviorSubject<string[]>(null)
  autoCompleteSelected$ = new BehaviorSubject<number>(-1)
  autoCompleteSelectedChange$ = new Subject<'up'|'down'>()

  constructor() {
    this.autoCompleteItems$.pipe(
      switchMap((items) => {
        if (!items) return EMPTY
        return this.autoCompleteSelectedChange$.pipe(
          startWith('down'),
          scan<any, number>((acc, value) => {
            if (value === 'up') {
              if (acc <= 0) {
                return items.length - 1
              } else {
                return acc - 1
              }
            } else {
              if (acc >= items.length - 1) {
                return 0
              } else {
                return acc + 1
              }
            }
          }, -1)
        )
      })
    ).subscribe(this.autoCompleteSelected$)
  }

  doAutoComplete = () => {
    const item = this.autoCompleteItems$.value[this.autoCompleteSelected$.value]
    // do something with `item`
  }
}
question from:https://stackoverflow.com/questions/53372138/how-to-convert-an-observable-into-a-behaviorsubject

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No need to convert it.

Just create a subject and attach observable to it with : obs.subscribe(sub)

example:

var obs = new rxjs.Observable((s) => {setTimeout(()=>{s.next([1])} , 500)}) //observable
var sub = new rxjs.BehaviorSubject([0]) //create subject
obs.subscribe(sub) //<----- HERE ----- attach observable to subject
setTimeout(() => {sub.next([2, 3])}, 1500) //subject updated
sub.subscribe(a => console.log(a)) //subscribe to subject

Note: obs.subscribe(sub) is equivalent to :

obs.subscribe({
  next: v => sub.next(v),
  error: v => sub.error(v),
  complete: () => sub.complete()
})

Run it online


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...