memoize.js 3.0 KB

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