Javascript 中间可观察到的下一个()

Javascript 中间可观察到的下一个(),javascript,rxjs,reactive-programming,rxjs5,Javascript,Rxjs,Reactive Programming,Rxjs5,我是RxJS新手,所以我的术语可能不简洁,抱歉。我使用map()创建了派生的Observable,并希望它不断通过自身传递其源值,以及在此基础上的其他事件。例如: //receiving values from server: const $source = new Rx.Subject; //map from network representation to client one: const $client = $source.map( server => server.x + s

我是RxJS新手,所以我的术语可能不简洁,抱歉。我使用map()创建了派生的Observable,并希望它不断通过自身传递其源值,以及在此基础上的其他事件。例如:

//receiving values from server:
const $source = new Rx.Subject;

//map from network representation to client one:
const $client = $source.map( server => server.x + server.y );
//display on screen:
$client.subscribe( client => console.log( "client:", client ) )

//have input to update client-side representation:
const $button = new Rx.Subject;
$button.subscribe( $client );

$button.next( { x : 1, y : 2 } );

遗憾的是,它打印“3”而不是对象,就好像$button将事件直接发送到$source而不是$client。为什么$button.next(…)发射到$source而不是发射到$client?我希望操作符(map()在本例中)生成新流。如何实现仍然依赖于原始流的本地循环,但不修改原始流?提前感谢。

您看到的结果是预期的,而您试图实现的目标是不可能的

我期望一个操作符(本例中为map())生成新的流

这是正确的,但是新生成的流是
源$
的扩展,因此:

$client = $source + map
// this means any data injected into client$
// will walk through an instance of source$ and then through the map-function
我知道,这只是解释了行为,并没有提供一个“解决方案”-然而,为了正确地提供一个解决问题的好答案,你应该写一些关于你试图实现的东西-除非你想要的是理解为什么是这样


另外:它当前的结构看起来非常复杂,如果您提供一些关于用例的信息,我相信这可以简化。

添加中间主题($anotherSource)并将其与原始$source合并解决了这个问题:

//eternal values receive from server:
const $source = new Rx.Subject;
$source.subscribe( () => console.log( "Should not" ) );

const $anotherSource = new Rx.Subject;

//map from network representation:
const $client = $source.map( server => server.x + server.y ).merge( $anotherSource );
//display on screen:
$client.subscribe( client => console.log( "client:", client ) )

//have input to update client-side representation interleaving with server one:
const $button = new Rx.Subject;
$button.subscribe( $anotherSource );

$button.next( { x : 1, y : 2 } );

$client现在收到的是一个对象,而不是预期的“3”。

谢谢您的回答。我只想让用户从服务器一开始更改值,但任何时候服务器都会发送任何新值-重置用户看到的内容并继续从新值更改。好吧,但这就是您的流当前所做的-也许您应该看看您的
映射
-方法,您确定要添加
x
y
?也许这就是你的问题?请用以下的流图更新您的问题:什么数据源在何时发出哪些数据,以及您希望在流的末尾显示哪些数据。您对map()是原始Observable的扩展(无论是什么)的解释让我想出了解决方案。把它作为答案贴出来,谢谢。太棒了!!找到自己的解决方案永远是最好的体验:-)