Skip to content

Instantly share code, notes, and snippets.

View benlesh's full-sized avatar
🐶
☕ 🔥 🔥 this is fine

Ben Lesh benlesh

🐶
☕ 🔥 🔥 this is fine
View GitHub Profile
var Observable = Rx.Observable;
// get a stream of keypresses (or even keyups)
var keypresses = Observable.fromEvent(myinput, 'keypress');
var subscription;
// throttled to every 500ms
subscription = keypresses.throttle(500).
// to a stream of values
map(function() {
@benlesh
benlesh / example.hbs
Created April 28, 2015 21:48
Car model update thing for jamesarosen
<h3>{{carMake}}</h3>
{{input value=model.car}}
<button {{action 'saveCar' model.car}}>Save</button>
@benlesh
benlesh / happy-promises.js
Last active February 5, 2016 00:59
I reject your reality
/**
* Running this file in Node (>4) will demonstrate that unhandled exceptions in promises will
* cause the process to continue running in a happy way, logging all 3 numbers.
* */
setTimeout(function () {
console.log(3);
}, 100);
console.log(1);
new Promise(function () {
@benlesh
benlesh / esnextbin.md
Created May 19, 2016 18:44
esnextbin sketch
@benlesh
benlesh / path-evaluation.js
Last active November 3, 2016 20:25
Primitive path evaluation implementation with refs
console.clear();
const path = ["list", 0];
const path0 = ["list", 20, 'name']; // ref ref
const path1 = ["list", [0, 1], "name"];
const path2 = ["list", [0, 1, { from: 3, length: 2 }], "name"];
const path3 = ["list", [0, 1, { from: 3, length: 2 }, { from: 7, to: 9 }], "name"];
const path4 = ["list", { to: 5 }, ["name", "rating"]];
const jsonGraph = {
// An observable that emits 10 multiples of 100 every 1 second
const source$ = Observable.interval(1000)
.take(10)
.map(x => x * 100);
/**
* returns a promise that waits `ms` milliseconds and emits "done"
*/
function promiseDelay(ms) {
return new Promise(resolve => {
setTimeout(() => resolve('done'), ms);
// An observable that emits 10 multiples of 100 every 1 second
const source$ = Observable.interval(1000)
.take(10)
.map(x => x * 100);
/**
* returns a promise that waits `ms` milliseconds and emits "done"
*/
function promiseDelay(ms) {
return new Promise(resolve => {
setTimeout(() => resolve('done'), ms);
function getErroringPromise() {
console.log('getErroringPromise called');
return Promise.reject(new Error('sad'));
}
Observable.defer(getErroringPromise)
.retry(3)
.subscribe(x => console.log);
// logs "getErroringPromise called" 4 times (once + 3 retries), then errors
Observable.defer(async function() {
const a = await promiseDelay(1000).then(() => 1);
const b = a + await promiseDelay(1000).then(() => 2);
return a + b + await promiseDelay(1000).then(() => 3);
})
.subscribe(x => console.log(x)) // logs 7
const click$ = Observable.fromEvent(button, 'clicks');
/**
* Waits for 10 clicks of the button
* then posts a timestamp of the tenth click to an endpoint
* using fetch
*/
async function doWork() {
await click$.take(10)
.forEach((_, i) => console.log(`click ${i + 1}`));
return await fetch(