memo.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.useMemoConnector = exports.useAsyncMemoPreprocessor = exports.useMemoPreprocessor = void 0;
  13. function withFunction(_, value) {
  14. return typeof value === 'function' ? `${value}` : value;
  15. }
  16. /**
  17. * Returns a sync function returning memoized transform of preprocessor and connector.
  18. * The memoized value will recompute only when the data reference or options has changed.
  19. */
  20. function useMemoPreprocessor(Preprocessor) {
  21. const dataCache = new Map();
  22. const NewPreprocessor = (options) => {
  23. const key = JSON.stringify(options, withFunction);
  24. const transform = Preprocessor(options);
  25. return ({ data }) => {
  26. if (dataCache.has(data)) {
  27. const cache = dataCache.get(data);
  28. cache[key] = cache[key] || transform(data);
  29. return cache[key];
  30. }
  31. const cache = {};
  32. cache[key] = transform({ data });
  33. dataCache.set(data, cache);
  34. return cache[key];
  35. };
  36. };
  37. NewPreprocessor.props = Preprocessor.props;
  38. return NewPreprocessor;
  39. }
  40. exports.useMemoPreprocessor = useMemoPreprocessor;
  41. /**
  42. * Returns a async function returning memoized transform and connector.
  43. * The memoized value will recompute only when the data reference or options has changed.
  44. */
  45. function useAsyncMemoPreprocessor(Preprocessor) {
  46. const dataCache = new Map();
  47. const NewPreprocessor = (options) => {
  48. const key = JSON.stringify(options, withFunction);
  49. const transform = Preprocessor(options);
  50. return ({ data }) => __awaiter(this, void 0, void 0, function* () {
  51. if (dataCache.has(data)) {
  52. const cache = dataCache.get(data);
  53. cache[key] = cache[key] || (yield transform(data));
  54. return cache[key];
  55. }
  56. const cache = {};
  57. cache[key] = transform({ data });
  58. dataCache.set(data, cache);
  59. return cache[key];
  60. });
  61. };
  62. NewPreprocessor.props = Preprocessor.props;
  63. return NewPreprocessor;
  64. }
  65. exports.useAsyncMemoPreprocessor = useAsyncMemoPreprocessor;
  66. /**
  67. * Returns a async function returning memoized connector transform.
  68. * The memoized value will recompute only when options has changed
  69. * and ignore data.
  70. */
  71. function useMemoConnector(Connector) {
  72. const cache = {};
  73. const NewConnector = (options) => {
  74. const transform = Connector(options);
  75. const key = JSON.stringify(options, withFunction);
  76. return () => __awaiter(this, void 0, void 0, function* () {
  77. cache[key] = cache[key] || (yield transform({}));
  78. return cache[key];
  79. });
  80. };
  81. NewConnector.props = Connector.props;
  82. return NewConnector;
  83. }
  84. exports.useMemoConnector = useMemoConnector;
  85. //# sourceMappingURL=memo.js.map