Skip to content

Instantly share code, notes, and snippets.

@briancavalier
Last active August 29, 2015 13:58
Show Gist options
  • Save briancavalier/9982742 to your computer and use it in GitHub Desktop.
Save briancavalier/9982742 to your computer and use it in GitHub Desktop.
RedisClient and when/node.liftAll
// Lift only the RedisClient prototype command methods
// to a new prototype so you can create many instances
var node = require('when/node');
var net = require('net');
var RedisClient = require('redis').RedisClient;
var host = '127.0.0.1';
var port = 6379;
var commands = require('redis/lib/commands');
function liftCommands(proto, f, n) {
if(commands.indexOf(n) >= 0) {
proto[n] = f;
}
return proto;
}
function PromisedRedisClient() {
RedisClient.apply(this, arguments);
}
PromisedRedisClient.prototype = node.liftAll(RedisClient.prototype, liftCommands);
var client = new PromisedRedisClient(net.connect(host, port));
client.port = port;
client.host = host;
client.get('foo').done(console.log);
// Lift a single redis client instance's command methods
// If you only ever need one instance, this might be a
// nice, simple alternative.
// Caveat: command methods become own methods of the instance
// rather than being inherited from the prototype. In practice
// it may not matter, but I haven't tested it.
var node = require('../node');
var net = require('net');
var redis = require('redis');
var RedisClient = redis.RedisClient;
var commands = require('redis/lib/commands');
function liftCommands(proto, f, n) {
if(commands.indexOf(n) >= 0) {
proto[n] = f;
}
return proto;
}
var client = node.liftAll(RedisClient.prototype, liftCommands, redis.createClient())
client.get('foo').done(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment