refactor(thermidor): Introduce SingletonFactory to DRY getOrCreate functions#7892
refactor(thermidor): Introduce SingletonFactory to DRY getOrCreate functions#7892lavoiesl wants to merge 3 commits into
Conversation
|
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
@coveo/atomic
@coveo/atomic-hosted-page
@coveo/atomic-legacy
@coveo/atomic-react
@coveo/auth
@coveo/bueno
@coveo/create-atomic
@coveo/create-atomic-component
@coveo/create-atomic-component-project
@coveo/create-atomic-result-component
@coveo/create-atomic-rollup-plugin
@coveo/headless
@coveo/headless-react
@coveo/shopify
commit: |
There was a problem hiding this comment.
Pull request overview
This PR refactors @coveo/thermidor internals to remove repeated Map-backed getOrCreate* caching patterns by introducing a shared SingletonFactory helper, and applies it across slices/actions/selectors and a couple of interface utilities.
Changes:
- Added a generic
SingletonFactoryutility (with unit tests) to memoize values per derived key. - Replaced per-module
Mapcaches in manygetOrCreate*exports withSingletonFactory(createX)calls. - Refactored
createFacadeCacheandgetOrCreateHydrateFromSnapshotActionto reuse the same caching helper.
Reviewed changes
Copilot reviewed 37 out of 37 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/thermidor/src/core/internal/triggers/triggers-slice.ts | Replace local slice cache with SingletonFactory(createTriggersSlice). |
| packages/thermidor/src/core/internal/triggers/triggers-actions.ts | Replace local actions cache with SingletonFactory(createTriggersActions). |
| packages/thermidor/src/core/internal/sort/sort-slice.ts | Replace local slice cache with SingletonFactory(createSortSlice). |
| packages/thermidor/src/core/internal/sort/sort-selectors.ts | Replace local selectors cache with SingletonFactory(createSortSelectors). |
| packages/thermidor/src/core/internal/sort/sort-actions.ts | Replace local actions cache with SingletonFactory(createSortActions). |
| packages/thermidor/src/core/internal/singleton-factory/singleton-factory.ts | Add shared memoization helper used by many getOrCreate* functions. |
| packages/thermidor/src/core/internal/singleton-factory/singleton-factory.test.ts | Add unit tests for SingletonFactory behavior and custom key derivation. |
| packages/thermidor/src/core/internal/search-parameters/search-parameters-slice.ts | Replace local slice cache with SingletonFactory(createSearchParametersSlice). |
| packages/thermidor/src/core/internal/search-parameters/search-parameters-selectors.ts | Replace local selectors cache with SingletonFactory(createSearchParametersSelectors). |
| packages/thermidor/src/core/internal/search-parameters/search-parameters-actions.ts | Replace local actions cache with SingletonFactory(createSearchParametersActions). |
| packages/thermidor/src/core/internal/search-box/search-box-slice.ts | Replace local slice cache with SingletonFactory(createSearchBoxSlice). |
| packages/thermidor/src/core/internal/search-box/search-box-selectors.ts | Replace local selectors cache with SingletonFactory(createSearchBoxSelectors). |
| packages/thermidor/src/core/internal/search-box/search-box-actions.ts | Replace local actions cache with SingletonFactory(createSearchBoxActions). |
| packages/thermidor/src/core/internal/result-list/result-list-slice.ts | Replace local slice cache with SingletonFactory(createResultsSlice). |
| packages/thermidor/src/core/internal/result-list/result-list-selectors.ts | Replace local selectors cache with SingletonFactory(createResultsSelectors). |
| packages/thermidor/src/core/internal/result-list/result-list-actions.ts | Replace local actions cache with SingletonFactory(createResultsActions). |
| packages/thermidor/src/core/internal/query-correction/query-correction-slice.ts | Replace local slice cache with SingletonFactory(createQueryCorrectionSlice). |
| packages/thermidor/src/core/internal/query-correction/query-correction-actions.ts | Replace local actions cache with SingletonFactory(createQueryCorrectionActions). |
| packages/thermidor/src/core/internal/product-list/product-list-slice.ts | Replace local slice cache with SingletonFactory(createProductListSlice). |
| packages/thermidor/src/core/internal/product-list/product-list-selectors.ts | Replace local selectors cache with SingletonFactory(createProductListSelectors). |
| packages/thermidor/src/core/internal/product-list/product-list-actions.ts | Replace local actions cache with SingletonFactory(createProductListActions). |
| packages/thermidor/src/core/internal/pagination/pagination-slice.ts | Replace local slice cache with SingletonFactory(createPaginationSlice). |
| packages/thermidor/src/core/internal/pagination/pagination-selectors.ts | Replace local selectors cache with SingletonFactory(createPaginationSelectors). |
| packages/thermidor/src/core/internal/pagination/pagination-actions.ts | Replace local actions cache with SingletonFactory(createPaginationActions). |
| packages/thermidor/src/core/internal/generative/generative-slice.ts | Replace local slice cache with SingletonFactory(createGenerativeSlice). |
| packages/thermidor/src/core/internal/generative/generative-selectors.ts | Replace local selectors cache with SingletonFactory(createGenerativeSelectors). |
| packages/thermidor/src/core/internal/generative/generative-actions.ts | Replace local actions cache with SingletonFactory(createGenerativeActions). |
| packages/thermidor/src/core/internal/facets/facets-slice.ts | Replace local slice cache with SingletonFactory(createFacetsSlice). |
| packages/thermidor/src/core/internal/facets/facets-selectors.ts | Replace local selectors cache with SingletonFactory(createFacetsSelectors). |
| packages/thermidor/src/core/internal/facets/facets-actions.ts | Replace local actions cache with SingletonFactory(createFacetsActions). |
| packages/thermidor/src/core/internal/cart/cart-slice.ts | Replace local slice cache with SingletonFactory(createCartSlice). |
| packages/thermidor/src/core/internal/cart/cart-selectors.ts | Replace local selectors cache with SingletonFactory(createCartSelectors). |
| packages/thermidor/src/core/internal/cart/cart-actions.ts | Replace local actions cache with SingletonFactory(createCartActions). |
| packages/thermidor/src/core/internal/api/search/search-thunk-slice.ts | Replace local selectors cache with SingletonFactory(createSearchEndpointSelectors). |
| packages/thermidor/src/core/internal/api/search/commerce-search-thunk-slice.ts | Replace local selectors cache with SingletonFactory(createCommerceSearchEndpointSelectors). |
| packages/thermidor/src/core/interface/utils/facade-cache.ts | Refactor facade cache to use SingletonFactory with a derived scope key. |
| packages/thermidor/src/core/interface/generative/generative-hydration.ts | Replace hydrate action cache with SingletonFactory(createHydrateAction). |
| export function SingletonFactory<K, V, A = K>( | ||
| factory: (arg: A) => V, | ||
| keyFn: (arg: A) => K = (arg) => arg as unknown as K | ||
| ): (arg: A) => V { |
There was a problem hiding this comment.
Makes sense to me. I’ll fix if we agree on this PR
|
Obsolete |
While reading through the thermidor code, I found the
has/get/setrepetitive, so I made a generic helper to DRY it out.Adds a generic
SingletonFactory<K, V, A>function that wraps a Map with lazy-initialization semantics — call it with an argument, get back the cached value or create it once via the factory.Refactors 33 instances of the repeated
Map+ has/set/get pattern acrossthermidor/src/core/internal/into one-linerSingletonFactory(createX)calls.Also rewrites
createFacadeCacheto delegate toSingletonFactorywith a customkeyFn.Changes:
packages/thermidor/src/core/internal/singleton-factory/singleton-factory.tsNon changes:
getOrCreateSearchEndpointSliceandgetOrCreateCommerceSearchEndpointSlice(key) => Vshape.Points of debate:
SingletonFactory, I’m happy to take other suggestions