What you need is a subject of some sort. ReplaySubject
, BehaviorSubject
, Subject
, etc.
Create a subject, then you can do subject.subscribe(...)
to subscribe to it. You can also do subject.onNext(...)
to add to the stream.
For example:
var subject = new Rx.Subject();
var subscription = subject.subscribe(
function (x) { console.log('onNext: ' + x); },
function (e) { console.log('onError: ' + e.message); },
function () { console.log('onCompleted'); }
);
subject.onNext(1);
// => onNext: 1
subject.onNext(2);
// => onNext: 2
subject.onCompleted();
// => onCompleted
subscription.dispose();
A more specific use case (will add to the observable stream every time a successful HTTP response comes back):
var httpResponseStream = new Rx.Subject();
var subscription = httpResponseStream.subscribe(function (response) {
console.log('HTTP response success: ', response);
});
makeAJAXCall().then(function (response) {
httpResponseStream.onNext(response);
});
As another user stated, make sure you change all onNext
's to next
if you are using V5. If you are using V4, stick to onNext
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…