Skip to content

Instantly share code, notes, and snippets.

@tusharmath
Last active August 11, 2016 18:59
Show Gist options
  • Save tusharmath/593e6d3ac79c5b9eaf576c243b256ef8 to your computer and use it in GitHub Desktop.
Save tusharmath/593e6d3ac79c5b9eaf576c243b256ef8 to your computer and use it in GitHub Desktop.
esnextbin sketch
<!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>
// write ES2015 code and import modules from npm
// and then press "Execute" to run your program
console.clear()
import {Observable as O} from 'rx'
function RAFThrottle(source) {
return O.create(observer => {
let value
let canUpdate = false
function update () {
if (canUpdate) {
observer.onNext(value)
canUpdate = false
requestAnimationFrame(() => canUpdate = true)
} else {
console.log('-', value)
}
}
requestAnimationFrame(() => canUpdate = true)
source.subscribe(x => {
value = x
update()
},
err => observer.onError(err),
() => observer.onCompleted()
)
})
}
const t = O.interval(1).take(100)
RAFThrottle(t).subscribe(x => console.log('+', x))
{
"name": "esnextbin-sketch",
"dependencies": {
"rx": "2.3.0"
},
"version": "0.0.0"
}
'use strict';
var _rx = require('rx');
// write ES2015 code and import modules from npm
// and then press "Execute" to run your program
console.clear();
function RAFThrottle(source) {
return _rx.Observable.create(function (observer) {
var value = void 0;
var canUpdate = false;
function update() {
if (canUpdate) {
observer.onNext(value);
canUpdate = false;
requestAnimationFrame(function () {
return canUpdate = true;
});
} else {
console.log('-', value);
}
}
requestAnimationFrame(function () {
return canUpdate = true;
});
source.subscribe(function (x) {
value = x;
update();
}, function (err) {
return observer.onError(err);
}, function () {
return observer.onCompleted();
});
});
}
var t = _rx.Observable.interval(1).take(100);
RAFThrottle(t).subscribe(function (x) {
return console.log('+', x);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment