Skip to content

Instantly share code, notes, and snippets.

@threepointone
threepointone / gist:43f16389fd96561a8b0b
Last active February 13, 2023 02:12
sto alternate api
// store.js
let {store, handler} = sto(initialState, reduceFn); // where reduceFn: function(currentState, action, ...args){}
dispatcher.register(handler);
export store;
// elsewhere
store.get() // -> current state
store.toObservable() // -> to be used with .observe()
"use strict";
var [BRA, KET, IDENT] = ['BRA', 'KET', 'IDENT'];
function last(arr){ return arr[arr.length -1] };
export default function act(src, prefix){
var tree = src.split('').reduce((tokens, char)=> {
if(char==='{'){
tokens.push({type: BRA});
@threepointone
threepointone / csp.js
Last active February 25, 2017 16:15
parallel / series requests with js-csp
var csp = require('js-csp'),
{ chan, putAsync, take, go, put, timeout, spawn} = csp,
request = require('superagent');
var urls = ['http://www.google.com', 'http://www.jlongster.com', 'http://www.myntra.com'];
go(function*(){
// 1. do a bunch of requests in parallel, and save their response lengths
var parallel = yield map(urls, function*(url){
return (yield fetch(url)).text.length;
@threepointone
threepointone / ionode.diff
Created January 20, 2015 20:55
io.js compatibility
diff --git a/src/clj/cljs/repl/node_repl.js b/src/clj/cljs/repl/node_repl.js
index 6bc1cd2..46e7f7e 100644
--- a/src/clj/cljs/repl/node_repl.js
+++ b/src/clj/cljs/repl/node_repl.js
@@ -1,6 +1,7 @@
process.env.NODE_DISABLE_COLORS = true;
-var net = require("net");
+var net = require("net"),
+ vm = require('vm');
@threepointone
threepointone / gist:26dffd540c9e438c466f
Created January 14, 2015 21:46
jade -> json trees
var _ = require('underscore'),
Parser = require('jade/lib/parser');
module.exports = {
toJSON: toJSON,
fromJSON: fromJSON
}
function toJSON(src, options) {
var parser = new Parser(src),
@threepointone
threepointone / oia_csp.js
Last active March 22, 2016 10:19
an implementation of rob pike's parallel search slide in oia
oia(lets [go chan put take timeout alts] (require 'js-csp/lib/csp') (do
(fn fake [kind]
(fn [c query]
(go (gen []
(yield (take (timeout (js Math.random()*200))))
(yield (put c [kind query]))))))
(def web1 (fake :web1))
(def web2 (fake :web2))
@threepointone
threepointone / store.js
Created July 21, 2014 06:00
simple stores for flux
var util = require('util'),
_ = require('underscore'),
EventEmitter = require('events').EventEmitter
function Store(ctx, initial) {
this.state = initial || {};
EventEmitter.call(this);
this.initialize.apply(this, arguments);
}
var _ = require('underscore');
var domready = require('domready'),
bean = require('bean'), // events
bonzo = require('bonzo'), // DOM wrapper/manipulation
qwery = require('qwery'), // css selectors
morpheus = require('morpheus'),
slice = Array.prototype.slice;
// Attach events API to the prototype of DOM wrapper:
@threepointone
threepointone / project.js
Last active March 22, 2016 10:20
project: Facebook's reconcilliation algorithm in regular js, applied to dom nodes License: MIT
"use strict";
var _ = require('underscore'),
slice = [].slice;
var mutations = {
append: append,
remove: remove,
replace: replace,
setAttr: setAttr
@threepointone
threepointone / gist:4134486
Created November 23, 2012 08:14
compound tasks
k.task('a', function(done){ done(); });
k.task('b', function(done){ done(); });
k.task('c', function(done){
this.D.a().b().then(done);
});
k().D.c().then(function(){
console.log('done!');