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.
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
- rx.js
Creates a subject that can only receive one value and that value is cached for all future observations.
var subject = new Rx.AsyncSubject();
subject.onNext(42);
subject.onCompleted();
var subscription = source.subscribe(
function (x) {
console.log('Next: ' + x);
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});
// => Next: 42
// => Completed
- rx.js
Unsubscribe all observers and release resources.
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
- rx.js
Indicates whether the subject has observers subscribed to it.
(Boolean): Returns true
if the AsyncSubject has observers, else false
.
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
- rx.js