made with esnextbin
Last active
August 11, 2016 18:59
-
-
Save tusharmath/593e6d3ac79c5b9eaf576c243b256ef8 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 | |
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)) |
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'); | |
// 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