diff --git a/packages/sanity/src/core/form/store/utils/getId.ts b/packages/sanity/src/core/form/store/utils/getId.ts index 29a591820f1..3eeaed0822e 100644 --- a/packages/sanity/src/core/form/store/utils/getId.ts +++ b/packages/sanity/src/core/form/store/utils/getId.ts @@ -4,6 +4,19 @@ const idCache = new WeakMap() const undefinedKey = Symbol('GetIdUndefined') const nullKey = Symbol('GetIdNull') +/** + * Generates a stable ID for various types of values, including `undefined`, `null`, objects, functions, and symbols. + * + * - **Primitives (string, number, boolean):** The value itself is used as the ID. + * - **Undefined and null:** Special symbols (`undefinedKey` and `nullKey`) are used to generate unique IDs. + * - **Objects and functions:** An ID is generated using the `nanoid` library and cached in a `WeakMap` for stable future retrieval. + * + * This function is used to reconcile inputs in `prepareFormState` immutably, allowing IDs to be generated and cached based + * on the reference of the object. This ensures that memoization functions can use these IDs for consistent hashing without + * recalculating on each call, as the inputs themselves are immutably edited. + * + * @internal + */ export function getId(value: unknown): string { switch (typeof value) { case 'undefined': {