memo.js 1.1 KB

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