没有价值的超时可观察订阅
Timeout observable subscription without value
我正在订阅来自 Firebase 的可观察返回值。如果连接出现问题,我希望订阅超时。这样做的正确方法是什么?我尝试了以下操作,但这是在我收到最后一个值后 20 秒超时:
let chatSubscription = this.getChats().timeoutWith(20000, Observable.throw(new Error('timeout'))).subscribe(chats => { ... });
//编辑:getChats()
getChats() {
return Observable.create(observer => {
let chatList = this.db.list('/users/' + this.auth.user.uid + '/chats').timeoutWith(20000, Observable.throw(new Error('timeout'))).subscribe(chats => {
observer.next(chats);
});
//keep track of this subscription
this.openSubscriptions.push(chatList);
});
}
您可以从超时 Observable
和 switchMap
开始,从初始超时到第一个发出的项目的原始值,例如:
const chats$ = this.getChats();
chats$.map(v => Observable.just(v))
.startWith(Observable.never().timeoutWith(20000, new Error()))
.switchMap(v => v)
.subscribe(...);
如果在初始化后的 20 秒内发出一个值,switchMap
只是身份运算符,并继续提供原始值流中的项目。
您可以使用 race
来收听首先产生某些内容的可观察对象:
const chats = this.db.list('/users/' + this.auth.user.uid + '/chats');
const timeout = Observable.throw(new Error("timed out")).delay(20000);
const chatWithTimeout = Observable.race(chats, timeout);
chatWithTimeout.subscribe(msg => ..., err => ...);
此外,您对 Observable.create
的用法似乎有点不合常理。我建议采用上面的代码并将其用作您的 getChats
:
getChats() {
const chats = this.db.list('/users/' + this.auth.user.uid + '/chats');
const timeout = Observable.throw(new Error("timed out")) .delay(20000);
const chatWithTimeout = Observable.race(chats, timeout);
return chatWithTimeout;
}
// usage
const subscription = foo.getChats().subscribe(...);
有了这个版本,您就不需要再保留一份开放订阅列表。让观察者自己跟踪这个订阅。
我正在订阅来自 Firebase 的可观察返回值。如果连接出现问题,我希望订阅超时。这样做的正确方法是什么?我尝试了以下操作,但这是在我收到最后一个值后 20 秒超时:
let chatSubscription = this.getChats().timeoutWith(20000, Observable.throw(new Error('timeout'))).subscribe(chats => { ... });
//编辑:getChats()
getChats() {
return Observable.create(observer => {
let chatList = this.db.list('/users/' + this.auth.user.uid + '/chats').timeoutWith(20000, Observable.throw(new Error('timeout'))).subscribe(chats => {
observer.next(chats);
});
//keep track of this subscription
this.openSubscriptions.push(chatList);
});
}
您可以从超时 Observable
和 switchMap
开始,从初始超时到第一个发出的项目的原始值,例如:
const chats$ = this.getChats();
chats$.map(v => Observable.just(v))
.startWith(Observable.never().timeoutWith(20000, new Error()))
.switchMap(v => v)
.subscribe(...);
如果在初始化后的 20 秒内发出一个值,switchMap
只是身份运算符,并继续提供原始值流中的项目。
您可以使用 race
来收听首先产生某些内容的可观察对象:
const chats = this.db.list('/users/' + this.auth.user.uid + '/chats');
const timeout = Observable.throw(new Error("timed out")).delay(20000);
const chatWithTimeout = Observable.race(chats, timeout);
chatWithTimeout.subscribe(msg => ..., err => ...);
此外,您对 Observable.create
的用法似乎有点不合常理。我建议采用上面的代码并将其用作您的 getChats
:
getChats() {
const chats = this.db.list('/users/' + this.auth.user.uid + '/chats');
const timeout = Observable.throw(new Error("timed out")) .delay(20000);
const chatWithTimeout = Observable.race(chats, timeout);
return chatWithTimeout;
}
// usage
const subscription = foo.getChats().subscribe(...);
有了这个版本,您就不需要再保留一份开放订阅列表。让观察者自己跟踪这个订阅。