mirror of
https://github.com/BreizhHardware/memoizee.git
synced 2026-01-18 16:37:21 +01:00
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:
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user