Skip to content

Commit 6708c9f

Browse files
committed
💥 Bump redis to v4
Fixes #19 This is a **BREAKING** change that upgrades the underlying `redis` client to v4, which had so many [breaking changes][1], that it's infeasible to maintain backwards-compatibility with v3. If consumers need to use v3, they are advised to pin their version of `sharedb-redis-pubsub` to v4. This change adapts to the breakages: - wrap the returned `Promise`s in callbacks - actively connect the `client`, which no longer auto-connects - move the `message` event handler into the subscription callback - adapt to the new call signature of `client.eval()` [1]: https://github.com/redis/node-redis/blob/HEAD/docs/v3-to-v4.md
1 parent fa5921e commit 6708c9f

File tree

3 files changed

+86
-17
lines changed

3 files changed

+86
-17
lines changed

‎index.js

+43-13
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ function RedisPubSub(options) {
1212
options || (options = {});
1313

1414
this.client = options.client || redis.createClient(options);
15+
this._clientConnection = null;
1516

1617
// Redis doesn't allow the same connection to both listen to channels and do
1718
// operations. Make an extra redis connection for subscribing with the same
1819
// options if not provided
1920
this.observer = options.observer || redis.createClient(this.client.options);
21+
this._observerConnection = null;
2022

21-
var pubsub = this;
22-
this.observer.on('message', function(channel, message) {
23-
var data = JSON.parse(message);
24-
pubsub._emit(channel, data);
25-
});
23+
this._connect();
2624
}
2725
module.exports = RedisPubSub;
2826

@@ -37,27 +35,59 @@ RedisPubSub.prototype.close = function(callback) {
3735
var pubsub = this;
3836
PubSub.prototype.close.call(this, function(err) {
3937
if (err) return callback(err);
40-
pubsub.client.quit(function(err) {
41-
if (err) return callback(err);
42-
pubsub.observer.quit(callback);
43-
});
38+
pubsub._close().then(function() {
39+
callback();
40+
}, callback);
4441
});
4542
};
4643

44+
RedisPubSub.prototype._close = function() {
45+
return this._closing = this._closing || this._connect().then(Promise.all([
46+
this.client.quit(),
47+
this.observer.quit()
48+
]));
49+
};
50+
4751
RedisPubSub.prototype._subscribe = function(channel, callback) {
48-
this.observer.subscribe(channel, callback);
52+
var pubsub = this;
53+
pubsub.observer
54+
.subscribe(channel, function(message) {
55+
var data = JSON.parse(message);
56+
pubsub._emit(channel, data);
57+
})
58+
.then(function() {
59+
callback();
60+
}, callback);
4961
};
5062

5163
RedisPubSub.prototype._unsubscribe = function(channel, callback) {
52-
this.observer.unsubscribe(channel, callback);
64+
this.observer.unsubscribe(channel)
65+
.then(function() {
66+
callback();
67+
}, callback);
5368
};
5469

5570
RedisPubSub.prototype._publish = function(channels, data, callback) {
5671
var message = JSON.stringify(data);
57-
var args = [PUBLISH_SCRIPT, 0, message].concat(channels);
58-
this.client.eval(args, callback);
72+
var args = [message].concat(channels);
73+
this.client.eval(PUBLISH_SCRIPT, {arguments: args}).then(function() {
74+
callback();
75+
}, callback);
5976
};
6077

78+
RedisPubSub.prototype._connect = function() {
79+
this._clientConnection = this._clientConnection || connect(this.client);
80+
this._observerConnection = this._observerConnection || connect(this.observer);
81+
return Promise.all([
82+
this._clientConnection,
83+
this._observerConnection
84+
]);
85+
};
86+
87+
function connect(client) {
88+
return client.isOpen ? Promise.resolve() : client.connect();
89+
}
90+
6191
var PUBLISH_SCRIPT =
6292
'for i = 2, #ARGV do ' +
6393
'redis.call("publish", ARGV[i], ARGV[1]) ' +

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "sharedb-redis-pubsub",
3-
"version": "4.0.0",
3+
"version": "5.0.0",
44
"description": "Redis pub/sub adapter adapter for ShareDB",
55
"main": "index.js",
66
"dependencies": {
7-
"redis": "^2.6.0 || ^3.0.0",
7+
"redis": "^4.0.0",
88
"sharedb": "^1.0.0 || ^2.0.0 || ^3.0.0 || ^4.0.0"
99
},
1010
"devDependencies": {

‎test/test.js

+41-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
var redisPubSub = require('../index');
2+
var redis = require('redis');
3+
var runTestSuite = require('sharedb/test/pubsub');
24

3-
require('sharedb/test/pubsub')(function(callback) {
4-
callback(null, redisPubSub());
5+
describe('default options', function() {
6+
runTestSuite(function(callback) {
7+
callback(null, redisPubSub());
8+
});
9+
});
10+
11+
describe('unconnected client', function() {
12+
runTestSuite(function(callback) {
13+
callback(null, redisPubSub({
14+
client: redis.createClient()
15+
}));
16+
});
17+
});
18+
19+
describe('connected client', function() {
20+
var client;
21+
22+
beforeEach(function(done) {
23+
client = redis.createClient();
24+
client.connect().then(function() {
25+
done();
26+
}, done);
27+
});
28+
29+
runTestSuite(function(callback) {
30+
callback(null, redisPubSub({
31+
client: client
32+
}));
33+
});
34+
});
35+
36+
describe('connecting client', function() {
37+
runTestSuite(function(callback) {
38+
var client = redis.createClient();
39+
client.connect();
40+
callback(null, redisPubSub({
41+
client: client
42+
}));
43+
});
544
});

0 commit comments

Comments
 (0)