source: trip-planner-front/node_modules/async/memoize.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.2 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = memoize;
7
8var _identity = require('lodash/identity');
9
10var _identity2 = _interopRequireDefault(_identity);
11
12var _slice = require('./internal/slice');
13
14var _slice2 = _interopRequireDefault(_slice);
15
16var _setImmediate = require('./internal/setImmediate');
17
18var _setImmediate2 = _interopRequireDefault(_setImmediate);
19
20var _initialParams = require('./internal/initialParams');
21
22var _initialParams2 = _interopRequireDefault(_initialParams);
23
24var _wrapAsync = require('./internal/wrapAsync');
25
26var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
27
28function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
30function 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 */
71function 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}
101module.exports = exports['default'];
Note: See TracBrowser for help on using the repository browser.