@object-ui/core 0.3.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/CHANGELOG.md +11 -0
  3. package/dist/actions/ActionRunner.d.ts +228 -4
  4. package/dist/actions/ActionRunner.js +397 -45
  5. package/dist/actions/TransactionManager.d.ts +193 -0
  6. package/dist/actions/TransactionManager.js +410 -0
  7. package/dist/actions/index.d.ts +2 -1
  8. package/dist/actions/index.js +2 -1
  9. package/dist/adapters/ApiDataSource.d.ts +69 -0
  10. package/dist/adapters/ApiDataSource.js +293 -0
  11. package/dist/adapters/ValueDataSource.d.ts +55 -0
  12. package/dist/adapters/ValueDataSource.js +287 -0
  13. package/dist/adapters/index.d.ts +3 -0
  14. package/dist/adapters/index.js +5 -2
  15. package/dist/adapters/resolveDataSource.d.ts +40 -0
  16. package/dist/adapters/resolveDataSource.js +59 -0
  17. package/dist/data-scope/DataScopeManager.d.ts +127 -0
  18. package/dist/data-scope/DataScopeManager.js +229 -0
  19. package/dist/data-scope/index.d.ts +10 -0
  20. package/dist/data-scope/index.js +10 -0
  21. package/dist/evaluator/ExpressionCache.d.ts +101 -0
  22. package/dist/evaluator/ExpressionCache.js +135 -0
  23. package/dist/evaluator/ExpressionEvaluator.d.ts +30 -2
  24. package/dist/evaluator/ExpressionEvaluator.js +60 -16
  25. package/dist/evaluator/FormulaFunctions.d.ts +58 -0
  26. package/dist/evaluator/FormulaFunctions.js +350 -0
  27. package/dist/evaluator/index.d.ts +4 -2
  28. package/dist/evaluator/index.js +4 -2
  29. package/dist/index.d.ts +14 -7
  30. package/dist/index.js +13 -9
  31. package/dist/query/index.d.ts +6 -0
  32. package/dist/query/index.js +6 -0
  33. package/dist/query/query-ast.d.ts +32 -0
  34. package/dist/query/query-ast.js +268 -0
  35. package/dist/registry/PluginScopeImpl.d.ts +80 -0
  36. package/dist/registry/PluginScopeImpl.js +243 -0
  37. package/dist/registry/PluginSystem.d.ts +66 -0
  38. package/dist/registry/PluginSystem.js +142 -0
  39. package/dist/registry/Registry.d.ts +83 -4
  40. package/dist/registry/Registry.js +113 -7
  41. package/dist/registry/WidgetRegistry.d.ts +120 -0
  42. package/dist/registry/WidgetRegistry.js +275 -0
  43. package/dist/theme/ThemeEngine.d.ts +82 -0
  44. package/dist/theme/ThemeEngine.js +400 -0
  45. package/dist/theme/index.d.ts +8 -0
  46. package/dist/theme/index.js +8 -0
  47. package/dist/validation/index.d.ts +9 -0
  48. package/dist/validation/index.js +9 -0
  49. package/dist/validation/validation-engine.d.ts +88 -0
  50. package/dist/validation/validation-engine.js +428 -0
  51. package/dist/validation/validators/index.d.ts +16 -0
  52. package/dist/validation/validators/index.js +16 -0
  53. package/dist/validation/validators/object-validation-engine.d.ts +118 -0
  54. package/dist/validation/validators/object-validation-engine.js +538 -0
  55. package/package.json +14 -5
  56. package/src/actions/ActionRunner.ts +577 -55
  57. package/src/actions/TransactionManager.ts +521 -0
  58. package/src/actions/__tests__/ActionRunner.params.test.ts +134 -0
  59. package/src/actions/__tests__/ActionRunner.test.ts +711 -0
  60. package/src/actions/__tests__/TransactionManager.test.ts +447 -0
  61. package/src/actions/index.ts +2 -1
  62. package/src/adapters/ApiDataSource.ts +349 -0
  63. package/src/adapters/ValueDataSource.ts +332 -0
  64. package/src/adapters/__tests__/ApiDataSource.test.ts +418 -0
  65. package/src/adapters/__tests__/ValueDataSource.test.ts +325 -0
  66. package/src/adapters/__tests__/resolveDataSource.test.ts +144 -0
  67. package/src/adapters/index.ts +6 -1
  68. package/src/adapters/resolveDataSource.ts +79 -0
  69. package/src/builder/__tests__/schema-builder.test.ts +235 -0
  70. package/src/data-scope/DataScopeManager.ts +269 -0
  71. package/src/data-scope/__tests__/DataScopeManager.test.ts +211 -0
  72. package/src/data-scope/index.ts +16 -0
  73. package/src/evaluator/ExpressionCache.ts +192 -0
  74. package/src/evaluator/ExpressionEvaluator.ts +61 -16
  75. package/src/evaluator/FormulaFunctions.ts +398 -0
  76. package/src/evaluator/__tests__/ExpressionCache.test.ts +135 -0
  77. package/src/evaluator/__tests__/ExpressionContext.test.ts +110 -0
  78. package/src/evaluator/__tests__/FormulaFunctions.test.ts +447 -0
  79. package/src/evaluator/index.ts +4 -2
  80. package/src/index.ts +14 -10
  81. package/src/query/__tests__/query-ast.test.ts +211 -0
  82. package/src/query/__tests__/window-functions.test.ts +275 -0
  83. package/src/query/index.ts +7 -0
  84. package/src/query/query-ast.ts +341 -0
  85. package/src/registry/PluginScopeImpl.ts +259 -0
  86. package/src/registry/PluginSystem.ts +161 -0
  87. package/src/registry/Registry.ts +136 -8
  88. package/src/registry/WidgetRegistry.ts +316 -0
  89. package/src/registry/__tests__/PluginSystem.test.ts +226 -0
  90. package/src/registry/__tests__/Registry.test.ts +293 -0
  91. package/src/registry/__tests__/WidgetRegistry.test.ts +321 -0
  92. package/src/registry/__tests__/plugin-scope-integration.test.ts +283 -0
  93. package/src/theme/ThemeEngine.ts +452 -0
  94. package/src/theme/__tests__/ThemeEngine.test.ts +606 -0
  95. package/src/theme/index.ts +22 -0
  96. package/src/validation/__tests__/object-validation-engine.test.ts +567 -0
  97. package/src/validation/__tests__/schema-validator.test.ts +118 -0
  98. package/src/validation/__tests__/validation-engine.test.ts +102 -0
  99. package/src/validation/index.ts +10 -0
  100. package/src/validation/validation-engine.ts +520 -0
  101. package/src/validation/validators/index.ts +25 -0
  102. package/src/validation/validators/object-validation-engine.ts +722 -0
  103. package/tsconfig.tsbuildinfo +1 -1
  104. package/vitest.config.ts +2 -0
  105. package/src/adapters/index.d.ts +0 -8
  106. package/src/adapters/index.js +0 -10
  107. package/src/builder/schema-builder.d.ts +0 -294
  108. package/src/builder/schema-builder.js +0 -503
  109. package/src/index.d.ts +0 -13
  110. package/src/index.js +0 -16
  111. package/src/registry/Registry.d.ts +0 -56
  112. package/src/registry/Registry.js +0 -43
  113. package/src/types/index.d.ts +0 -19
  114. package/src/types/index.js +0 -8
  115. package/src/utils/filter-converter.d.ts +0 -57
  116. package/src/utils/filter-converter.js +0 -100
  117. package/src/validation/schema-validator.d.ts +0 -94
  118. package/src/validation/schema-validator.js +0 -278
