简体   繁体   English

rxjs中do和subscribe之间的区别

[英]Difference between do and subscribe in rxjs

In this JS Bin I have some observables that load a mocked paginated data source. 在这个JS Bin中,我有一些可加载模拟的分页数据源的observable。 getPageFromServer loads a page of data from the server starting at the given index: getPageFromServer从给定索引开始从服务器加载一页数据:

function getPageFromServer( index ) {

  index = index || 0;

  var values = [];
  var nextIndex;

  for(var i = 0; i< 3; i++) {
    var newValue = index + i;
    if(newValue < 10){
      values.push( newValue );
      nextIndex = newValue + 1;
    }
    else {
      nextIndex = undefined;
    }
  }

  data = { values: values, nextIndex: nextIndex };

  return Rx.Observable.return(data).delay(500);
}

getPagedItems then loads the pages and concats the multiple observables into one so that we just get an observable of all the values from all the pages: 然后getPagedItems加载页面并将多个observable连接成一个,这样我们就可以获得所有页面中所有值的可观察值:

function getPagedItems(index) {
  return getPageFromServer(index)
    .flatMap(function (response) {
      var result = Rx.Observable.from(response.values);

      if(response.nextIndex !== undefined){
        return result.concat( getPagedItems(response.nextIndex) );
      } else {
        return result;
      }

  });
}

This gives me my expected result: 这给了我预期的结果:

"page received: 1"
"page received: 2"
"page received: 3"
"page received: 4"
"page received: 5"
"page received: 6"
"page received: 7"
"page received: 8"
"page received: 9"
"complete"

However if I add a do to the end of getPagedItems: 但是,如果我在getPagedItems的末尾添加一个do:

function getPagedItems(index) {
  return getPageFromServer(index)
    .flatMap(function (response) {
      var result = Rx.Observable.from(response.values);

      if(response.nextIndex !== undefined){
        return result.concat( getPagedItems(response.nextIndex) );
      } else {
        return result;
      }
  })
  .do( function ( result) { console.log( "do: " + result); } );
}

I get each value the number of times equal to the number of observables we have created in the flatmap: 我得到每个值的次数等于我们在flatmap中创建的可观察数量:

"starting..."
"do: 0"
"page received: 0"
"do: 1"
"page received: 1"
"do: 2"
"page received: 2"
"do: 3"
"do: 3"
"page received: 3"
"do: 4"
"do: 4"
"page received: 4"
"do: 5"
"do: 5"
"page received: 5"
"do: 6"
"do: 6"
"do: 6"
"page received: 6"
"do: 7"
"do: 7"
"do: 7"
"page received: 7"
"do: 8"
"do: 8"
"do: 8"
"page received: 8"
"do: 9"
"do: 9"
"do: 9"
"do: 9"
"page received: 9"
"complete"

Please explain to me why "do" is firing multiple times but "onNext" in the subscription is only firing once. 请向我解释为什么“do”多次触发,但订阅中的“onNext”只触发一次。

Many Thanks 非常感谢

I figured out this answer about 5 minutes after I posted the question! 在我发布问题后约5分钟,我想出了这个答案!

The reason that the do fires multiple times is because I am calling the loadCommentThreads recursively so each nested call to loadComment threads has a do in the pipeline before the message returns to the parent calling function instance. do多次触发的原因是因为我以递归方式调用loadCommentThreads,因此每个对loadComment线程的嵌套调用在消息返回到父调用函数实例之前都在管道中有do。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM