Skip to content

Instantly share code, notes, and snippets.

@pchi
Created January 30, 2016 11:22
Show Gist options
  • Save pchi/d47ee357233e09941a31 to your computer and use it in GitHub Desktop.
Save pchi/d47ee357233e09941a31 to your computer and use it in GitHub Desktop.
polling with rx
/*
* create data STREAM
* using RX observable timer which allows a starts with time plus interval vs,
* flatMapLatest will always merge the last value from stream,
* so we don't create multiple observers which are hanging in limbo
* give me the first observable that satisfies our predicate, which then unsubscribes OR
* unsubscribe after elapsed time of 1 minute
*/
function pollData(dataMart) {
return Rx.Observable.timer(2000, 15000)
.flatMapLatest(() => _getDMStatus(dataMart))
.first(response => response.data.resultset.length > 0)
.takeUntilWithTime(60000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment