Represents an object that is both an observable sequence as well as an observer. Each notification is broadcasted to all subscribed and future observers, subject to buffer trimming policies.
This class inherits both from the Rx.Observable
and Rx.Observer
classes.
The follow example shows the basic usage of an Rx.ReplaySubject
class. Note that this only holds the past two items in the cache.
var subject = new Rx.ReplaySubject(2 /* buffer size */);
subject.onNext('a');
subject.onNext('b');
subject.onNext('c');
var subscription = subject.subscribe(
function (x) {
console.log('Next: ' + x.toString());
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});
// => Next: b
// => Next: c
subject.onNext('d');
// => Next: d
- rx.binding.js
Initializes a new instance of the Rx.ReplaySubject
class with the specified buffer size, window and scheduler.
[bufferSize = Number.MAX_VALUE]
(Number): Maximum element count of the replay buffer.[windowSize = NUMBER.MAX_VALUE]
(Number): Maximum time length of the replay buffer.[scheduler = Rx.Scheduler.currentThread]
(Scheduler): Scheduler the observers are invoked on.
var subject = new Rx.ReplaySubject(
2 /* buffer size */,
null /* unlimited time buffer */,
Rx.Scheduler.timeout);
subject.onNext('a');
subject.onNext('b');
subject.onNext('c');
var subscription = subject.subscribe(
function (x) {
console.log('Next: ' + x.toString());
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});
// => Next: b
// => Next: c
subject.onNext('d');
// => Next: d
- rx.binding.js
Unsubscribe all observers and release resources.
var subject = new Rx.ReplaySubject();
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.binding.js
Indicates whether the subject has observers subscribed to it.
(Boolean): Returns true
if the Subject has observers, else false
.
var subject = new Rx.ReplaySubject();
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.binding.js