Fix internal primitive id resolution. Fix #15

It was not forced to be serialized to string, therefore it collided
with `null` case
This commit is contained in:
Mariusz Nowak
2014-07-07 12:15:46 +02:00
parent d60468b6d3
commit da0f3356a7
2 changed files with 22 additions and 13 deletions

View File

@@ -91,22 +91,20 @@ module.exports = function (original, length, options) {
};
} else {
memoized = function (arg) {
var result, args = arguments;
if (resolve) {
args = resolve(arguments);
arg = args[0];
var result, args = arguments, id;
if (resolve) args = resolve(arguments);
id = String(args[0]);
if (hasOwnProperty.call(cache, id)) {
if (getListeners) conf.emit('get', id, args, this);
return cache[id];
}
if (hasOwnProperty.call(cache, arg)) {
if (getListeners) conf.emit('get', arg, args, this);
return cache[arg];
}
if (args.length === 1) result = call.call(original, this, arg);
if (args.length === 1) result = call.call(original, this, args[0]);
else result = apply.call(original, this, args);
if (hasOwnProperty.call(cache, arg)) {
if (hasOwnProperty.call(cache, id)) {
throw customError("Circular invocation", 'CIRCULAR_INVOCATION');
}
cache[arg] = result;
if (setListeners) conf.emit('set', arg);
cache[id] = result;
if (setListeners) conf.emit('set', id);
return result;
};
}
@@ -116,7 +114,7 @@ module.exports = function (original, length, options) {
get: function (args) {
if (resolve) args = resolve(args);
if (get) return get(args);
return args[0];
return String(args[0]);
},
has: function (id) { return hasOwnProperty.call(cache, id); },
delete: function (id) {

View File

@@ -221,6 +221,17 @@ module.exports = function () {
d();
});
});
},
"Primitive null arg case": function (a, d) {
var x = {}, mfn = memoize(function f(id, cb) { cb(null, x); }, {
async: true,
primitive: true
});
mfn(null, function (err, res) {
a.deep([err, res], [null, x], "Args");
d();
});
}
},
"Sync Clear": function (a, d) {