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,aAAa,YAC7B,OAAO;IAEX,GAAG;QAAC;QAAU;QAAO;QAAO;QAAS;KAAc;AACrD;AAEA,SAAS,gCAAU,KAAU;IAC3B,OAAQ,OAAO;QACb,KAAK;YACH,OAAO,SAAS;QAClB,KAAK;QACL,KAAK;YACH,OAAO;QACT;YACE,OAAO;IACX;AACF","sources":["packages/react-aria/src/collections/useCachedChildren.ts"],"sourcesContent":["/*\n * Copyright 2024 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {cloneElement, ReactElement, ReactNode, useMemo} from 'react';\nimport {Key} from '@react-types/shared';\n\nexport interface CachedChildrenOptions<T> {\n /** Item objects in the collection. */\n items?: Iterable<T>;\n /** The contents of the collection. */\n children?: ReactNode | ((item: T) => ReactNode);\n /** Values that should invalidate the item cache when using dynamic collections. */\n dependencies?: ReadonlyArray<any>;\n /** A scope to prepend to all child item ids to ensure they are unique. */\n idScope?: Key;\n /** Whether to add `id` and `value` props to all child items. */\n addIdAndValue?: boolean;\n}\n\n/**\n * Maps over a list of items and renders React elements for them. Each rendered item is\n * cached based on object identity, and React keys are generated from the `key` or `id` property.\n */\nexport function useCachedChildren<T>(props: CachedChildrenOptions<T>): ReactNode {\n let {children, items, idScope, addIdAndValue, dependencies = []} = props;\n\n // In development, invalidate when the children function updates (e.g. HMR).\n let childrenString = useMemo(\n () =>\n process.env.NODE_ENV !== 'production' && typeof children === 'function'\n ? children.toString()\n : undefined,\n [children]\n );\n\n // Invalidate the cache whenever dependencies change.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n let cache = useMemo(() => new WeakMap(), [...dependencies, childrenString]);\n\n return useMemo(() => {\n if (items && typeof children === 'function') {\n let res: ReactElement[] = [];\n for (let item of items) {\n let cacheKey = isWeakKey(item) ? item : null;\n let rendered = cacheKey ? cache.get(cacheKey) : null;\n if (!rendered) {\n rendered = children(item);\n // @ts-ignore\n let id = rendered.props.id ?? item?.key ?? item?.id;\n if (idScope != null && rendered.props.id == null && id != null) {\n id = idScope + ':' + id;\n }\n\n // If no id is inferred from data, use the index as the React key.\n // An id will be generated by the collection document.\n let key = id ?? res.length;\n\n // Note: only works if wrapped Item passes through id...\n rendered = cloneElement(rendered, addIdAndValue ? {key, id, value: item} : {key});\n if (cacheKey) {\n cache.set(cacheKey, rendered);\n }\n }\n res.push(rendered);\n }\n return res;\n } else if (typeof children !== 'function') {\n return children;\n }\n }, [children, items, cache, idScope, addIdAndValue]);\n}\n\nfunction isWeakKey(value: any): value is WeakKey {\n switch (typeof value) {\n case 'object':\n return value != null;\n case 'function':\n case 'symbol':\n return true;\n default:\n return false;\n }\n}\n"],"names":[],"version":3,"file":"useCachedChildren.cjs.map"}