rx data driven subwidgets

一曲冷凌霜 提交于 2019-12-12 18:28:01

问题


Following up on How to structure rxjs code, concerning how to structure a widget with subwidget when using rx, how would you structure rx code where the subwidgets are data-driven?

As a toy problem, suppose you have an external source (e.g. web service) streaming a list of stocks to watch, with value, high, low, etc. So you have an Observable stream like (time goes down):

[{id: 'csco', value: 12, high: 20, low: 10}]

[{id: 'csco', value: 12, high: 20, low: 8}, 
 {id: 'aapl', value: 29, high: 30, low: 20}]

You need to build a widget for each stock symbol. So the pattern in the previous question has to be modified. There's not one child, and a child is created/destroyed based on the input stream, e.g. we create a child for 'csco' on the 1st event. On the 2nd event the 'csco' child gets an update (low: 8) and we create a child for 'aapl'.

You could move the subwidget creation to the subscribe:

function widget(state) {
  state = state.share();
  var children = {};
  state.subscribe(function (s) {
    findNew(children, s).forEach(function (stock) {
       children[stock] = subwidget(state.select(findById.bind(null, stock)));
    });
    // ... delete old ones similarly
  });
}

This introduces an ordering problem: the child doesn't get the event that cause it to be created. You can work around this by doing something like state.repeat(1), or state.startsWith(s).select(...), but it looks a bit odd.

Suppose the children are also returning streams. E.g. maybe the user manipulates the children to model different positions, so we want to get some computation back from the children and display an overall total, or other metric, in widget.

Do you create a Subject in widget() and push on it the streams from the children? Like

   var positions = new Rx.Subject();
   positions.mergeAll().subscribe(displayTotal);
   ...
      .. subscribe(function(s) {
       child = subwidget(...)
       children[stock] = child.disposable; 
       positions.onNext(child.position);

This all seems pretty clunky. Is there a better way to organize it? I tried modeling it as a stream of streams, one per child, and merging the outputs. This worked poorly. It was hard to keep track of the inner subscriptions, or have multiple streams output from the children.

来源:https://stackoverflow.com/questions/22185030/rx-data-driven-subwidgets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!