3 Object.defineProperty(exports, "__esModule", {
6 exports.default = memoize;
8 var _identity = require('lodash/identity');
10 var _identity2 = _interopRequireDefault(_identity);
12 var _rest = require('./internal/rest');
14 var _rest2 = _interopRequireDefault(_rest);
16 var _setImmediate = require('./internal/setImmediate');
18 var _setImmediate2 = _interopRequireDefault(_setImmediate);
20 var _initialParams = require('./internal/initialParams');
22 var _initialParams2 = _interopRequireDefault(_initialParams);
24 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26 function has(obj, key) {
31 * Caches the results of an `async` function. When creating a hash to store
32 * function results against, the callback is omitted from the hash and an
33 * optional hash function can be used.
35 * If no hash function is specified, the first argument is used as a hash key,
36 * which may work reasonably if it is a string or a data type that converts to a
37 * distinct string. Note that objects and arrays will not behave reasonably.
38 * Neither will cases where the other arguments are significant. In such cases,
39 * specify your own hash function.
41 * The cache of results is exposed as the `memo` property of the function
42 * returned by `memoize`.
46 * @memberOf module:Utils
49 * @param {Function} fn - The function to proxy and cache results from.
50 * @param {Function} hasher - An optional function for generating a custom hash
51 * for storing results. It has all the arguments applied to it apart from the
52 * callback, and must be synchronous.
53 * @returns {Function} a memoized version of `fn`
56 * var slow_fn = function(name, callback) {
58 * callback(null, result);
60 * var fn = async.memoize(slow_fn);
62 * // fn can now be used as if it were slow_fn
63 * fn('some name', function() {
67 function memoize(fn, hasher) {
68 var memo = Object.create(null);
69 var queues = Object.create(null);
70 hasher = hasher || _identity2.default;
71 var memoized = (0, _initialParams2.default)(function memoized(args, callback) {
72 var key = hasher.apply(null, args);
74 (0, _setImmediate2.default)(function () {
75 callback.apply(null, memo[key]);
77 } else if (has(queues, key)) {
78 queues[key].push(callback);
80 queues[key] = [callback];
81 fn.apply(null, args.concat((0, _rest2.default)(function (args) {
85 for (var i = 0, l = q.length; i < l; i++) {
86 q[i].apply(null, args);
92 memoized.unmemoized = fn;
95 module.exports = exports['default'];