Skip to content

Instantly share code, notes, and snippets.

Bruno Jouhier bjouhier

Block or report user

Report or block bjouhier

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bjouhier
bjouhier / fibug.js
Created Oct 3, 2014
Crashes node-fibers 1.0.2, at least on OSX
View fibug.js
var Fiber = require("./build/Release/fibers").Fiber;
function wrap(fn, idx) {
function F() {
var cb = arguments[idx];
var that = this,
args = arguments;
Fiber(function () {
var val, err = null;
@bjouhier
bjouhier / timedrift.js
Last active Apr 3, 2017
timedrift.js
View timedrift.js
"use strict";
function millis(hr) {
return hr[0] * 1000 + Math.floor(hr[1] / 1000000);
}
function busyWait(ms, cb) {
var hr0 = process.hrtime();
while (millis(process.hrtime(hr0)) < ms);
var elapsed = millis(process.hrtime(hr0));
@bjouhier
bjouhier / waitForOne._js
Last active Aug 29, 2015
waitForOne.js
View waitForOne._js
var galaxy = require('galaxy');
function *waitForOne(futures) {
return yield galaxy.star(function(cb) {
for (var i = 0; i < futures.length; i++) {
galaxy.unstar(futures[i], 0)(function(e, r) {
if (cb) cb(e, r);
cb = null;
});
}
View fibers.js
//fibers
function markComplete(postedItems, completionCallback) {
var errors = [];
var getItemToUpdate = function(item) {
var fiber = Fiber.current;
process.nextTick(function() {
client.queryEntity('tasks', 'partition1', item, function(err, task) {
fiber.run(task);
});
@bjouhier
bjouhier / binding.gyp
Created Nov 4, 2013
exception broken in osx addon
View binding.gyp
{
"targets": [
{
"conditions": [
["OS=='mac'", {
"xcode_settings": {
"GCC_ENABLE_CPP_EXCEPTIONS": "YES"
}
}],
],
@bjouhier
bjouhier / fibo.js
Last active Dec 26, 2015
paris.js 30-10-2013
View fibo.js
require('colors');
function* genFibo() {
var f1 = 0, f2 = 1;
while (true) {
console.log(("genFibo loop: " + f1).green)
yield f1;
var f = f1 + f2;
f1 = f2;
f2 = f;
@bjouhier
bjouhier / fiboCrash.js
Last active Dec 17, 2015
Demonstrates crash with V8 generators
View fiboCrash.js
function isGenerator(val) {
return Object.prototype.toString.call(val) === "[object Generator]";
}
var PENDING = {};
function run(fn, args, idx) {
var cb = args[idx],
g;
@bjouhier
bjouhier / fiboBench.js
Created May 10, 2013
First bench to compare raw callbacks and the 3 streamline modes: callbacks, fibers and generators
View fiboBench.js
var fs = require('fs');
function fib(n) {
return n <= 1 ? 1 : fib(n - 1) + fib(n - 2);
}
function rawBench(n, loop, modulo, asyncFn, callback) {
var count = 0;
var l = loop;
@bjouhier
bjouhier / testgen._js
Created May 9, 2013
streamlined harmony generators example based on https://gist.github.com/creationix/5544019
View testgen._js
var fs = require('fs');
function testgen(_) {
console.log(fs.readFile(__filename, _));
console.log("Sleeping for 2000ms...");
setTimeout(_, 2000);
console.log("Done");
}
testgen(_);
View wrappers._js
// Wrapping asynchronous functions
// This wrapper is implemented with callbacks
// i is the index of the callback in the parameter list
// isFuture is a reserved parameter - do not pass it
function wrapper1(fn, i, isFuture) {
return function wrapped() {
var cb = arguments[i];
if (!isFuture) console.log("WRAPPER1 BEFORE CALL: " + fn.name);
if (cb == null) return wrapper1(fn.apply(this, arguments), 0, true);
arguments[i] = function(err, result) {
You can’t perform that action at this time.