【问题标题】:RxJS compare to last and emitRxJS 与 last 比较并发出
【发布时间】:2015-12-05 17:43:13
【问题描述】:

我对 ReactiveJS 比较陌生,我想实现以下目标:

只有在时间戳(updated_at)与上一个不同时才发出一个值(对象)。

var checkJobsRx = new Rx.Subject();
checkJobsRx
.dosomethinghere()
.subscribe(function (data) {})

【问题讨论】:

标签: javascript reactive-programming rxjs


【解决方案1】:

请阅读有关 distinctUntilChanged() 的文档。 http://reactivex.io/documentation/operators/distinct.html

我猜下面的代码就是问题的答案。(只添加了 1 行)

var checkJobsRx = new Rx.Subject();
  checkJobsRx
 .dosomethinghere()
 .distinctUntilChanged(function(job) { return job.updated_at })
 .subscribe(function (data) {})

【讨论】:

【解决方案2】:

您可以使用distinctUntilChanged 来检查发出的重复值:

checkJobsRx
  .map(fooBar)
  .distinctUntilChanged((item) => item.updated_at)
  .subscribe((data) => { })

查看the documentation

【讨论】:

  • 这是一个小字,但distinctUntilChanged 会更接近a different timestamp (updated_at) from the LAST one. 的要求。 distinct 过滤掉重复出现的值,与它们是否立即连续出现无关。
  • 这正是我不久前更新答案的原因:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-08
  • 2017-12-14
相关资源
最近更新 更多