memo.js 3.2 KB

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