Skip to content

Instantly share code, notes, and snippets.

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="https://code.jquery.com/jquery-2.1.3.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/rxjs/2.3.25/rx.all.js"></script>
<script src="script.js"></script>
</head>
@pchi
pchi / rx-shortpoll.js
Last active June 27, 2019 15:19
Short polling until predicate is met with RXJS
function getWikipediaSearchResults(term) {
return Rx.Observable.create(function forEach(observer) {
var cancelled = false;
var url = 'http://en.wikipedia.org/w/api.php?action=opensearch&format=json&search='
+ encodeURIComponent(term) + '&callback=?';
$.getJSON(url, function(data) {
if (!cancelled) {
observer.onNext(data[1]);
observer.onCompleted();
}
@pchi
pchi / eventStream.js
Created January 30, 2016 11:21
chaining observables with Rx
// watch angular scope $destroy expression and create a kill observable
var controllerScopeIsDestroyed = angularObservableSvc.watchToObservable($scope, '$destroy');
// get datamart status request
var statusObs = dataMartSvc.getSavedState({
userId: $stateParams.id,
refId: $stateParams.ref
});
// get custom views request
@pchi
pchi / poll.js
Created January 30, 2016 11:22
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)
@pchi
pchi / test2.js
Created August 30, 2016 15:36
attempt to move dupes to end of list
const someCollection = [{
results: [
{
name: 'alpha',
region : {
region_id : 1234
}
},
{
name: 'beta',