From 889ffa129e7342ef7863eeb111fddb3ac3b727c8 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Mon, 27 Aug 2012 12:49:22 +0200 Subject: [PATCH] RedisStore can have its users (e.g connect-session) handle connection to Redis problems --- lib/connect-redis.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/connect-redis.js b/lib/connect-redis.js index 8f3b90b0..e8c593bb 100644 --- a/lib/connect-redis.js +++ b/lib/connect-redis.js @@ -42,6 +42,8 @@ module.exports = function(connect){ */ function RedisStore(options) { + var self = this; + options = options || {}; Store.call(this, options); this.prefix = null == options.prefix @@ -58,7 +60,6 @@ module.exports = function(connect){ this.ttl = options.ttl; if (options.db) { - var self = this; self.client.select(options.db); self.client.on("connect", function() { self.client.send_anyways = true; @@ -66,10 +67,17 @@ module.exports = function(connect){ self.client.send_anyways = false; }); } + + // Let users of the RedisStore handle connection to Redis issues if safe mode enabled + if (options.safeMode) { + self.client.on('error', function () { self.emit('connectionError'); }); + self.client.on('connect', function () { self.emit('connectionEstablished'); }); + } }; /** * Inherit from `Store`. + * Store is an EventEmitter so RedisStore is one too */ RedisStore.prototype.__proto__ = Store.prototype;