Back to Rxjs

This is RxJS v 4. Find the latest version here

doc/api/subjects/asyncsubject.md

4.1.03.9 KB
Original Source

This is RxJS v 4. Find the latest version here

Rx.AsyncSubject class

Represents the result of an asynchronous operation. The last value before the OnCompleted notification, or the error received through OnError, is sent to all subscribed observers.

This class inherits both from the Rx.Observable and Rx.Observer classes.

Usage

The following example shows caching on the last value produced when followed by an onCompleted notification which makes it available to all subscribers.

js
var subject = new Rx.AsyncSubject();

var i = 0;
var handle = setInterval(function () {
	subject.onNext(i);
	if (++i > 3) {
		subject.onCompleted();
		clearInterval(handle);
	}
}, 500);

var subscription = subject.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Next: 3
// => Completed

Location

  • rx.js

AsyncSubject Constructor

AsyncSubject Instance Methods

Inherited Classes

AsyncSubject Constructor

<a id="rxasyncsubject"></a>Rx.AsyncSubject()

<a href="#rxasyncsubject">#</a>

Creates a subject that can only receive one value and that value is cached for all future observations.

Example

js
var subject = new Rx.AsyncSubject();

subject.onNext(42);
subject.onCompleted();

var subscription = subject.subscribe(
    function (x) {
        console.log('Next: ' + x);
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Next: 42
// => Completed

Location

  • rx.js

AsyncSubject Instance Methods

<a id="rxasyncsubjectprototypedispose"></a>Rx.AsyncSubject.prototype.dispose()

<a href="#rxasyncsubjectprototypedispose">#</a>

Unsubscribe all observers and release resources.

Example

js
var subject = new Rx.AsyncSubject();

var subscription = subject.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

subject.onNext(42);
subject.onCompleted();

// => Next: 42
// => Completed

subject.dispose();

try {
	subject.onNext(56);
} catch (e) {
	console.log(e.message);
}

// => Object has been disposed

Location

  • rx.js

<a id="rxasyncsubjectprototypehasobservers"></a>Rx.AsyncSubject.prototype.hasObservers()

<a href="#rxasyncsubjectprototypehasobservers">#</a>

Indicates whether the subject has observers subscribed to it.

Returns

(Boolean): Returns true if the AsyncSubject has observers, else false.

Example

js
var subject = new Rx.AsyncSubject();

console.log(subject.hasObservers());

// => false

var subscription = subject.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

console.log(subject.hasObservers());

// => true

Location

  • rx.js