2023-08-15 11:26:37 +00:00
|
|
|
/**
|
|
|
|
* Does a depth-first search of a meta object to find the first instance of a component.
|
|
|
|
*
|
|
|
|
* @template T - The type of the component meta object
|
|
|
|
*/
|
|
|
|
export function findComponentMeta< T >(
|
2023-08-18 05:30:25 +00:00
|
|
|
obj: Record< string, unknown >,
|
|
|
|
visited = new Set< Record< string, unknown > >()
|
2023-08-15 11:26:37 +00:00
|
|
|
): T | undefined {
|
2023-08-18 05:30:25 +00:00
|
|
|
if ( visited.has( obj ) ) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
visited.add( obj );
|
|
|
|
|
2023-08-15 11:26:37 +00:00
|
|
|
for ( const key in obj ) {
|
2023-08-18 05:30:25 +00:00
|
|
|
if ( obj.hasOwnProperty( key ) ) {
|
|
|
|
if ( key === 'component' ) {
|
|
|
|
return obj as T;
|
|
|
|
} else if (
|
|
|
|
typeof obj[ key ] === 'object' &&
|
|
|
|
obj[ key ] !== null
|
|
|
|
) {
|
|
|
|
const found = findComponentMeta< T >(
|
|
|
|
obj[ key ] as Record< string, unknown >,
|
|
|
|
visited
|
|
|
|
);
|
|
|
|
if ( found !== undefined ) {
|
|
|
|
return found;
|
|
|
|
}
|
2023-08-15 11:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|