made with esnextbin
Last active
August 26, 2016 19:41
-
-
Save dmackerman/53da52ddb8387f3151f7f5a0cf76f2a4 to your computer and use it in GitHub Desktop.
esnextbin sketch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>ESNextbin Sketch</title> | |
<!-- put additional styles and scripts here --> | |
</head> | |
<body> | |
<!-- put markup and other contents here --> | |
<button id="addStuff">Add Stuff</button> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.clear(); | |
// write ES2015 code and import modules from npm | |
// and then press "Execute" to run your program | |
import Rx from 'rxjs/Rx'; | |
import $ from 'jquery'; | |
// do some async shit | |
const performRequest = (term) => { | |
// add some intentional error failing | |
const requestUrl = term !== 'dudes' ? 'https://en.wikipedia.org/w/api.php' : 'failme.js'; | |
const doRequest = (url) => { | |
return $.ajax({ | |
url: requestUrl, | |
dataType: 'jsonp', | |
data: { | |
action: 'opensearch', | |
format: 'json', | |
search: encodeURI(term) | |
} | |
}).promise(); | |
}; | |
return Rx.Observable.of(requestUrl) | |
.flatMap(requestUrl => doRequest(requestUrl)) | |
.do(x => console.log(x)) | |
} | |
// initial requests | |
const requests = ['cool', 'dudes', 'fuck', 'wow', 'amazing']; | |
var requestQueue = new Rx.BehaviorSubject(requests) | |
.flatMap(term => term) | |
.concatMap(performRequest) | |
.retryWhen(errors => { | |
console.log('ERRORS', errors); | |
// return errors.delay(200); | |
}) | |
.map(res => { | |
return res; | |
}) | |
// .retry(3) | |
.subscribe( | |
next => console.log('onNext'), | |
err => console.log('onError'), | |
() => console.log('onComplete') | |
); | |
document.getElementById('addStuff').addEventListener('click', () => { | |
requestQueue.next(new Date() * 1000); | |
// kill the observable | |
requestQueue.complete(); | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"jquery": "3.1.0", | |
"rxjs": "5.0.0-beta.11" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _Rx = require('rxjs/Rx'); | |
var _Rx2 = _interopRequireDefault(_Rx); | |
var _jquery = require('jquery'); | |
var _jquery2 = _interopRequireDefault(_jquery); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
console.clear(); | |
// write ES2015 code and import modules from npm | |
// and then press "Execute" to run your program | |
// do some async shit | |
var performRequest = function performRequest(term) { | |
// add some intentional error failing | |
var requestUrl = term !== 'dudes' ? 'https://en.wikipedia.org/w/api.php' : 'failme.js'; | |
var doRequest = function doRequest(url) { | |
return _jquery2.default.ajax({ | |
url: requestUrl, | |
dataType: 'jsonp', | |
data: { | |
action: 'opensearch', | |
format: 'json', | |
search: encodeURI(term) | |
} | |
}).promise(); | |
}; | |
return _Rx2.default.Observable.of(requestUrl).flatMap(function (requestUrl) { | |
return doRequest(requestUrl); | |
}).do(function (x) { | |
return console.log(x); | |
}); | |
}; | |
// initial requests | |
var requests = ['cool', 'dudes', 'fuck', 'wow', 'amazing']; | |
var requestQueue = new _Rx2.default.BehaviorSubject(requests).flatMap(function (term) { | |
return term; | |
}).concatMap(performRequest).retryWhen(function (errors) { | |
console.log('ERRORS', errors); | |
// return errors.delay(200); | |
}).map(function (res) { | |
return res; | |
}) | |
// .retry(3) | |
.subscribe(function (next) { | |
return console.log('onNext'); | |
}, function (err) { | |
return console.log('onError'); | |
}, function () { | |
return console.log('onComplete'); | |
}); | |
document.getElementById('addStuff').addEventListener('click', function () { | |
requestQueue.next(new Date() * 1000); | |
// kill the observable | |
requestQueue.complete(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment