简体   繁体   English

RxJS Observables-发出任何新值时调用函数

[英]RxJS Observables - Call function when any new value emitted

I have following the observable which sucesfully obtains JSON data from a firebase DB: 我遵循了从firebase DB成功获取JSON数据的observable:

this.http.get('https://testdb.firebaseio.com/.json').map(res => res.json()).subscribe(result => {
  // do something with result
});

I want to call a function, foo() , inside the observable when any new value arrives after the first pull from the database. 我想第一次从数据库中提取任何 ,在observable中调用一个函数foo() To be clear, I want foo() only when a value arrives "later". 需要明确的是,我仅在值到达“稍后”时才需要foo()

For instance: 例如:

this.http.get('https://testdb.firebaseio.com/.json').map(res => res.json()).subscribe(result => {

  // Only called when a new value arrives in the stream later, not on the first pull when the stream is first activated.
  foo();

});

By later, I mean whenever the subscribed database is updated. 稍后,我的意思是每当订阅的数据库更新时。 I also don't need the actual value that was updated, I just want to trigger a function (which takes no parameters) when any new value arrives. 我也不需要更新的实际值,我只想在任何新值到达时触发一个函数(不带参数)。

I've no experience with firebase but if you want to skip the very first value, then yourObservable.skip(1) will do it. 我没有使用firebase的经验,但是如果您想跳过第一个值,那么yourObservable.skip(1)会做到。 Where yourObservable could be your data source ie: yourObservable可能是您的数据源,即:

this.http.get('https://testdb.firebaseio.com/.json')
.map(res => res.json())
.skip(1)
.subscribe((result) => foo();)

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

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