rdesign/frontend/node_modules/@copilotkit/a2ui-renderer/dist/react-renderer/registry/ComponentRegistry.cjs

114 lines
2.8 KiB
JavaScript

const require_runtime = require('../../_virtual/_rolldown/runtime.cjs');
let react = require("react");
//#region src/react-renderer/registry/ComponentRegistry.ts
/**
* Registry for A2UI components. Allows registration of custom components
* and supports lazy loading for code splitting.
*
* @example
* ```tsx
* const registry = new ComponentRegistry();
*
* // Register a component directly
* registry.register('Text', { component: Text });
*
* // Register with lazy loading
* registry.register('Modal', {
* component: () => import('./components/Modal'),
* lazy: true
* });
*
* // Use with A2UIRenderer
* <A2UIRenderer surfaceId="main" registry={registry} />
* ```
*/
var ComponentRegistry = class ComponentRegistry {
constructor() {
this.registry = /* @__PURE__ */ new Map();
this.lazyCache = /* @__PURE__ */ new Map();
}
static {
this._instance = null;
}
/**
* Get the singleton instance of the registry.
* Use this for the default global registry.
*/
static getInstance() {
if (!ComponentRegistry._instance) ComponentRegistry._instance = new ComponentRegistry();
return ComponentRegistry._instance;
}
/**
* Reset the singleton instance.
* Useful for testing.
*/
static resetInstance() {
ComponentRegistry._instance = null;
}
/**
* Register a component type.
*
* @param type - The A2UI component type name (e.g., 'Text', 'Button')
* @param registration - The component registration
*/
register(type, registration) {
this.registry.set(type, registration);
}
/**
* Unregister a component type.
*
* @param type - The component type to unregister
*/
unregister(type) {
this.registry.delete(type);
this.lazyCache.delete(type);
}
/**
* Check if a component type is registered.
*
* @param type - The component type to check
* @returns True if the component is registered
*/
has(type) {
return this.registry.has(type);
}
/**
* Get a component by type. If the component is registered with lazy loading,
* returns a React.lazy wrapped component.
*
* @param type - The component type to get
* @returns The React component, or null if not found
*/
get(type) {
const registration = this.registry.get(type);
if (!registration) return null;
if (registration.lazy && typeof registration.component === "function") {
const cached = this.lazyCache.get(type);
if (cached) return cached;
const lazyComponent = (0, react.lazy)(registration.component);
this.lazyCache.set(type, lazyComponent);
return lazyComponent;
}
return registration.component;
}
/**
* Get all registered component types.
*
* @returns Array of registered type names
*/
getRegisteredTypes() {
return Array.from(this.registry.keys());
}
/**
* Clear all registrations.
*/
clear() {
this.registry.clear();
this.lazyCache.clear();
}
};
//#endregion
exports.ComponentRegistry = ComponentRegistry;
//# sourceMappingURL=ComponentRegistry.cjs.map