如何在行为主题上抛出错误并继续播放? [英] How do I throw an error on a behaviour subject and continue the stream?
问题描述
一方面,我有一个流,有时可能会引发错误:
On one end, I have a stream which may occasionally throw an error:
this.behaviorSubject.error(error)
不过,稍后,我想继续播放视频流
Later on, however, I want to continue the stream:
this.behaviorSubject.next(anotherValue)
另一端,我有一个订阅者订阅了 behaviorSubject.asObservable()
.
on the other end, I have a subscriber subscribed to behaviorSubject.asObservable()
.
在订阅中,我正在处理值和错误:
In the subscribtion, I'm handling the value and the error:
.subscribe(
( value ) => { /* ok */ },
( error ) => { /* some error */ }
);
我希望效果与简单的 onSuccess
和 onError
回调相同,其中每次发生错误时都会调用 onError
,不会阻止将来的 onSuccess
调用.我该如何使用RXJS?
I want the effect to be the same as a simple onSuccess
and onError
callback, where onError
is called every time an error occurs and doesn't prevent future onSuccess
calls from being made. How do I do this with RXJS?
我已经研究了catch,但是它似乎只是防止在订阅者上调用错误.
I've looked into catch but it seems to just prevent error from being called on subscribers.
推荐答案
简短答案:这是不可能的.
如何使用它: RxJS的基本概念是,任何错误
或 complete
调用都将基本上"kill"流.这个概念迫使您只是想随便乱扔错误" ,而是要正确处理应用程序中的错误和数据流.例如, BehaviorSubject
通常用于保存数据,但是,不不应用于包含数据的检索/创建过程,并处理在此过程中可能发生的错误.数据检索.
How to work with this: The basic concept of RxJS is that any error
or complete
-call will basically "kill" a stream. This concept forces you not "just to throw around errors here and there as you please" but to handle errors and the flow of data within your application properly. A BehaviorSubject
for example is typically meant to hold data, however it should not be used to also include the process of retrieving/creating that data and handle possible errors that might occur during the retrieval of the data.
因此,如果您想读一本书,则应将流程分为两部分:
So if you want to go by the book, you should split up your flow into two parts:
- 数据的检索/创建:一种流,该流将运行一次,然后完成并/或在发生流错误时抛出错误.检索到数据后,会将其发送到商店.
- 商店(例如,您的情况:一堆BehaviorSubjects):只有有效数据到达商店,这意味着此处未执行任何错误处理,所有部分都依赖于商店可以信任商店中保存的正确数据.
- Retrieval/creation of the data: A stream, that will run once then then completes and/or throws an error whenever one occurs. When the data is retrieved it will be sent to the store.
- The store (e.g. as in your case: a bunch of BehaviorSubjects): Only valid data arrives in the store, this means that no error-handling is done here and all parts relying on the store can trust in the store that it holds the correct data.
作为示例,您的数据流可能如下所示(粗略显示):
As an example your data flow could look as follows (as a rough sketch):
store.ts
dataStore: BehaviorSubject<IData> = new BehaviorSubject<IData>();
errorMessage: BehaviorSubject<IErrorMsg> = new BehaviorSubject<IErrorMsg>();
数据检索.
fetchDataById(id: string) {
httpService.get(`some/rest/endpoint/${id}`)
.subscribe(handleData, handleError);
}
handleData(data: IData) {
errorMessage.next(null);
dataStore.next(data);
}
handleError(error: Error) {
errorMessage.next(error.message);
dataStore.next(null);
}
但是,这看起来像是很多开销……" -是的,但是它可以确保应用程序中数据的干净且易于理解,易于测试和维持.还有一些现成的商店概念,例如 ngrx 或 redux .
"But this looks like a lot of overhead..." - True, however it ensures a clean and easy-to-understand flow of data within your application, that is easy to test and maintain. Also there are ready-to-use store-concepts like ngrx or redux that could be used.
这篇关于如何在行为主题上抛出错误并继续播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!