Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Latest commit

 

History

History
170 lines (122 loc) · 3.86 KB

asyncsubject.md

File metadata and controls

170 lines (122 loc) · 3.86 KB

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.

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

Rx.AsyncSubject()

#

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

Example

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

Rx.AsyncSubject.prototype.dispose()

#

Unsubscribe all observers and release resources.

Example

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

Rx.AsyncSubject.prototype.hasObservers()

#

Indicates whether the subject has observers subscribed to it.

Returns

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

Example

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