From 3e2ef9db9f69c5ba8736e4cf18790a2132cb6d0e Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Thu, 11 Apr 2024 19:48:44 -0700 Subject: [PATCH] feat: make setKeyInfoForResource public (#9352) * fix: make setKeyInfoForResource public * fix export --- packages/store/src/-private.ts | 8 +----- .../src/-private/caches/identifier-cache.ts | 9 +++---- packages/store/src/-types/q/identifier.ts | 25 +++++++++++++++++++ packages/store/src/index.ts | 10 +++++--- tests/docs/fixtures/expected.js | 1 + 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/packages/store/src/-private.ts b/packages/store/src/-private.ts index 9ebddf4afae..fbf841cfded 100644 --- a/packages/store/src/-private.ts +++ b/packages/store/src/-private.ts @@ -8,13 +8,7 @@ export { recordIdentifierFor } from './-private/caches/instance-cache'; export { CacheHandler, type LifetimesService } from './-private/cache-handler'; -export { - setIdentifierGenerationMethod, - setIdentifierUpdateMethod, - setIdentifierForgetMethod, - setIdentifierResetMethod, - isStableIdentifier, -} from './-private/caches/identifier-cache'; +export { isStableIdentifier } from './-private/caches/identifier-cache'; export { default as constructResource } from './-private/utils/construct-resource'; diff --git a/packages/store/src/-private/caches/identifier-cache.ts b/packages/store/src/-private/caches/identifier-cache.ts index b9306ec7064..a42d1f27f18 100644 --- a/packages/store/src/-private/caches/identifier-cache.ts +++ b/packages/store/src/-private/caches/identifier-cache.ts @@ -25,6 +25,8 @@ import type { ExistingResourceObject, ResourceIdentifierObject } from '@warp-dri import type { ForgetMethod, GenerationMethod, + KeyInfo, + KeyInfoMethod, ResetMethod, ResourceData, UpdateMethod, @@ -76,10 +78,7 @@ type TypeMap = { [key: string]: KeyOptions }; // type IdentifierTypeLookup = { all: Set; id: Map }; // type IdentifiersByType = Map; type IdentifierMap = Map; -type KeyInfo = { - id: string | null; - type: string; -}; + type StableCache = { resources: IdentifierMap; documents: Map; @@ -87,8 +86,6 @@ type StableCache = { polymorphicLidBackMap: Map; }; -export type KeyInfoMethod = (resource: unknown, known: StableRecordIdentifier | null) => KeyInfo; - export type MergeMethod = ( targetIdentifier: StableRecordIdentifier, matchedIdentifier: StableRecordIdentifier, diff --git a/packages/store/src/-types/q/identifier.ts b/packages/store/src/-types/q/identifier.ts index c326d05d1cd..5627b391f18 100644 --- a/packages/store/src/-types/q/identifier.ts +++ b/packages/store/src/-types/q/identifier.ts @@ -171,3 +171,28 @@ export type ForgetMethod = (identifier: StableIdentifier | StableRecordIdentifie @static */ export type ResetMethod = () => void; + +/** + Configure a callback for when the identifier cache is generating a new + StableRecordIdentifier for a resource. + + This method controls the `type` and `id` that will be assigned to the + `StableRecordIdentifier` that is created. + + This configuration MUST occur prior to the store instance being created. + + ```js + import { setKeyInfoForResource } from '@ember-data/store'; + ``` + + @method setKeyInfoForResource + @for @ember-data/store + @param method + @public + @static + */ +export type KeyInfo = { + id: string | null; + type: string; +}; +export type KeyInfoMethod = (resource: unknown, known: StableRecordIdentifier | null) => KeyInfo; diff --git a/packages/store/src/index.ts b/packages/store/src/index.ts index e6711bdb98a..3b34b8b6874 100644 --- a/packages/store/src/index.ts +++ b/packages/store/src/index.ts @@ -187,10 +187,14 @@ export { CacheHandler, type LifetimesService, type StoreRequestInput, + recordIdentifierFor, + storeFor, +} from './-private'; + +export { setIdentifierGenerationMethod, setIdentifierUpdateMethod, setIdentifierForgetMethod, setIdentifierResetMethod, - recordIdentifierFor, - storeFor, -} from './-private'; + setKeyInfoForResource, +} from './-private/caches/identifier-cache'; diff --git a/tests/docs/fixtures/expected.js b/tests/docs/fixtures/expected.js index fb8fc049772..3ebaa75e570 100644 --- a/tests/docs/fixtures/expected.js +++ b/tests/docs/fixtures/expected.js @@ -467,6 +467,7 @@ module.exports = { '(public) @ember-data/store @ember-data/store#setIdentifierGenerationMethod', '(public) @ember-data/store @ember-data/store#setIdentifierResetMethod', '(public) @ember-data/store @ember-data/store#setIdentifierUpdateMethod', + '(public) @ember-data/store @ember-data/store#setKeyInfoForResource', '(public) @ember-data/store CacheManager#changedAttrs', '(public) @ember-data/store CacheManager#clientDidCreate', '(public) @ember-data/store CacheManager#commitWasRejected',