11'use client'
2- import { type ReactNode , useEffect , useEffectEvent } from 'react'
2+ import { cloneElement , isValidElement , type ReactNode , useEffect , useEffectEvent } from 'react'
33import { MountTrackerUtil } from '@src/util/mount-tracker.util.js'
44import { BaseNode } from '@src/core.node.js'
55import type { NodeInstance } from '@src/types/node.type.js'
66
77/**
8- * `MeoNodeUnmounter` is a client-side React component responsible for cleaning up
9- * resources associated with a rendered node when it unmounts .
8+ * `MeoNodeUnmounter` is a client-side React component responsible for performing cleanup
9+ * operations when a `MeoNode` instance is unmounted from the React tree .
1010 *
11- * It uses a `useEffect` hook to register a cleanup function that runs when the component
12- * unmounts or when its `stableKey` changes. The cleanup function checks if the node
13- * identified by `stableKey` is currently tracked as mounted. If it is, it removes
14- * the node from `BaseNode.elementCache` and untracks its mount status using `MountTrackerUtil`.
15- * Additionally, it clears the `lastPropsRef` and `lastSignature` of the associated `BaseNode`
16- * instance to prevent memory leaks from retained prop objects.
11+ * It leverages `useEffectEvent` to create a stable cleanup function that is called
12+ * when the component unmounts. This cleanup function performs the following actions:
13+ * - Deletes the node from `BaseNode.elementCache` using its `stableKey`.
14+ * - Untracks the node's mount status via `MountTrackerUtil.untrackMount`.
15+ * - Unregisters the node from `BaseNode.cacheCleanupRegistry` to prevent redundant
16+ * finalization callbacks.
17+ * - Clears the `lastSignature` of the associated `BaseNode` instance to help prevent
18+ * memory leaks from retained prop objects.
1719 * @param {object } props The component's props.
1820 * @param {NodeInstance } props.node The BaseNode instance associated with this component.
1921 * @param {ReactNode } [props.children] The children to be rendered by this component.
2022 * @returns {ReactNode } The `children` passed to the component.
2123 */
22- export default function MeoNodeUnmounter ( { node, children } : { node : NodeInstance ; children ?: ReactNode } ) : ReactNode {
24+ export default function MeoNodeUnmounter ( { node, children, ... rest } : { node : NodeInstance ; children ?: ReactNode } ) : ReactNode {
2325 const onUnmount = useEffectEvent ( ( ) => {
2426 if ( node . stableKey ) {
2527 BaseNode . elementCache . delete ( node . stableKey )
@@ -40,5 +42,9 @@ export default function MeoNodeUnmounter({ node, children }: { node: NodeInstanc
4042 return ( ) => onUnmount ( )
4143 } , [ ] )
4244
45+ if ( isValidElement ( children ) ) {
46+ return cloneElement ( children , rest )
47+ }
48+
4349 return children
4450}
0 commit comments