7b81ec86fb3ecbbbc282b882af17e2aefe72bcf206a51f76c04968a9585b1de2f5449a6001651609facbe6c6e2acc906f9a651fc6b58902945a25534ed9c70 917 B

123456789101112131415161718192021222324252627282930
  1. export function memoize(_target, key, descriptor) {
  2. let fnKey = null;
  3. let fn = null;
  4. if (typeof descriptor.value === 'function') {
  5. fnKey = 'value';
  6. fn = descriptor.value;
  7. if (fn.length !== 0) {
  8. console.warn('Memoize should only be used in functions with zero parameters');
  9. }
  10. }
  11. else if (typeof descriptor.get === 'function') {
  12. fnKey = 'get';
  13. fn = descriptor.get;
  14. }
  15. if (!fn) {
  16. throw new Error('not supported');
  17. }
  18. const memoizeKey = `$memoize$${key}`;
  19. descriptor[fnKey] = function (...args) {
  20. if (!this.hasOwnProperty(memoizeKey)) {
  21. Object.defineProperty(this, memoizeKey, {
  22. configurable: false,
  23. enumerable: false,
  24. writable: false,
  25. value: fn.apply(this, args)
  26. });
  27. }
  28. return this[memoizeKey];
  29. };
  30. }