memo.js 1021 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* eslint-disable @typescript-eslint/no-unsafe-member-access */
  2. /* eslint-disable @typescript-eslint/no-explicit-any */
  3. /**
  4. * Helper to decycle json objects
  5. */
  6. function memoBuilder() {
  7. const hasWeakSet = typeof WeakSet === 'function';
  8. const inner = hasWeakSet ? new WeakSet() : [];
  9. function memoize(obj) {
  10. if (hasWeakSet) {
  11. if (inner.has(obj)) {
  12. return true;
  13. }
  14. inner.add(obj);
  15. return false;
  16. }
  17. // eslint-disable-next-line @typescript-eslint/prefer-for-of
  18. for (let i = 0; i < inner.length; i++) {
  19. const value = inner[i];
  20. if (value === obj) {
  21. return true;
  22. }
  23. }
  24. inner.push(obj);
  25. return false;
  26. }
  27. function unmemoize(obj) {
  28. if (hasWeakSet) {
  29. inner.delete(obj);
  30. } else {
  31. for (let i = 0; i < inner.length; i++) {
  32. if (inner[i] === obj) {
  33. inner.splice(i, 1);
  34. break;
  35. }
  36. }
  37. }
  38. }
  39. return [memoize, unmemoize];
  40. }
  41. export { memoBuilder };
  42. //# sourceMappingURL=memo.js.map