Skip to content

Instantly share code, notes, and snippets.

View briancavalier's full-sized avatar

Brian Cavalier briancavalier

  • Pittsburgh
View GitHub Profile
@briancavalier
briancavalier / most-ws.js
Created September 12, 2014 22:29
Simple two-way websocket with most.js ... this could become the basis for a most-ws package that provides a fromWebSocket() adapter
var most = require('most');
module.exports = function() {
var ws = new WebSocket('ws://localhost:8080');
var s = most.create(function(add, end, error) {
ws.addEventListener('open', onOpen);
module.exports = captureDefines;
function captureDefines (amdEval) {
return function (load) {
var result, isAnon, _define;
result = { named: [] };
_define = function captureDefine () {
var args, def;
@briancavalier
briancavalier / ambiguous-race.js
Last active August 29, 2015 14:07
Promise.race answers differently with same inputs, depending on when it is called.
// Use native or try any of these promise impls.
// They all behave the same.
//var Promise = require('when').Promise;
//var Promise = require('rsvp').Promise;
//var Promise = require('bluebird');
// Here are 2 promises, p1 and p2. p2 always *actually* finishes
// first, since p1 finishes in 1 millisecond, and p2 is already
// finished when created
function logWinner (p1, p2) {
Promise.race([p1, p2]).then(console.log.bind(console));
}
var p1 = new Promise(function(resolve) {
setTimeout(function() { resolve('p1'); }, 20);
});
var p2 = new Promise(function(resolve) {
setTimeout(function() { resolve('p2'); }, 10);
var most = require('most');
var connect = { then: function(f) { f(); } };
function getResource() {
return most.fromPromise(connect).flatMap(function() {
return getSnapshot().flatMap(function() {
return getUpdates();
});
});
@briancavalier
briancavalier / unhandledRejection.js
Created October 27, 2014 17:17
Aliasing console
/** @license MIT License (c) copyright 2010-2014 original author or authors */
/** @author Brian Cavalier */
/** @author John Hann */
(function(define) { 'use strict';
define(function(require) {
var setTimer = require('../env').setTimer;
return function unhandledRejection(Promise) {
/** @license MIT License (c) copyright 2010-2014 original author or authors */
/**
* Licensed under the MIT License at:
* http://www.opensource.org/licenses/mit-license.php
* @author: Brian Cavalier
*/
var when = require('when');
function Transaction(runTransaction) {
@briancavalier
briancavalier / 0-test.js
Created November 3, 2014 01:09
when.map perf
var when = require('../../../when');
var a = [];
for(var i=0; i<1000000; ++i) {
a[i] = when.resolve(i);
}
var start = Date.now();
when.map(a, function(x) {
return add(x, 1);
@briancavalier
briancavalier / 0-reduce.js
Created November 3, 2014 02:30
Improve reduce perf
var when = require('../../../when');
var a = [];
for(var i=0; i<1000000; ++i) {
a[i] = when.resolve(i);
}
var start = Date.now();
when.reduce(a, add, 0).done(function(r) {
//when.reduceRight(a, add, 0).done(function(r) {
@briancavalier
briancavalier / 0-reduce-no-sparse-array.js
Last active August 29, 2015 14:08
Improve reduce perf, non-sparse arrays
// Dropping sparse array support makes this code much simpler
function reduce(promises, f) {
var hasArg = arguments.length > 2;
if((promises.length>>>0) === 0) {
return hasArg ? when.promise(arguments[2]) : when.reject(new TypeError('Cannot reduce empty array with no initial value'));
}
return hasArg ? runReduce(0, promises, f, arguments[2])
: runReduce(1, promises, f, promises[0]);
}