weak.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* eslint id-length: 0, no-shadow: 0, no-unused-vars: 0 */
  2. "use strict";
  3. module.exports = function (t, a, d) {
  4. var value = [], obj = {}, memoized, count = 0, x, y, z;
  5. memoized = t(
  6. function (arg, x, y) {
  7. a(arg, obj);
  8. return x + y;
  9. },
  10. {
  11. refCounter: true,
  12. dispose: function (val) {
  13. value.push(val);
  14. }
  15. }
  16. );
  17. a(memoized(obj, 3, 7), 10);
  18. a(memoized(obj, 5, 8), 13);
  19. a(memoized(obj, 12, 4), 16);
  20. a.deep(value, [], "Pre");
  21. a(memoized(obj, 5, 8), 13);
  22. memoized.deleteRef(obj, 5, 8);
  23. a.deep(value, [], "Pre");
  24. memoized.deleteRef(obj, 5, 8);
  25. a.deep(value, [13], "#1");
  26. value = [];
  27. memoized.deleteRef(obj, 12, 4);
  28. a.deep(value, [16], "#2");
  29. value = [];
  30. memoized(obj, 77, 11);
  31. x = {};
  32. y = {};
  33. z = {};
  34. memoized = t(function (arg) {
  35. return ++count;
  36. });
  37. a(memoized(x), 1);
  38. a(memoized(y), 2);
  39. a(memoized(x), 1);
  40. a(memoized(z), 3);
  41. a(count, 3);
  42. count = 0;
  43. memoized = t(
  44. function (arg) {
  45. return ++count;
  46. },
  47. { maxAge: 1 }
  48. );
  49. memoized(obj);
  50. setTimeout(function () {
  51. memoized(obj);
  52. a(count, 2);
  53. d();
  54. }, 100);
  55. };