memoize.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = memoize;
  6. var _setImmediate = require('./internal/setImmediate.js');
  7. var _setImmediate2 = _interopRequireDefault(_setImmediate);
  8. var _initialParams = require('./internal/initialParams.js');
  9. var _initialParams2 = _interopRequireDefault(_initialParams);
  10. var _wrapAsync = require('./internal/wrapAsync.js');
  11. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. /**
  14. * Caches the results of an async function. When creating a hash to store
  15. * function results against, the callback is omitted from the hash and an
  16. * optional hash function can be used.
  17. *
  18. * **Note: if the async function errs, the result will not be cached and
  19. * subsequent calls will call the wrapped function.**
  20. *
  21. * If no hash function is specified, the first argument is used as a hash key,
  22. * which may work reasonably if it is a string or a data type that converts to a
  23. * distinct string. Note that objects and arrays will not behave reasonably.
  24. * Neither will cases where the other arguments are significant. In such cases,
  25. * specify your own hash function.
  26. *
  27. * The cache of results is exposed as the `memo` property of the function
  28. * returned by `memoize`.
  29. *
  30. * @name memoize
  31. * @static
  32. * @memberOf module:Utils
  33. * @method
  34. * @category Util
  35. * @param {AsyncFunction} fn - The async function to proxy and cache results from.
  36. * @param {Function} hasher - An optional function for generating a custom hash
  37. * for storing results. It has all the arguments applied to it apart from the
  38. * callback, and must be synchronous.
  39. * @returns {AsyncFunction} a memoized version of `fn`
  40. * @example
  41. *
  42. * var slow_fn = function(name, callback) {
  43. * // do something
  44. * callback(null, result);
  45. * };
  46. * var fn = async.memoize(slow_fn);
  47. *
  48. * // fn can now be used as if it were slow_fn
  49. * fn('some name', function() {
  50. * // callback
  51. * });
  52. */
  53. function memoize(fn, hasher = v => v) {
  54. var memo = Object.create(null);
  55. var queues = Object.create(null);
  56. var _fn = (0, _wrapAsync2.default)(fn);
  57. var memoized = (0, _initialParams2.default)((args, callback) => {
  58. var key = hasher(...args);
  59. if (key in memo) {
  60. (0, _setImmediate2.default)(() => callback(null, ...memo[key]));
  61. } else if (key in queues) {
  62. queues[key].push(callback);
  63. } else {
  64. queues[key] = [callback];
  65. _fn(...args, (err, ...resultArgs) => {
  66. // #1465 don't memoize if an error occurred
  67. if (!err) {
  68. memo[key] = resultArgs;
  69. }
  70. var q = queues[key];
  71. delete queues[key];
  72. for (var i = 0, l = q.length; i < l; i++) {
  73. q[i](err, ...resultArgs);
  74. }
  75. });
  76. }
  77. });
  78. memoized.memo = memo;
  79. memoized.unmemoized = fn;
  80. return memoized;
  81. }
  82. module.exports = exports['default'];