Last active
August 29, 2015 13:58
-
-
Save briancavalier/9982742 to your computer and use it in GitHub Desktop.
RedisClient and when/node.liftAll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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