This is RxJS v 4. Find the latest version here
Represents an object that is both an observable sequence as well as an observer. Each notification is broadcasted to all subscribed observers.
This class inherits both from the Rx.Observable
and Rx.Observer
classes.
The following example shows the basic usage of an Rx.Subject.
var subject = new Rx.Subject();
var subscription = subject.subscribe(
function (x) {
console.log('Next: ' + x.toString());
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});
subject.onNext(42);
// => Next: 42
subject.onNext(56);
// => Next: 56
subject.onCompleted();
// => Completed
- rx.js
Creates a subject.
var subject = new Rx.Subject();
var subscription = subject.subscribe(
function (x) {
console.log('Next: ' + x);
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});
subject.onNext(42);
// => Next: 42
subject.onCompleted();
// => Completed
- rx.js
Creates a subject from the specified observer and observable.
observer
(Observer): The observer used to send messages to the subject.observable
(Observable): The observable used to subscribe to messages sent from the subject.
(Subject): Subject implemented using the given observer and observable.
/* Using a Web Worker to send and receive data via an Rx.Subject */
/* worker.js */
self.onmessage = function(e) {
self.postMessage(e.data);
};
/* client.js */
var worker = new Worker('worker.js');
// Create observer to handle sending messages
var observer = Rx.Observer.create(
function (data) {
worker.postMessage(data);
});
// Create observable to handle the messages
var observable = Rx.Observable.create(function (obs) {
worker.onmessage = function (data) {
obs.onNext(data);
};
worker.onerror = function (err) {
obs.onError(err);
};
return function () {
worker.close();
};
});
var subject = Rx.Subject.create(observer, observable);
var subscription = subject.subscribe(
function (x) {
console.log('Next: ' + x);
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});
subject.onNext(42);
// => Next: 42
- rx.js
Unsubscribe all observers and release resources.
var subject = new Rx.Subject();
var subscription = subject.subscribe(
function (x) {
console.log('Next: ' + x.toString());
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});
subject.onNext(42);
// => Next: 42
subject.onCompleted();
// => 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 Subject has observers, else false
.
var subject = new Rx.Subject();
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