Projekt

Obecné

Profil

Stáhnout (3.16 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
'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 _slice = require('./internal/slice');
13
14
var _slice2 = _interopRequireDefault(_slice);
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
var _wrapAsync = require('./internal/wrapAsync');
25
26
var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
27
28
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
30
function has(obj, key) {
31
    return key in obj;
32
}
33
34
/**
35
 * Caches the results of an async function. When creating a hash to store
36
 * function results against, the callback is omitted from the hash and an
37
 * optional hash function can be used.
38
 *
39
 * If no hash function is specified, the first argument is used as a hash key,
40
 * which may work reasonably if it is a string or a data type that converts to a
41
 * distinct string. Note that objects and arrays will not behave reasonably.
42
 * Neither will cases where the other arguments are significant. In such cases,
43
 * specify your own hash function.
44
 *
45
 * The cache of results is exposed as the `memo` property of the function
46
 * returned by `memoize`.
47
 *
48
 * @name memoize
49
 * @static
50
 * @memberOf module:Utils
51
 * @method
52
 * @category Util
53
 * @param {AsyncFunction} fn - The async function to proxy and cache results from.
54
 * @param {Function} hasher - An optional function for generating a custom hash
55
 * for storing results. It has all the arguments applied to it apart from the
56
 * callback, and must be synchronous.
57
 * @returns {AsyncFunction} a memoized version of `fn`
58
 * @example
59
 *
60
 * var slow_fn = function(name, callback) {
61
 *     // do something
62
 *     callback(null, result);
63
 * };
64
 * var fn = async.memoize(slow_fn);
65
 *
66
 * // fn can now be used as if it were slow_fn
67
 * fn('some name', function() {
68
 *     // callback
69
 * });
70
 */
71
function memoize(fn, hasher) {
72
    var memo = Object.create(null);
73
    var queues = Object.create(null);
74
    hasher = hasher || _identity2.default;
75
    var _fn = (0, _wrapAsync2.default)(fn);
76
    var memoized = (0, _initialParams2.default)(function memoized(args, callback) {
77
        var key = hasher.apply(null, args);
78
        if (has(memo, key)) {
79
            (0, _setImmediate2.default)(function () {
80
                callback.apply(null, memo[key]);
81
            });
82
        } else if (has(queues, key)) {
83
            queues[key].push(callback);
84
        } else {
85
            queues[key] = [callback];
86
            _fn.apply(null, args.concat(function () /*args*/{
87
                var args = (0, _slice2.default)(arguments);
88
                memo[key] = args;
89
                var q = queues[key];
90
                delete queues[key];
91
                for (var i = 0, l = q.length; i < l; i++) {
92
                    q[i].apply(null, args);
93
                }
94
            }));
95
        }
96
    });
97
    memoized.memo = memo;
98
    memoized.unmemoized = fn;
99
    return memoized;
100
}
101
module.exports = exports['default'];