Skip to content

Add callback to pubsub methods #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion lib/client/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ exports.subscribe = function () {
});
}
}
if ('function' === typeof arguments[arguments.length - 1]) {
var callback = arguments[arguments.length - 1];
var channels = Array.prototype.slice.call(arguments, 0, arguments.length - 1);
process.nextTick(function () {
callback(null, ...channels);
});
}
};

/**
Expand All @@ -53,6 +60,13 @@ exports.psubscribe = function () {
});
}
}
if ('function' === typeof arguments[arguments.length - 1]) {
var callback = arguments[arguments.length - 1];
var channels = Array.prototype.slice.call(arguments, 0, arguments.length - 1);
process.nextTick(function () {
callback(null, ...channels);
});
}
};
/**
* Unsubscribe
Expand Down Expand Up @@ -83,7 +97,13 @@ exports.unsubscribe = function () {
})(channelName));
}
}

if ('function' === typeof arguments[arguments.length - 1]) {
var callback = arguments[arguments.length - 1];
var channels = Array.prototype.slice.call(arguments, 0, arguments.length - 1);
process.nextTick(function () {
callback(null, ...channels);
});
}
// TODO: If this was the last subscription, pub_sub_mode should be set to false
this.pub_sub_mode = false;
};
Expand Down Expand Up @@ -111,6 +131,13 @@ exports.punsubscribe = function () {
});
}
}
if ('function' === typeof arguments[arguments.length - 1]) {
var callback = arguments[arguments.length - 1];
var channels = Array.prototype.slice.call(arguments, 0, arguments.length - 1);
process.nextTick(function () {
callback(null, ...channels);
});
}
// TODO: If this was the last subscription, pub_sub_mode should be set to false
};

Expand Down
5 changes: 4 additions & 1 deletion lib/server/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ exports.del = function (keys, callback) {
for (let i = 0; i < keys.length; i++) {

if (keys[i] in this.storage) {

if (this.storage[keys[i]]._expire) {
clearTimeout(this.storage[keys[i]]._expire);
}
delete this.storage[keys[i]];
keysDeleted++;

Expand All @@ -25,6 +27,7 @@ exports.del = function (keys, callback) {
helpers.callCallback(callback, null, keysDeleted);
};


/**
* Exists
*/
Expand Down
20 changes: 16 additions & 4 deletions test/client/redis-mock.pubsub.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ describe("publish and subscribe", function () {

});

r.subscribe(channelName);
r.subscribe(channelName, function (err, ch) {
should.equal(err, null);
should.equal(ch, channelName);
});
});

it("should unsubscribe to all channels if no arguments are given", function (done) {
Expand Down Expand Up @@ -56,8 +59,14 @@ describe("publish and subscribe", function () {
}
});

r.subscribe(channelNames[0]);
r.subscribe(channelNames[1]);
r.subscribe(channelNames[0], function (err, ch) {
should.equal(err, null);
should.equal(channelNames[0], channelNames[0]);
});
r.subscribe(channelNames[1], function (err, ch) {
should.equal(err, null);
should.equal(ch, channelNames[1]);
});
});

it("should psubscribe and punsubscribe to a channel", function (done) {
Expand All @@ -76,7 +85,10 @@ describe("publish and subscribe", function () {
should.equal(ch, channelName);
r.quit(done);
});
r.psubscribe(channelName);
r.psubscribe(channelName, function (err, ch) {
should.equal(err, null);
should.equal(ch, channelName);
});
});

it("suscribing and publishing with the same connection should make an error", function (done) {
Expand Down