@@ -0,0 +1,259 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ /**
10
+ * @object-ui/core - Plugin Scope Implementation
11
+ *
12
+ * Section 3.3: Implementation of scoped plugin system to prevent conflicts.
13
+ * Provides isolated component registration, state management, and event bus.
14
+ *
15
+ * @module plugin-scope-impl
16
+ * @packageDocumentation
17
+ */
18
+
19
+ import type {
20
+ PluginScope,
21
+ PluginScopeConfig,
22
+ PluginEventHandler
23
+ } from '@object-ui/types';
24
+ import type { Registry, ComponentMeta as RegistryComponentMeta } from './Registry.js';
25
+
26
+ /**
27
+ * Event Bus for scoped plugin events
28
+ */
29
+ class EventBus {
30
+ private listeners = new Map<string, Set<PluginEventHandler>>();
31
+
32
+ on(event: string, handler: PluginEventHandler): () => void {
33
+ if (!this.listeners.has(event)) {
34
+ this.listeners.set(event, new Set());
35
+ }
36
+ this.listeners.get(event)!.add(handler);
37
+
38
+ // Return unsubscribe function
39
+ return () => {
40
+ this.listeners.get(event)?.delete(handler);
41
+ if (this.listeners.get(event)?.size === 0) {
42
+ this.listeners.delete(event);
43
+ }
44
+ };
45
+ }
46
+
47
+ emit(event: string, data?: any): void {
48
+ const handlers = this.listeners.get(event);
49
+ if (handlers) {
50
+ handlers.forEach(handler => {
51
+ try {
52
+ handler(data);
53
+ } catch (error) {
54
+ console.error(`Error in event handler for "${event}":`, error);
55
+ }
56
+ });
57
+ }
58
+ }
59
+
60
+ cleanup(): void {
61
+ this.listeners.clear();
62
+ }
63
+ }
64
+
65
+ /**
66
+ * Global event bus for cross-plugin communication
67
+ */
68
+ const globalEventBus = new EventBus();
69
+
70
+ /**
71
+ * Plugin Scope Implementation
72
+ *
73
+ * Provides isolated access to registry, state, and events for each plugin.
74
+ */
75
+ export class PluginScopeImpl implements PluginScope {
76
+ public readonly name: string;
77
+ public readonly version: string;
78
+
79
+ private registry: Registry;
80
+ private state = new Map<string, any>();
81
+ private eventBus = new EventBus();
82
+ private config: Required<PluginScopeConfig>;
83
+
84
+ constructor(
85
+ name: string,
86
+ version: string,
87
+ registry: Registry,
88
+ config?: PluginScopeConfig
89
+ ) {
90
+ this.name = name;
91
+ this.version = version;
92
+ this.registry = registry;
93
+ this.config = {
94
+ enableStateIsolation: config?.enableStateIsolation ?? true,
95
+ enableEventIsolation: config?.enableEventIsolation ?? true,
96
+ allowGlobalEvents: config?.allowGlobalEvents ?? true,
97
+ maxStateSize: config?.maxStateSize ?? 5 * 1024 * 1024, // 5MB
98
+ };
99
+ }
100
+
101
+ /**
102
+ * Register a component in the scoped namespace
103
+ */
104
+ registerComponent(type: string, component: any, meta?: any): void {
105
+ // Components are registered as "pluginName:type"
106
+ const registryMeta: RegistryComponentMeta = {
107
+ ...meta,
108
+ namespace: this.name,
109
+ };
110
+ this.registry.register(type, component, registryMeta);
111
+ }
112
+
113
+ /**
114
+ * Get a component from the scoped namespace
115
+ */
116
+ getComponent(type: string): any | undefined {
117
+ // First try scoped lookup
118
+ const scoped = this.registry.get(`${this.name}:${type}`);
119
+ if (scoped) {
120
+ return scoped;
121
+ }
122
+
123
+ // Fall back to global lookup
124
+ return this.registry.get(type);
125
+ }
126
+
127
+ /**
128
+ * Scoped state management
129
+ */
130
+ useState<T>(key: string, initialValue: T): [T, (value: T | ((prev: T) => T)) => void] {
131
+ if (!this.config.enableStateIsolation) {
132
+ throw new Error('State isolation is disabled for this plugin');
133
+ }
134
+
135
+ // Initialize state if not present
136
+ if (!this.state.has(key)) {
137
+ this.setState(key, initialValue);
138
+ }
139
+
140
+ const currentValue = this.getState<T>(key) ?? initialValue;
141
+
142
+ const setValue = (value: T | ((prev: T) => T)) => {
143
+ const newValue = typeof value === 'function'
144
+ ? (value as (prev: T) => T)(this.getState<T>(key) ?? initialValue)
145
+ : value;
146
+ this.setState(key, newValue);
147
+ };
148
+
149
+ return [currentValue, setValue];
150
+ }
151
+
152
+ /**
153
+ * Get scoped state value
154
+ */
155
+ getState<T>(key: string): T | undefined {
156
+ return this.state.get(key);
157
+ }
158
+
159
+ /**
160
+ * Set scoped state value
161
+ */
162
+ setState<T>(key: string, value: T): void {
163
+ if (!this.config.enableStateIsolation) {
164
+ throw new Error('State isolation is disabled for this plugin');
165
+ }
166
+
167
+ // Check state size limit
168
+ const stateSize = this.estimateStateSize();
169
+ const valueSize = this.estimateValueSize(value);
170
+
171
+ if (stateSize + valueSize > this.config.maxStateSize) {
172
+ throw new Error(
173
+ `Plugin "${this.name}" exceeded maximum state size of ${this.config.maxStateSize} bytes`
174
+ );
175
+ }
176
+
177
+ this.state.set(key, value);
178
+ }
179
+
180
+ /**
181
+ * Subscribe to scoped events
182
+ */
183
+ on(event: string, handler: PluginEventHandler): () => void {
184
+ if (!this.config.enableEventIsolation) {
185
+ // If isolation is disabled, use global event bus
186
+ return this.onGlobal(event, handler);
187
+ }
188
+
189
+ // Scoped event: prefix with plugin name
190
+ const scopedEvent = `${this.name}:${event}`;
191
+ return this.eventBus.on(scopedEvent, handler);
192
+ }
193
+
194
+ /**
195
+ * Emit a scoped event
196
+ */
197
+ emit(event: string, data?: any): void {
198
+ if (!this.config.enableEventIsolation) {
199
+ // If isolation is disabled, emit globally
200
+ this.emitGlobal(event, data);
201
+ return;
202
+ }
203
+
204
+ // Scoped event: prefix with plugin name
205
+ const scopedEvent = `${this.name}:${event}`;
206
+ this.eventBus.emit(scopedEvent, data);
207
+ }
208
+
209
+ /**
210
+ * Emit a global event
211
+ */
212
+ emitGlobal(event: string, data?: any): void {
213
+ if (!this.config.allowGlobalEvents) {
214
+ throw new Error('Global events are disabled for this plugin');
215
+ }
216
+ globalEventBus.emit(event, data);
217
+ }
218
+
219
+ /**
220
+ * Subscribe to global events
221
+ */
222
+ onGlobal(event: string, handler: PluginEventHandler): () => void {
223
+ if (!this.config.allowGlobalEvents) {
224
+ throw new Error('Global events are disabled for this plugin');
225
+ }
226
+ return globalEventBus.on(event, handler);
227
+ }
228
+
229
+ /**
230
+ * Clean up all plugin resources
231
+ */
232
+ cleanup(): void {
233
+ this.state.clear();
234
+ this.eventBus.cleanup();
235
+ }
236
+
237
+ /**
238
+ * Estimate total state size in bytes
239
+ */
240
+ private estimateStateSize(): number {
241
+ let size = 0;
242
+ for (const value of this.state.values()) {
243
+ size += this.estimateValueSize(value);
244
+ }
245
+ return size;
246
+ }
247
+
248
+ /**
249
+ * Estimate size of a value in bytes
250
+ */
251
+ private estimateValueSize(value: any): number {
252
+ try {
253
+ return JSON.stringify(value).length * 2; // UTF-16 encoding
254
+ } catch {
255
+ // If not serializable, use rough estimate
256
+ return 1024; // 1KB default
257
+ }
258
+ }
259
+ }
@@ -0,0 +1,161 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ import type { Registry } from './Registry.js';
10
+ import type { PluginScope, PluginScopeConfig } from '@object-ui/types';
11
+ import { PluginScopeImpl } from './PluginScopeImpl.js';
12
+
13
+ export interface PluginDefinition {
14
+ name: string;
15
+ version: string;
16
+ dependencies?: string[]; // Dependencies on other plugins
17
+ peerDependencies?: string[]; // Peer dependencies
18
+ register: (registry: Registry | PluginScope) => void; // Support both legacy and scoped registration
19
+ onLoad?: () => void | Promise<void>; // Lifecycle hook: called after registration
20
+ onUnload?: () => void | Promise<void>; // Lifecycle hook: called before unload
21
+ scopeConfig?: PluginScopeConfig; // Optional scope configuration
22
+ }
23
+
24
+ export class PluginSystem {
25
+ private plugins = new Map<string, PluginDefinition>();
26
+ private loaded = new Set<string>();
27
+ private scopes = new Map<string, PluginScopeImpl>();
28
+
29
+ /**
30
+ * Load a plugin into the system with optional scope isolation
31
+ * @param plugin The plugin definition to load
32
+ * @param registry The component registry to use for registration
33
+ * @param useScope Whether to use scoped loading (default: true for better isolation)
34
+ * @throws Error if dependencies are missing
35
+ */
36
+ async loadPlugin(plugin: PluginDefinition, registry: Registry, useScope: boolean = true): Promise<void> {
37
+ // Check if already loaded
38
+ if (this.loaded.has(plugin.name)) {
39
+ console.warn(`Plugin "${plugin.name}" is already loaded. Skipping.`);
40
+ return;
41
+ }
42
+
43
+ // Check dependencies
44
+ for (const dep of plugin.dependencies || []) {
45
+ if (!this.loaded.has(dep)) {
46
+ throw new Error(`Missing dependency: ${dep} required by ${plugin.name}`);
47
+ }
48
+ }
49
+
50
+ try {
51
+ if (useScope) {
52
+ // Create scoped environment for plugin
53
+ const scope = new PluginScopeImpl(
54
+ plugin.name,
55
+ plugin.version,
56
+ registry,
57
+ plugin.scopeConfig
58
+ );
59
+
60
+ // Store scope for cleanup
61
+ this.scopes.set(plugin.name, scope);
62
+
63
+ // Execute registration with scope
64
+ plugin.register(scope);
65
+ } else {
66
+ // Legacy mode: direct registry access
67
+ plugin.register(registry);
68
+ }
69
+
70
+ // Store plugin definition
71
+ this.plugins.set(plugin.name, plugin);
72
+
73
+ // Execute lifecycle hook
74
+ await plugin.onLoad?.();
75
+
76
+ // Mark as loaded
77
+ this.loaded.add(plugin.name);
78
+ } catch (error) {
79
+ // Clean up on failure
80
+ this.plugins.delete(plugin.name);
81
+ this.scopes.delete(plugin.name);
82
+ throw error;
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Unload a plugin from the system
88
+ * @param name The name of the plugin to unload
89
+ * @throws Error if other plugins depend on this plugin
90
+ */
91
+ async unloadPlugin(name: string): Promise<void> {
92
+ const plugin = this.plugins.get(name);
93
+ if (!plugin) {
94
+ throw new Error(`Plugin "${name}" is not loaded`);
95
+ }
96
+
97
+ // Check if any loaded plugins depend on this one
98
+ for (const [pluginName, pluginDef] of this.plugins.entries()) {
99
+ if (this.loaded.has(pluginName) && pluginDef.dependencies?.includes(name)) {
100
+ throw new Error(`Cannot unload plugin "${name}" - plugin "${pluginName}" depends on it`);
101
+ }
102
+ }
103
+
104
+ // Execute lifecycle hook
105
+ await plugin.onUnload?.();
106
+
107
+ // Clean up scope if exists
108
+ const scope = this.scopes.get(name);
109
+ if (scope) {
110
+ scope.cleanup();
111
+ this.scopes.delete(name);
112
+ }
113
+
114
+ // Remove from loaded set
115
+ this.loaded.delete(name);
116
+ this.plugins.delete(name);
117
+ }
118
+
119
+ /**
120
+ * Get the scope for a loaded plugin
121
+ * @param name The name of the plugin
122
+ * @returns The plugin scope or undefined
123
+ */
124
+ getScope(name: string): PluginScope | undefined {
125
+ return this.scopes.get(name);
126
+ }
127
+
128
+ /**
129
+ * Check if a plugin is loaded
130
+ * @param name The name of the plugin
131
+ * @returns true if the plugin is loaded
132
+ */
133
+ isLoaded(name: string): boolean {
134
+ return this.loaded.has(name);
135
+ }
136
+
137
+ /**
138
+ * Get a loaded plugin definition
139
+ * @param name The name of the plugin
140
+ * @returns The plugin definition or undefined
141
+ */
142
+ getPlugin(name: string): PluginDefinition | undefined {
143
+ return this.plugins.get(name);
144
+ }
145
+
146
+ /**
147
+ * Get all loaded plugin names
148
+ * @returns Array of loaded plugin names
149
+ */
150
+ getLoadedPlugins(): string[] {
151
+ return Array.from(this.loaded);
152
+ }
153
+
154
+ /**
155
+ * Get all plugin definitions
156
+ * @returns Array of all plugin definitions
157
+ */
158
+ getAllPlugins(): PluginDefinition[] {
159
+ return Array.from(this.plugins.values());
160
+ }
161
+ }
@@ -6,7 +6,7 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
 
9
- import type { SchemaNode } from '../types';
9
+ import type { SchemaNode } from '../types/index.js';
10
10
 
11
11
  export type ComponentRenderer<T = any> = T;
12
12
 
@@ -26,6 +26,17 @@ export type ComponentMeta = {
26
26
  label?: string; // Display name in designer
27
27
  icon?: string; // Icon name or svg string
28
28
  category?: string; // Grouping category
29
+ namespace?: string; // Component namespace (e.g., 'ui', 'plugin-grid', 'field')
30
+ /**
31
+ * When true, prevents the component from being registered with a non-namespaced fallback.
32
+ * Use this when a component should only be accessible via its full namespaced key.
33
+ * This avoids conflicts with other components that share the same base name.
34
+ *
35
+ * @example
36
+ * // Register as 'view:form' only, don't overwrite 'form'
37
+ * registry.register('form', FormView, { namespace: 'view', skipFallback: true });
38
+ */
39
+ skipFallback?: boolean;
29
40
  inputs?: ComponentInput[];
30
41
  defaultProps?: Record<string, any>; // Default props when dropped
31
42
  defaultChildren?: SchemaNode[]; // Default children when dropped
@@ -50,36 +61,153 @@ export type ComponentConfig<T = any> = ComponentMeta & {
50
61
  export class Registry<T = any> {
51
62
  private components = new Map<string, ComponentConfig<T>>();
52
63
 
64
+ /**
65
+ * Register a component with optional namespace support.
66
+ * If namespace is provided in meta, the component will be registered as "namespace:type".
67
+ *
68
+ * @param type - Component type identifier
69
+ * @param component - Component renderer
70
+ * @param meta - Component metadata (including optional namespace)
71
+ *
72
+ * @example
73
+ * // Register with namespace
74
+ * registry.register('button', ButtonComponent, { namespace: 'ui' });
75
+ * // Accessible as 'ui:button' or 'button' (fallback)
76
+ *
77
+ * @example
78
+ * // Register without namespace (backward compatible)
79
+ * registry.register('button', ButtonComponent);
80
+ * // Accessible as 'button'
81
+ */
53
82
  register(type: string, component: ComponentRenderer<T>, meta?: ComponentMeta) {
54
- if (this.components.has(type)) {
55
- console.warn(`Component type "${type}" is already registered. Overwriting.`);
83
+ const fullType = meta?.namespace ? `${meta.namespace}:${type}` : type;
84
+
85
+ // Warn if registering without namespace (deprecated pattern)
86
+ if (!meta?.namespace) {
87
+ console.warn(
88
+ `Registering component "${type}" without a namespace is deprecated. ` +
89
+ `Please provide a namespace in the meta parameter.`
90
+ );
56
91
  }
57
- this.components.set(type, {
58
- type,
92
+
93
+ if (this.components.has(fullType)) {
94
+ // console.warn(`Component type "${fullType}" is already registered. Overwriting.`);
95
+ }
96
+
97
+ this.components.set(fullType, {
98
+ type: fullType,
59
99
  component,
60
100
  ...meta
61
101
  });
102
+
103
+ // Also register without namespace for backward compatibility
104
+ // This allows "button" to work even when registered as "ui:button"
105
+ // Note: If multiple namespaced components share the same short name,
106
+ // the last registration wins for non-namespaced lookups
107
+ // Skip this if skipFallback is true to avoid overwriting other components
108
+ if (meta?.namespace && !meta?.skipFallback) {
109
+ this.components.set(type, {
110
+ type: fullType, // Keep reference to namespaced type
111
+ component,
112
+ ...meta
113
+ });
114
+ }
62
115
  }
63
116
 
64
- get(type: string): ComponentRenderer<T> | undefined {
117
+ /**
118
+ * Get a component by type. Supports both namespaced and non-namespaced lookups.
119
+ *
120
+ * @param type - Component type (e.g., 'button' or 'ui:button')
121
+ * @param namespace - Optional namespace for lookup priority
122
+ * @returns Component renderer or undefined
123
+ *
124
+ * @example
125
+ * // Direct lookup
126
+ * registry.get('ui:button') // Gets ui:button
127
+ *
128
+ * @example
129
+ * // Fallback lookup
130
+ * registry.get('button') // Gets first registered button
131
+ *
132
+ * @example
133
+ * // Namespaced lookup with priority
134
+ * registry.get('button', 'ui') // Tries 'ui:button' first, then 'button'
135
+ */
136
+ get(type: string, namespace?: string): ComponentRenderer<T> | undefined {
137
+ // If namespace is explicitly provided, ONLY look in that namespace (no fallback)
138
+ if (namespace) {
139
+ const namespacedType = `${namespace}:${type}`;
140
+ return this.components.get(namespacedType)?.component;
141
+ }
142
+
143
+ // When no namespace provided, use backward compatibility lookup
65
144
  return this.components.get(type)?.component;
66
145
  }
67
146
 
68
- getConfig(type: string): ComponentConfig<T> | undefined {
147
+ /**
148
+ * Get component configuration by type with namespace support.
149
+ *
150
+ * @param type - Component type (e.g., 'button' or 'ui:button')
151
+ * @param namespace - Optional namespace for lookup priority
152
+ * @returns Component configuration or undefined
153
+ */
154
+ getConfig(type: string, namespace?: string): ComponentConfig<T> | undefined {
155
+ // If namespace is explicitly provided, ONLY look in that namespace (no fallback)
156
+ if (namespace) {
157
+ const namespacedType = `${namespace}:${type}`;
158
+ return this.components.get(namespacedType);
159
+ }
160
+
161
+ // When no namespace provided, use backward compatibility lookup
69
162
  return this.components.get(type);
70
163
  }
71
164
 
72
- has(type: string): boolean {
165
+ /**
166
+ * Check if a component type is registered.
167
+ *
168
+ * @param type - Component type (e.g., 'button' or 'ui:button')
169
+ * @param namespace - Optional namespace for lookup
170
+ * @returns True if component is registered
171
+ */
172
+ has(type: string, namespace?: string): boolean {
173
+ // If namespace is explicitly provided, ONLY look in that namespace (no fallback)
174
+ if (namespace) {
175
+ const namespacedType = `${namespace}:${type}`;
176
+ return this.components.has(namespacedType);
177
+ }
178
+ // When no namespace provided, use backward compatibility lookup
73
179
  return this.components.has(type);
74
180
  }
75
181
 
182
+ /**
183
+ * Get all registered component types.
184
+ *
185
+ * @returns Array of all component type identifiers
186
+ */
76
187
  getAllTypes(): string[] {
77
188
  return Array.from(this.components.keys());
78
189
  }
79
190
 
191
+ /**
192
+ * Get all registered component configurations.
193
+ *
194
+ * @returns Array of all component configurations
195
+ */
80
196
  getAllConfigs(): ComponentConfig<T>[] {
81
197
  return Array.from(this.components.values());
82
198
  }
199
+
200
+ /**
201
+ * Get all components in a specific namespace.
202
+ *
203
+ * @param namespace - Namespace to filter by
204
+ * @returns Array of component configurations in the namespace
205
+ */
206
+ getNamespaceComponents(namespace: string): ComponentConfig<T>[] {
207
+ return Array.from(this.components.values()).filter(
208
+ config => config.namespace === namespace
209
+ );
210
+ }
83
211
  }
84
212
 
85
213
  export const ComponentRegistry = new Registry<any>();