我需要通过一个主题代理所有不同的事件流。
我想出了这段代码:
var mySubject,
getObservable;
getObservable = function (subject, eventName) {
return subject
.asObservable()
.filter(function (x) {
return x.EventName === eventName;
})
.flatMap(function (x) {
if (x.Type === 'onNext') {
return Rx.Observable.return(x.Data);
}
if (x.Type === 'onError') {
return Rx.Observable.throw(x.Data);
}
return Rx.Observable.empty();
});
};
mySubject = new Rx.Subject();
getObservable(mySubject, 'foo')
.subscribe(function(x){
console.log('foo onNext ' + x);
}, function(x){
console.log('foo onError ' + x);
}, function(){
console.log('foo onComplete');
});
getObservable(mySubject, 'bar')
.subscribe(function(x){
console.log('bar onNext ' + x);
}, function(x){
console.log('bar onError ' + x);
}, function(){
console.log('bar onComplete');
});
mySubject.onNext({Type: 'onNext', EventName: 'foo', Data: 5});
mySubject.onNext({Type: 'onCompleted', EventName: 'foo'});
mySubject.onNext({Type: 'onNext', EventName: 'bar', Data: 5});
mySubject.onNext({Type: 'onError', EventName: 'bar', Data: 'Error message'});
得到输出:
foo onNext 5
bar onNext 5
bar onError Error message
预期产出:
foo onNext 5
foo onCompleted
bar onNext 5
bar onError Error message
对于bar
事件,它就像一个魅力:onNext
将被传播,一旦错误引发,onError
函数被调用并且事件流结束。但是,我无法让它适用于onComplete
。
每当完整的通知提出时,我都会看到Rx.Observable.empty()
被调用,但这不会导致调用订阅者onComplete
。相反,它调用它的onNext
处理程序。
答案 0 :(得分:1)
getObservable
函数返回一个观察者,该观察者订阅通过eventName
发送的subject
个事件。
let getObservable = function (subject, eventName) {
return Rx.Observable.create(function (observer) {
subject
.asObservable()
.filter(function(x) {
return x.EventName === eventName;
})
.map(function(x) {
if (x.Type === 'onNext') {
observer.onNext(x.Data);
}
if (x.Type === 'onError') {
observer.onError(x.Data);
}
if (x.Type === 'onCompleted') {
observer.onCompleted();
}
return x;
})
.subscribe();
});
};
这是一个使用原始问题数据的工作示例:
var mySubject,
getObservable;
getObservable = function (subject, eventName) {
return Rx.Observable.create(function (observer) {
subject
.asObservable()
.filter(function(x) {
return x.EventName === eventName;
})
.map(function(x) {
if (x.Type === 'onNext') {
observer.onNext(x.Data);
}
if (x.Type === 'onError') {
observer.onError(x.Data);
}
if (x.Type === 'onCompleted') {
observer.onCompleted();
}
return x;
})
.subscribe();
});
};
mySubject = new Rx.Subject();
getObservable(mySubject, 'foo')
.subscribe(function(x){
console.log('SomethingHappened onNext ' + x);
}, function(x){
console.log('SomethingHappened onError ' + x);
}, function(){
console.log('SomethingHappened onComplete');
});
getObservable(mySubject, 'bar')
.subscribe(function(x){
console.log('DataUpdated onNext ' + x);
}, function(x){
console.log('DataUpdated onError ' + x);
}, function(){
console.log('DataUpdated onComplete');
});
mySubject.onNext({Type: 'onNext', EventName: 'foo', Data: 5});
mySubject.onNext({Type: 'onCompleted', EventName: 'foo'});
mySubject.onNext({Type: 'onNext', EventName: 'bar', Data: 5});
mySubject.onNext({Type: 'onError', EventName: 'bar', Data: 'Error message'});
<script src='https://rawgit.com/Reactive-Extensions/RxJS/master/dist/rx.all.js'></script>
答案 1 :(得分:0)
.SelectMany使用Observable.Merge将流合并到一个复合observable。 IMHO Observable.Merge仅在完成任何合并的observable时完成。
e.g。 http://theburningmonk.com/2010/02/rx-framework-iobservable-merge/
这可能是问题的原因。