typescript 如何将 Observable 转换为 BehaviorSubject?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53372138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert an Observable into a BehaviorSubject?
提问by awmleer
I'm trying to convert an Observable into a BehaviorSubject. Like this:
我正在尝试将 Observable 转换为 BehaviorSubject。像这样:
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 a Observable to a BehaviorSubject without using class constructor?
那么,是否有一种更简单的方法可以在不使用类构造函数的情况下将 Observable 转换为 BehaviorSubject?
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`
}
}
采纳答案by yaya
No need to convert it. just create a subject and attach observable to it. example:
不需要转换它。只需创建一个主题并将可观察对象附加到它。例子:
var obs = new rxjs.Observable((s) => {setTimeout(()=>{s.next([1])} , 500)}) //observable
var sub = new rxjs.BehaviorSubject([0]) //create subject
obs.subscribe(sub) //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(sub)
相当于:
obs.subscribe({
next: v => sub.next(v),
error: v => sub.error(v),
complete: () => sub.complete()
})
回答by tmuecksch
This is how I convert my Observables to BehaviorSubjects:
这就是我将 Observables 转换为 BehaviorSubjects 的方式:
import { Observable, BehaviorSubject } from 'rxjs';
export function convertObservableToBehaviorSubject<T>(observable: Observable<T>, initValue: T): BehaviorSubject<T> {
const subject = new BehaviorSubject(initValue);
observable.subscribe({
complete: () => subject.complete(),
error: x => subject.error(x),
next: x => subject.next(x)
});
return subject;
}
回答by Luillyfe
I have pretty concerns about the use case too. But here it comes a solution, feel free vote down as long you leave feedback too. Since BehaviourSubject
and any other Subject
are Observables
,
我也很担心用例。但这里有一个解决方案,只要您也留下反馈,请随意投票。由于BehaviourSubject
和任何其他Subject
都是Observables
,
import { BehaviorSubject, from } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators';
const source$ = from([1,2,3,4,5,6,7,8,9]);
const bs = new BehaviorSubject('start')
.pipe(
mergeMap(() => source$)
);
bs.subscribe(console.log);