weak.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. var customError = require("es5-ext/error/custom")
  3. , defineLength = require("es5-ext/function/_define-length")
  4. , partial = require("es5-ext/function/#/partial")
  5. , copy = require("es5-ext/object/copy")
  6. , normalizeOpts = require("es5-ext/object/normalize-options")
  7. , callable = require("es5-ext/object/valid-callable")
  8. , d = require("d")
  9. , WeakMap = require("es6-weak-map")
  10. , resolveLength = require("./resolve-length")
  11. , extensions = require("./registered-extensions")
  12. , resolveResolve = require("./resolve-resolve")
  13. , resolveNormalize = require("./resolve-normalize");
  14. var slice = Array.prototype.slice, defineProperties = Object.defineProperties;
  15. module.exports = function (memoize) {
  16. return function (fn/*, options*/) {
  17. var map, length, options = normalizeOpts(arguments[1]), memoized, resolve, normalizer;
  18. callable(fn);
  19. // Do not memoize already memoized function
  20. if (hasOwnProperty.call(fn, "__memoized__") && !options.force) return fn;
  21. length = resolveLength(options.length, fn.length, options.async && extensions.async);
  22. options.length = length ? length - 1 : 0;
  23. map = new WeakMap();
  24. if (options.resolvers) resolve = resolveResolve(options.resolvers);
  25. if (options.normalizer) normalizer = resolveNormalize(options.normalizer);
  26. if (
  27. length === 1 &&
  28. !normalizer &&
  29. !options.async &&
  30. !options.dispose &&
  31. !options.maxAge &&
  32. !options.max &&
  33. !options.refCounter
  34. ) {
  35. return defineProperties(
  36. function (obj) {
  37. var result, args = arguments;
  38. if (resolve) args = resolve(args);
  39. obj = args[0];
  40. if (map.has(obj)) return map.get(obj);
  41. result = fn.apply(this, args);
  42. if (map.has(obj)) {
  43. throw customError("Circular invocation", "CIRCULAR_INVOCATION");
  44. }
  45. map.set(obj, result);
  46. return result;
  47. },
  48. {
  49. __memoized__: d(true),
  50. delete: d(function (obj) {
  51. if (resolve) obj = resolve(arguments)[0];
  52. return map.delete(obj);
  53. })
  54. }
  55. );
  56. }
  57. memoized = defineProperties(
  58. defineLength(function (obj) {
  59. var memoizer, args = arguments;
  60. if (resolve) {
  61. args = resolve(args);
  62. obj = args[0];
  63. }
  64. memoizer = map.get(obj);
  65. if (!memoizer) {
  66. if (normalizer) {
  67. options = copy(options);
  68. options.normalizer = copy(normalizer);
  69. options.normalizer.get = partial.call(options.normalizer.get, obj);
  70. options.normalizer.set = partial.call(options.normalizer.set, obj);
  71. if (options.normalizer.delete) {
  72. options.normalizer.delete = partial.call(
  73. options.normalizer.delete, obj
  74. );
  75. }
  76. }
  77. map.set(obj, memoizer = memoize(partial.call(fn, obj), options));
  78. }
  79. return memoizer.apply(this, slice.call(args, 1));
  80. }, length),
  81. {
  82. __memoized__: d(true),
  83. delete: d(
  84. defineLength(function (obj) {
  85. var memoizer, args = arguments;
  86. if (resolve) {
  87. args = resolve(args);
  88. obj = args[0];
  89. }
  90. memoizer = map.get(obj);
  91. if (!memoizer) return;
  92. memoizer.delete.apply(this, slice.call(args, 1));
  93. }, length)
  94. )
  95. }
  96. );
  97. if (!options.refCounter) return memoized;
  98. defineProperties(memoized, {
  99. deleteRef: d(
  100. defineLength(function (obj) {
  101. var memoizer, args = arguments;
  102. if (resolve) {
  103. args = resolve(args);
  104. obj = args[0];
  105. }
  106. memoizer = map.get(obj);
  107. if (!memoizer) return null;
  108. return memoizer.deleteRef.apply(this, slice.call(args, 1));
  109. }, length)
  110. ),
  111. getRefCount: d(
  112. defineLength(function (obj) {
  113. var memoizer, args = arguments;
  114. if (resolve) {
  115. args = resolve(args);
  116. obj = args[0];
  117. }
  118. memoizer = map.get(obj);
  119. if (!memoizer) return 0;
  120. return memoizer.getRefCount.apply(this, slice.call(args, 1));
  121. }, length)
  122. )
  123. });
  124. return memoized;
  125. };
  126. };