3e3bada804f85693c9e21db7d997ee541a18cfd6
[yaffs-website] / node_modules / grunt-uncss / node_modules / async / memoize.js
1 'use strict';
2
3 Object.defineProperty(exports, "__esModule", {
4     value: true
5 });
6 exports.default = memoize;
7
8 var _identity = require('lodash/identity');
9
10 var _identity2 = _interopRequireDefault(_identity);
11
12 var _rest = require('./internal/rest');
13
14 var _rest2 = _interopRequireDefault(_rest);
15
16 var _setImmediate = require('./internal/setImmediate');
17
18 var _setImmediate2 = _interopRequireDefault(_setImmediate);
19
20 var _initialParams = require('./internal/initialParams');
21
22 var _initialParams2 = _interopRequireDefault(_initialParams);
23
24 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
26 function has(obj, key) {
27     return key in obj;
28 }
29
30 /**
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.
34  *
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.
40  *
41  * The cache of results is exposed as the `memo` property of the function
42  * returned by `memoize`.
43  *
44  * @name memoize
45  * @static
46  * @memberOf module:Utils
47  * @method
48  * @category Util
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`
54  * @example
55  *
56  * var slow_fn = function(name, callback) {
57  *     // do something
58  *     callback(null, result);
59  * };
60  * var fn = async.memoize(slow_fn);
61  *
62  * // fn can now be used as if it were slow_fn
63  * fn('some name', function() {
64  *     // callback
65  * });
66  */
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);
73         if (has(memo, key)) {
74             (0, _setImmediate2.default)(function () {
75                 callback.apply(null, memo[key]);
76             });
77         } else if (has(queues, key)) {
78             queues[key].push(callback);
79         } else {
80             queues[key] = [callback];
81             fn.apply(null, args.concat((0, _rest2.default)(function (args) {
82                 memo[key] = args;
83                 var q = queues[key];
84                 delete queues[key];
85                 for (var i = 0, l = q.length; i < l; i++) {
86                     q[i].apply(null, args);
87                 }
88             })));
89         }
90     });
91     memoized.memo = memo;
92     memoized.unmemoized = fn;
93     return memoized;
94 }
95 module.exports = exports['default'];