Skip to content

Commit

Permalink
use enhanced event emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
sagiegurari committed Jan 7, 2017
1 parent e3f836b commit 418e2a8
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ See [contributing guide](.github/CONTRIBUTING.md)

| Date | Version | Description |
| ----------- | ------- | ----------- |
| 2016-12-31 | v1.0.25 | Maintenance |
| 2017-01-07 | v1.0.26 | Maintenance |
| 2015-10-22 | v0.0.16 | Timeout child commands (see childCommandTimeout option) |
| 2015-10-16 | v0.0.12 | Maintenance |
| 2015-09-23 | v0.0.7 | Upgrade to redis 2.0 |
Expand Down
2 changes: 1 addition & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
| Date | Version | Description |
| ----------- | ------- | ----------- |
| 2016-12-31 | v1.0.25 | Maintenance |
| 2017-01-07 | v1.0.26 | Maintenance |
| 2015-10-22 | v0.0.16 | Timeout child commands (see childCommandTimeout option) |
| 2015-10-16 | v0.0.12 | Maintenance |
| 2015-09-23 | v0.0.7 | Upgrade to redis 2.0 |
Expand Down
32 changes: 9 additions & 23 deletions lib/multiple-redis.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

//load external modules
var events = require('events');
var util = require('util');
var EventEmitterEnhancer = require('event-emitter-enhancer');
var debug = require('debuglog')('multiple-redis');
var redis = require('redis');
var redisCommands = require('redis-commands');
Expand All @@ -28,7 +28,7 @@ function MultiRedisClient(params) {
var self = this;

//call super constructor
events.EventEmitter.call(self);
EventEmitterEnhancer.EnhancedEventEmitter.call(self);

var options = params.options || {};
self.childCommandTimeout = options.childCommandTimeout || 10000;
Expand Down Expand Up @@ -57,8 +57,8 @@ function MultiRedisClient(params) {
self.setupStateChangeEvents();
}

//setup MessageSocket as an event emitter
util.inherits(MultiRedisClient, events.EventEmitter);
//setup MultiRedisClient as an event emitter
util.inherits(MultiRedisClient, EventEmitterEnhancer.EnhancedEventEmitter);

/**
* True when at least one internal redis client is connected.
Expand Down Expand Up @@ -131,9 +131,7 @@ MultiRedisClient.prototype.initClients = function (params, options) {
* @private
*/
MultiRedisClient.prototype.setupEventProxy = function () {
var self = this;

[
this.proxyEvents(this.clients, [
'ready',
'connect',
'reconnecting',
Expand All @@ -148,15 +146,7 @@ MultiRedisClient.prototype.setupEventProxy = function () {
'psubscribe',
'unsubscribe',
'punsubscribe'
].forEach(function createProxy(event) {
self.clients.forEach(function onClientEvent(client) {
client.on(event, function onEvent() {
var argumentsArray = Array.prototype.slice.call(arguments, 0);
argumentsArray.unshift(event);
self.emit.apply(self, argumentsArray);
});
});
});
]);
};

/**
Expand All @@ -169,17 +159,13 @@ MultiRedisClient.prototype.setupEventProxy = function () {
MultiRedisClient.prototype.setupStateChangeEvents = function () {
var self = this;

var onStateChange = function () {
self.resetState();
};

[
self.onAny([
'connect',
'ready',
'error',
'end'
].forEach(function addListener(eventName) {
self.on(eventName, onStateChange);
], function runResetState() {
self.resetState();
});
};

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "multiple-redis",
"version": "1.0.25",
"version": "1.0.26",
"description": "Run redis commands against multiple redis instances.",
"author": {
"name": "Sagie Gur-Ari",
Expand Down Expand Up @@ -41,12 +41,12 @@
"dependencies": {
"async": "^2.1.4",
"debuglog": "^1.0.1",
"event-emitter-enhancer": "latest",
"redis": "^2.6.3",
"redis-commands": "^1.3.0"
},
"devDependencies": {
"chai": "latest",
"event-emitter-enhancer": "latest",
"grunt": "latest",
"grunt-cli": "latest",
"husky": "latest",
Expand Down
3 changes: 0 additions & 3 deletions test/spec/multiple-redis-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var assert = chai.assert;
var redis = require('redis');
var events = require('events');
var EventEmitter = events.EventEmitter;
var EventEmitterEnhancer = require('event-emitter-enhancer');
var MultipleRedis = require('../../');

var noop = function noop() {
Expand Down Expand Up @@ -394,8 +393,6 @@ describe('MultipleRedis Tests', function () {
assert.isFalse(client.connected);
assert.isNull(client.server_info);

EventEmitterEnhancer.modifyInstance(client);

var unbind = client.onAsync('connect', function () {
assert.isTrue(client.connected);
assert.equal(client.server_info, 'server1');
Expand Down

0 comments on commit 418e2a8

Please sign in to comment.