Skip to content

Instantly share code, notes, and snippets.

View rcastillo's full-sized avatar

Roger Castillo rcastillo

View GitHub Profile
@rcastillo
rcastillo / gist:2679582
Created May 13, 2012 04:14
Wrap a callback in timeout - node.js
/**
* Wraps a callback in a timeout timer
*/
function callbackWithTimeout(callback, delay) {
// light the fuse
var timeoutId = setTimeout(function(){
timedCallback('timeout', null);
}, delay);
var timedCallback = function(err, result) {
@rcastillo
rcastillo / gist:2632281
Created May 8, 2012 03:17
socket.io Jasmine helpers for creating test socket.io server and client
var _ = require('underscore'),
SocketIOClient = require('socket.io-client'),
liveSocket = require('../lib/live-socket');
exports.setupClient = function(opts) {
return function() {
opts = opts || {};
_.defaults(opts, {
url: 'http://localhost:8000',
options: {
@rcastillo
rcastillo / gist:2595882
Created May 4, 2012 16:22
server (client + server) websocket node.js
// (server) server-side
var io = require('socket.io').listen(8000);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('ping', function (data) {
console.log(data);
});
@rcastillo
rcastillo / gist:2560817
Created April 30, 2012 18:31
MockSocket class for BDD testing
var _ = require('underscore');
var idCounter = 0;
// Mock Socket Class used for asynchronous faking
var MockSocket = function() {
// sub-class to hook
// dictionary of message queues by msg
this.messageQueues = {};
this.subscriptions = {};
@rcastillo
rcastillo / crawl loop
Created December 10, 2011 17:23
Crawl Loop: sitecawl
function runCrawler(obs) {
var self = this;
self.crawling = true;
Rx.Observable.While(
function() {return self.crawling},
Rx.Observable.If(
function(){return self.crawlQueue.length > 0;},
Rx.Observable.Defer(function (){
var nextCrawlStep = self.crawlQueue.pop();
return self.selectForCrawlLinks(nextCrawlStep.Delay(self.delay))}),
@rcastillo
rcastillo / gist:1455565
Created December 10, 2011 16:49
callback hell
// pseudocode: ajax(request_object, callback)
ajax(a, function() {
ajax(b(a.somedata), function() {
ajax(c(b.somedata), function() {
c.finish()
})
})
})