Skip to content

Commit 13c46af

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 13c46af

File tree

3 files changed

+90
-18
lines changed

3 files changed

+90
-18
lines changed

‎index.js‎

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ function RedisPubSub(options) {
1818
// options if not provided
1919
this.observer = options.observer || redis.createClient(this.client.options);
2020

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

@@ -37,27 +33,64 @@ RedisPubSub.prototype.close = function(callback) {
3733
var pubsub = this;
3834
PubSub.prototype.close.call(this, function(err) {
3935
if (err) return callback(err);
40-
pubsub.client.quit(function(err) {
41-
if (err) return callback(err);
42-
pubsub.observer.quit(callback);
43-
});
36+
pubsub._close().then(function() {
37+
callback();
38+
}, callback);
4439
});
4540
};
4641

42+
RedisPubSub.prototype._close = function() {
43+
return this._closing = this._closing || this._connect().then(Promise.all([
44+
this.client.quit(),
45+
this.observer.quit()
46+
]));
47+
};
48+
4749
RedisPubSub.prototype._subscribe = function(channel, callback) {
48-
this.observer.subscribe(channel, callback);
50+
var pubsub = this;
51+
this._observerConnection.then(function() {
52+
pubsub.observer
53+
.subscribe(channel, function(message) {
54+
var data = JSON.parse(message);
55+
pubsub._emit(channel, data);
56+
})
57+
.then(function() {
58+
callback();
59+
}, callback);
60+
});
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) {
56-
var message = JSON.stringify(data);
57-
var args = [PUBLISH_SCRIPT, 0, message].concat(channels);
58-
this.client.eval(args, callback);
71+
var client = this.client;
72+
this._clientConnection.then(function() {
73+
var message = JSON.stringify(data);
74+
var arguments = [message].concat(channels);
75+
client.eval(PUBLISH_SCRIPT, {arguments: arguments}).then(function() {
76+
callback();
77+
}, callback);
78+
});
79+
};
80+
81+
RedisPubSub.prototype._connect = function() {
82+
this._clientConnection = this._clientConnection || connect(this.client);
83+
this._observerConnection = this._observerConnection || connect(this.observer);
84+
return Promise.all([
85+
this._clientConnection,
86+
this._observerConnection
87+
]);
5988
};
6089

90+
function connect(client) {
91+
return client.isOpen ? Promise.resolve() : client.connect();
92+
}
93+
6194
var PUBLISH_SCRIPT =
6295
'for i = 2, #ARGV do ' +
6396
'redis.call("publish", ARGV[i], ARGV[1]) ' +

‎package.json‎

Lines changed: 2 additions & 2 deletions
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‎

Lines changed: 41 additions & 2 deletions
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)