Tiberiu Popescu
Tiberiu Popescu

Reputation: 4524

Rxjs pass data after error on Observable

I know that by definition when an Observable is Completed or it has an Error it became cold and you cannot pass data to it. So if you want to pass data after the Error you need to create a new Observable.

I'm just wondering if you can somehow restore the Observable after the Error, as it makes more sense for me to restore it instead to create a new one with the same functionality ?

Ex :

const subject = new Rx.Subject();

subject.subscribe(
  data => console.log(data), 
  error => console.log('error')
);

subject.next('new data');
subject.next('new data2');

subject.error('error');

subject.next('new data3');

The new data3 is not sent as it has an error before that.
Working example : https://jsbin.com/sizinovude/edit?js,console

PS : I want the subscriber to have the error and also send data after, looks like the only way is to create a new Observable and subscribe to the new one after the error.

Upvotes: 2

Views: 1071

Answers (2)

jlvaquero
jlvaquero

Reputation: 8785

One strategy could be to handle well know errors (i.e. data validation) in observables by creating an observable that is the error stream.

I use .NET but I am sure you get the idea:

class UseSubject
{
    public class Order
    {            
        private DateTime? _paidDate;

        private readonly Subject<Order> _paidSubj = new Subject<Order>();
        private readonly Subject<Error> _errorSubj = new Subject<Error>();
        public IObservable<Order> Paid { get { return _paidSubj.AsObservable(); } }
        public IObserble<Error> Error {get {return _errorSubj.AsObservable();
}}
        public void MarkPaid(DateTime paidDate)
        {
            if (!paidDate.isValid){_errorSubj.OnNext(New Error("invalid paidDate")); return}
            _paidDate = paidDate;                
            _paidSubj.OnNext(this); // Raise PAID event
        }
    }

    private static void Main()
    {
        var order = new Order();
        order.Paid.Subscribe(_ => Console.WriteLine("Paid")); // Subscribe
        order.Error.Subscribe(_ => Console.WriteLine("Error"));
        order.MarkPaid(DateTime.Now);
    }
}

Upvotes: 0

cartant
cartant

Reputation: 58440

No, my understanding is that you cannot emit further data to subscribers after the observable errors.

The Observable Termination section in the Observable Contract states:

When an Observable does issue an OnCompleted or OnError notification, the Observable may release its resources and terminate, and its observers should not attempt to communicate with it any further.

This suggests to me that if you call next after error, any subscribers will not receive the value, as they will have unsubscribed upon receipt of the error notification. This is also mentioned in the Observable Termination section:

When an Observable issues an OnError or OnComplete notification to its observers, this ends the subscription.

Upvotes: 1

Related Questions