WICG / observable

Observable API proposal
https://wicg.github.io/observable/
Other
543 stars 12 forks source link

Operator should not close its observable subscription on throwing error? #115

Closed tetsuharuohzeki closed 3 months ago

tetsuharuohzeki commented 4 months ago

I'm sorry if this discussion has been concluded once previously.

I understand the design has a long history since the birth of Reactive Extensions that Observable will be completed on throwing an exception in the middle operator.

However, I also think it might be more beneficial design that Observable does not close (stop) on throwing error in a middle operator. By this design, an user can observe an event until they unsubscribe, and keep observable type to Observable<T> if an middle operation in chain might be failure.

For example, Cysharp/R3 breaks its design that does not stop a observable chain and allow to pass () | Exception as a argument for onComplete().

domfarolino commented 4 months ago

By this design, an user can observe an event until they unsubscribe

Or until the Observable calls subscriber.error(), right? If I understand correctly, you're proposing that throw error; in an Observable does not close the subscription, but that subscriber.error() would still close it?

My only concern there is that we'd have a pretty big difference in behavior between two similar error-handling scenarios, which might confuse people ("what, which error-throwing scenario results in 'closing' vs not??")

tetsuharuohzeki commented 4 months ago

Or until the Observable calls subscriber.error(), right? If I understand correctly, you're proposing that throw error; in an Observable does not close the subscription, but that subscriber.error() would still close it?

No. What I would like to say in https://github.com/WICG/observable/issues/115#issue-2128252835 is that subscriber.complete() only runs close the subscription. Calling subscriber.error() in upstream just send an error to downstream SubscriptionObserver like:

Observable((subscriber) => {
    subscriber.next(0);
    subscriber.error(1);
    subscriber.next(2);
    subscriber.complete();
}).subscribe((value) => {
    console.log(`onNext: ${value}`);
}, (error) => {
    console.log(`onError: ${error}`);
}, () => {
    console.log(`onComplete`);
});
// result is:
//     onNext: 0
//     onError: 1
//     onNext: 2
//     onComplete

I filed this issue as a proposal which that "whether Observable should contains all events including happened errors until completing it"

domfarolino commented 3 months ago

I don't think there is much appetite for this unfortunately, and existing Observable implementations seem to pretty much not do this, which is inline with the Observable contract.

I think I will close this for now, but thanks for the feedback.