From f30ecbe5615bfa42370c689a9877e019b73066ba Mon Sep 17 00:00:00 2001 From: Ian Babrou Date: Sun, 2 Oct 2011 11:42:23 +0400 Subject: [PATCH 1/2] added ability to pass undefined instead of callback --- index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index a1dd40fe97..beb8d07844 100644 --- a/index.js +++ b/index.js @@ -483,7 +483,7 @@ function Command(command, args, sub_command, callback) { } RedisClient.prototype.send_command = function (command, args, callback) { - var arg, this_args, command_obj, i, il, elem_count, stream = this.stream, buffer_args, command_str = "", buffered_writes = 0; + var arg, this_args, command_obj, i, il, elem_count, stream = this.stream, buffer_args, command_str = "", buffered_writes = 0, last_arg_type; if (typeof command !== "string") { throw new Error("First argument to send_command must be the command name string, not " + typeof command); @@ -500,9 +500,11 @@ RedisClient.prototype.send_command = function (command, args, callback) { // send_command(command, [arg1, arg2, cb]); // client.command(arg1, arg2); (callback is optional) // send_command(command, [arg1, arg2]); - if (typeof args[args.length - 1] === "function") { - callback = args[args.length - 1]; - args.length -= 1; + // client.command(arg1, arg2, undefined); (callback is undefined) + // send_command(command, [arg1, arg2, undefined]); + last_arg_type = typeof args[args.length - 1]; + if (last_arg_type === "function" || last_arg_type === "undefined") { + callback = args.pop(); } } else { throw new Error("send_command: last argument must be a callback or undefined"); From 0c1c8cab83b6f5468123ba28a94ce23f297e426c Mon Sep 17 00:00:00 2001 From: bobrik Date: Sat, 15 Oct 2011 17:07:40 +0400 Subject: [PATCH 2/2] added test for optional callbacks --- test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test.js b/test.js index d909faebd0..9fcacb21c3 100644 --- a/test.js +++ b/test.js @@ -1175,6 +1175,20 @@ tests.TTL = function () { }, 500); }; +tests.OPTIONAL_CALLBACK = function() { + var name = "OPTIONAL_CALLBACK"; + client.del("op_cb1"); + client.set("op_cb1", "x"); + client.get("op_cb1", last(name, require_string("x", name))); +}; + +tests.OPTIONAL_CALLBACK_UNDEFINED = function() { + var name = "OPTIONAL_CALLBACK_UNDEFINED"; + client.del("op_cb2"); + client.set("op_cb2", "y", undefined); + client.get("op_cb2", last(name, require_string("y", name))); +} + all_tests = Object.keys(tests); all_start = new Date(); test_count = 0;