Skip to content

Instantly share code, notes, and snippets.

2e525e375740023525561b716918026c73560f312f194472695f5b631a6f0d7260540173754c436c
6d140e723611532a75175f3340131a3d3554423c201c57724056003b301c4421751d4a72505c137f
331142213004133f41401033271101323b141336565c1372220d01697c4c1c221a6f0d7260540173
754c436c6d5d4331210744732c1f4672535c0d3625060d7321185a21045e06213315463675194072
415d002039045536315046214d5d04722f1a44733a1613264c56433f2f07557325025a3f4d470a24
2554443d36024a22505a0c3c600044303d1e5a23515610722215523631505c3c044116362919443d
2111412b046f412a2f06483d322c11724554023b2e0755733450413d5052173b2e13013830091d72
6d13083c2f0301243d1147725d5c1675321101273d195d394d5d047e6016542775005f3745400672
241b4f74215047205d1302223018583a3b17133b5013173d601c48373050523c5d470b3b2e130130
2705503b455f4334321b4c732118567254411a3b2e1301362c1540724b55431c1335013c27504a3d
define(["jquery", "lodash", "bacon"], function($, _, Bacon){
return function(url, cb) {
var parseTimeCode = function (timeCodeStr) {
return _(timeCodeStr.split(/[:,]/))
.map(Number)
.zipWith([60 * 60 * 1000, 60 * 1000, 1000, 1], function(a, b){ return a * b; })
.reduce(_.add, 0);
};
@tweinfeld
tweinfeld / example.js
Last active May 28, 2016 23:11
A JSONP mixin for Lodash
_.jsonp('https://api.ipify.org/?format=jsonp', function(err, value){
console.log(err || value);
});
@tweinfeld
tweinfeld / concurrent_promise.js
Last active October 15, 2015 10:03
Limit execution concurrency of promise generation functions
let allConcurrent = function(limit, generators){
return new Promise(function(resolveAll){
let plan = generators.map((generator)=>({ value: undefined, executed: false, settled: false, generator: generator }));
let executeBatch = () => {
plan
.filter(({ settled, executed })=> !executed || (executed && !settled))
.slice(0, limit)
.filter(({ executed }) => !executed)
.map(planItem => {
planItem.executed = true;
@tweinfeld
tweinfeld / baconjs_buffering_debounce.js
Last active October 22, 2015 06:57
Buffering Debounce with Bacon
const QUEUE_TIMEOUT = 500,
QUEUE_DEBOUNCE = 5;
Bacon
.repeatedly(10, ["*", "-"])
.flatMapLatest((function(buffer, stamp) {
return function(request) {
buffer.length === 0 && (stamp = Date.now());
buffer.push(request);
return (stamp + QUEUE_TIMEOUT < Date.now()) ? Bacon.once(buffer.splice(0)) : Bacon.fromBinder(function(sink) {
function createLogger(writer = console.log.bind(console), bufferThreshold = 100, delay = 5000){
let buf = [], timer;
const dispose = ()=>{ writer(buf.splice(0)); };
return function(obj){
buf.push(obj);
clearTimeout(timer);
(buf.length >= bufferThreshold ? dispose() : timer = setTimeout(dispose, delay));
}
}
@tweinfeld
tweinfeld / docker_shell.js
Last active November 25, 2016 10:27
Lib for executing command-line on Docker images. Shares similar interface to "child_process".
const
_ = require('lodash'),
spawn = require('child_process').spawn,
bacon = require('baconjs');
const bufferUp = function(stream){
return bacon
.fromEvent(stream, 'data')
.takeUntil(bacon.fromEvent(stream, 'close').take(1))
.fold([], '.concat')
@tweinfeld
tweinfeld / 360_projector.js
Last active February 3, 2017 08:53
A rudimentary 360° video sphere projector. Receives a source element (i.e. <video/>) to read from and a destination <canvas/> to draw into.
define([
"jquery",
"lodash",
"three"
], function(
$,
_,
Three
){
var requestAnimationFrame = (window.requestAnimationFrame || _.noop).bind(window),
@tweinfeld
tweinfeld / generator_promise_yielder.js
Created February 4, 2017 22:09
Generator Promise Yielder
const run = function(gen, ...args){
let iterator = gen.apply(undefined, args);
return new Promise((resolve)=>{
const rep = function(previousValue){
let { done, value } = iterator[previousValue instanceof Error ? "throw" : "next"](previousValue);
!done ? ((value instanceof Promise ? value.then((v)=>rep(v)).catch((v)=>rep(new Error(v))) : rep(value))) : resolve(value);
}
rep();
})
};
@tweinfeld
tweinfeld / frp_react.js
Last active February 9, 2017 13:29
React without Redux or Cycle.js
const
[div, span, button] = ["div", "span", "button"].map((name)=>React.createFactory(name)),
myCounterComponent = function({ count, callback }){
return div({}, [],
button({ onClick: ()=>callback('decrease') }, ["-"]),
span({}, [count]),
button({ onClick: ()=>callback('increase') }, ["+"])
);
},
myCounterCombo = function({ state: { counter_1, counter_2 }, callback }){