Skip to content

Instantly share code, notes, and snippets.

@michiel
michiel / sequencer.coffee
Created May 17, 2011 13:33
Sequencer in coffeescript
sequence = (arr) ->
self = ->
if arr.length then arr.shift() self
self()
async = (callback) ->
console.log "Calling callback after 1s"
setTimeout callback, 1000
sequence [async, async, async]
@michiel
michiel / example-10k-sequence-pipeline.js
Created February 10, 2011 17:04
Run 10000 async calls through a pipeline'd sequencer
//
// Example #3 : sequence 10000 async calls in a pipeline, with max 500 open
//
function pipeline(arr, maxOpenCalls, finalCallback) {
var openCalls = 0;
function callback() {
if (--openCalls < maxOpenCalls) {
@michiel
michiel / example-sequence-10k-in-blocks-of-500.js
Created February 9, 2011 22:02
Run 10000 async calls through a sequencer in blocks of 500
//
// Example #2 : sequence 10000 async calls in blocks of 500 using a collector
//
function sequence(arr) {
var self = function() {
arr.length && arr.shift()(self);
}
self();
}
@michiel
michiel / example-sequence-10k.js
Created February 9, 2011 22:00
Run 10000 async calls through a sequencer
//
// Example #1 : sequence 10000 async calls
//
function sequence(arr) {
var self = function() {
arr.length && arr.shift()(self);
}
self();
Sequencer = function(lambdas) {
var self = this;
this.start = function() {
this.next();
}
this.next = function() {
if (lambdas.length != 0) {
var BLOCKSIZE = 500;
var dataset = []; // Assuming, of course, that there's something in here..
var renderActions = [];
for (var i=0; i<dataset.length; i+= BLOCKSIZE) {
renderActions.push((function(offset) {
return function(callback) {
//