Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJS Detect When Subscription has Closed

Is there a way to detect when a Subscription has closed? I have a loading component that displays a loading message when a Subscription exists && is not closed, and otherwise displays the content, but once closed I wanted to reset the variable that was referencing the subscription, otherwise I can't use mySubscription.closed as an useful indicating in the template.

like image 545
mtpultz Avatar asked Jun 18 '18 06:06

mtpultz


2 Answers

Yes, based on the RxJS subscribe() documentation there are 3 arguments that can be passed, last one being the OnCompleted callback.

var observer = Rx.Observer.create(
    function (x) {
        console.log('Next: %s', x);
    },
    function (err) {
        console.log('Error: %s', err);
    },
    function () {
        console.log('Completed');
    });
like image 107
Boanta Ionut Avatar answered Nov 14 '22 16:11

Boanta Ionut


A couple other options.

tap() and finalize()

These are both operators that you put in a pipe(...) - so the logic runs independent of the subscribe(...) call which you may not even be in your control.

finalize() is called on completion OR error

tap() has three parameters next, error, and complete that are called on those conditions

Finalize should probably be reserved for true cleanup type tasks.

like image 37
Simon_Weaver Avatar answered Nov 14 '22 16:11

Simon_Weaver