You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
648 B
24 lines
648 B
2 weeks ago
|
var has = require('./has');
|
||
|
exports = function(fn) {
|
||
|
var hashFn =
|
||
|
arguments.length > 1 && arguments[1] !== undefined
|
||
|
? arguments[1]
|
||
|
: JSON.stringify;
|
||
|
var singleton = function() {
|
||
|
var cache = singleton.cache;
|
||
|
var address = hashFn.apply(this, arguments);
|
||
|
if (has(cache, address)) {
|
||
|
return cache[address];
|
||
|
}
|
||
|
var promise = fn.apply(this, arguments).finally(function() {
|
||
|
delete cache[address];
|
||
|
});
|
||
|
cache[address] = promise;
|
||
|
return promise;
|
||
|
};
|
||
|
singleton.cache = {};
|
||
|
return singleton;
|
||
|
};
|
||
|
|
||
|
module.exports = exports;
|