made with esnextbin
Last active
August 14, 2016 12:35
-
-
Save tusharmath/20a7dc58af331e8f91cb570aa97dc2f2 to your computer and use it in GitHub Desktop.
esnextbin sketch
This file contains 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 --> | |
</body> | |
</html> |
This file contains 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
// write ES2015 code and import modules from npm | |
// and then press "Execute" to run your program | |
import {Observable as O} from 'rx' | |
const log = (...t) => document.writeln(t.join('\t') + '<br/>') | |
function RAFThrottle (source) { | |
return O.create(observer => { | |
let queue = null | |
function queueValue (value) { | |
log('que', value) | |
if(queue) { | |
cancelAnimationFrame(queue) | |
} | |
queue = requestAnimationFrame(function () { | |
log('req', value) | |
observer.onNext(value) | |
}) | |
} | |
return source.subscribe( | |
queueValue, | |
err => observer.onError(err), | |
() => observer.onCompleted() | |
) | |
}) | |
} | |
const t= O.interval(1).take(10) | |
RAFThrottle(t).subscribe(x => log(x)) |
This file contains 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", | |
"dependencies": { | |
"rx": "2.3.0" | |
}, | |
"version": "0.0.0" | |
} |
This file contains 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('rx'); | |
var log = function log() { | |
for (var _len = arguments.length, t = Array(_len), _key = 0; _key < _len; _key++) { | |
t[_key] = arguments[_key]; | |
} | |
return document.writeln(t.join('\t') + '<br/>'); | |
}; // write ES2015 code and import modules from npm | |
// and then press "Execute" to run your program | |
function RAFThrottle(source) { | |
return _rx.Observable.create(function (observer) { | |
var queue = null; | |
function queueValue(value) { | |
log('que', value); | |
if (queue) { | |
cancelAnimationFrame(queue); | |
} | |
queue = requestAnimationFrame(function () { | |
log('req', value); | |
observer.onNext(value); | |
}); | |
} | |
return source.subscribe(queueValue, function (err) { | |
return observer.onError(err); | |
}, function () { | |
return observer.onCompleted(); | |
}); | |
}); | |
} | |
var t = _rx.Observable.interval(1).take(10); | |
RAFThrottle(t).subscribe(function (x) { | |
return log(x); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment