有没有办法让RxJ创建一个Observable / Observer对象,它具有接收事件和发出事件的逻辑,类似于Rx.Subject
,除非没有组合Observable和Observer,所以当从内部发出事件时Observable的主体,它们不由Observer身体功能处理。
这样的事情:
var someSystem = /* some system that receives and emits events */;
function someFn() {
return ObservableObserver((observer, observable) => {
observable.subscribe((e) => someSystem.emit(e));
someSystem.on("data", (d) => observer.onNext(d));
});
}
const mySystem = someFn();
mySystem.subscribe(
(e) => {
console.log("Received from 'someSystem':", e);
mySystem.onNext("sending this to 'someSystem'");
}
);
修改
我相信我要找的东西类似于Node.js Duplex Stream:
https://nodejs.org/api/stream.html#stream_class_stream_duplex
答案 0 :(得分:2)
如果我遵循您的要求,您可以使用Subject#create
方法:
function someFn() {
return Subject.create(
Observer.create((e) => someSystem.emit(e)),
Observable.fromEvent(someSystem, 'data')
);
}
const mySystem = someFn()
source.subscribe(mySystem);
mySystem.subscribe((e) => {
console.log("Received from 'someSystem':", e);
});