Skip to content

Commit 37789d7

Browse files
authored
Replace auto-discovery with manual component registration v0.2.0 (#3)
BREAKING CHANGES: - Remove ArizonaSvelteDiscovery class and auto-discovery functionality - Remove componentsDir and pattern constructor options - Remove init() and getDiscovery() methods NEW FEATURES: - Add registerComponents() method for batch component registration - Add components option to constructor for immediate registration - Simplified API focused on manual component management IMPROVEMENTS: - Update README with new registration patterns and index.js approach - Add comprehensive JSDoc documentation with examples - Add tests for new batch registration functionality - Remove dependency on import.meta.glob static path limitations This version provides more control and reliability for component registration while removing the brittle auto-discovery system that couldn't work across different project structures. Users should now register components explicitly: new ArizonaSvelte({ components: { Counter, HelloWorld } })
1 parent cfcf1c9 commit 37789d7

11 files changed

+427
-466
lines changed

README.md

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,28 @@ my_page(_Bindings) ->
9494
""").
9595
```
9696

97-
### 2. JavaScript Side - Initialize Svelte Integration
97+
### 2. JavaScript Side - Register Components and Initialize
9898

9999
```javascript
100100
// assets/js/main.js
101101
import ArizonaSvelte from '@arizona-framework/svelte';
102+
import Counter from '../svelte/components/Counter.svelte';
103+
import HelloWorld from '../svelte/components/HelloWorld.svelte';
104+
105+
// Option 1: Register components in constructor (recommended)
106+
const arizonaSvelte = new ArizonaSvelte({
107+
components: {
108+
Counter,
109+
HelloWorld
110+
}
111+
});
102112

103-
// Initialize Arizona Svelte
104-
const arizonaSvelte = new ArizonaSvelte();
113+
// Option 2: Register components manually
114+
// const arizonaSvelte = new ArizonaSvelte();
115+
// arizonaSvelte.registerComponents({
116+
// Counter,
117+
// HelloWorld
118+
// });
105119

106120
// Start automatic component lifecycle monitoring
107121
arizonaSvelte.startMonitoring({
@@ -115,6 +129,28 @@ arizonaSvelte.startMonitoring({
115129
globalThis.arizonaSvelte = arizonaSvelte;
116130
```
117131

132+
### Alternative: Using an Index File
133+
134+
Create a centralized component index:
135+
136+
```javascript
137+
// svelte/components/index.js
138+
export { default as Counter } from './Counter.svelte';
139+
export { default as HelloWorld } from './HelloWorld.svelte';
140+
export { default as Dashboard } from './Dashboard.svelte';
141+
```
142+
143+
Then import all at once:
144+
145+
```javascript
146+
// assets/js/main.js
147+
import ArizonaSvelte from '@arizona-framework/svelte';
148+
import * as components from '../svelte/components/index.js';
149+
150+
const arizonaSvelte = new ArizonaSvelte({ components });
151+
arizonaSvelte.startMonitoring();
152+
```
153+
118154
### 3. Create Your Svelte Components
119155

120156
```svelte
@@ -136,9 +172,9 @@ globalThis.arizonaSvelte = arizonaSvelte;
136172

137173
- **🔄 Automatic Lifecycle Management**: Components mount/unmount automatically
138174
when DOM changes
139-
- **🏗️ Component Discovery**: Auto-discovers and registers Svelte components
175+
- **🏗️ Flexible Component Registration**: Register components via constructor or batch methods
140176
- **📡 Real-time Integration**: Works seamlessly with Arizona's WebSocket updates
141-
- **🎯 Minimal Configuration**: Just install and start monitoring
177+
- **🎯 Simple Setup**: Register components and start monitoring in a few lines
142178
- **🧪 Development Friendly**: Built-in logging and debugging support
143179
- **⚡ High Performance**: Zero-debounce monitoring for immediate responsiveness
144180

@@ -181,6 +217,30 @@ arizona_svelte:render_component(Component, Props) -> Template.
181217

182218
Creates a new Arizona Svelte instance.
183219

220+
**Options:**
221+
222+
- `components: Object` - Components to register on instantiation
223+
224+
```javascript
225+
const arizonaSvelte = new ArizonaSvelte({
226+
components: {
227+
Counter: CounterComponent,
228+
HelloWorld: HelloWorldComponent
229+
}
230+
});
231+
```
232+
233+
#### `arizonaSvelte.registerComponents(components)`
234+
235+
Register multiple components at once.
236+
237+
```javascript
238+
arizonaSvelte.registerComponents({
239+
Dashboard: DashboardComponent,
240+
Modal: ModalComponent
241+
});
242+
```
243+
184244
#### `arizonaSvelte.startMonitoring(options?)`
185245

186246
Starts automatic component lifecycle monitoring.
@@ -198,6 +258,9 @@ Starts automatic component lifecycle monitoring.
198258
- `arizonaSvelte.isMonitoring()` - Check if monitoring is active
199259
- `arizonaSvelte.getRegistry()` - Get component registry
200260
- `arizonaSvelte.getLifecycle()` - Get lifecycle manager
261+
- `arizonaSvelte.getComponent(name)` - Get a specific component
262+
- `arizonaSvelte.hasComponent(name)` - Check if component is registered
263+
- `arizonaSvelte.getComponentNames()` - Get all registered component names
201264

202265
## Requirements
203266

assets/js/arizona-svelte-discovery.js

Lines changed: 0 additions & 94 deletions
This file was deleted.

assets/js/arizona-svelte-discovery.test.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

assets/js/arizona-svelte-registry.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,31 @@ class ArizonaSvelteRegistry {
7171
return this.components.delete(name);
7272
}
7373

74+
/**
75+
* Register multiple components at once
76+
* @param {Object.<string, Function>} components - Object mapping component names to component classes
77+
* @returns {number} Number of components registered
78+
* @example
79+
* registry.registerComponents({
80+
* Counter: CounterComponent,
81+
* HelloWorld: HelloWorldComponent,
82+
* Dashboard: DashboardComponent
83+
* });
84+
*/
85+
registerComponents(components) {
86+
if (!components || typeof components !== 'object') {
87+
throw new Error('Components must be an object mapping names to component classes');
88+
}
89+
90+
let registeredCount = 0;
91+
for (const [name, component] of Object.entries(components)) {
92+
this.registerComponent(name, component);
93+
registeredCount++;
94+
}
95+
96+
return registeredCount;
97+
}
98+
7499
/**
75100
* Clear all registered components
76101
*/

assets/js/arizona-svelte-registry.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,20 @@ describe('ArizonaSvelteRegistry', () => {
4141

4242
expect(registry.getComponentNames()).toEqual([]);
4343
});
44+
45+
it('should register multiple components at once', () => {
46+
const components = {
47+
Counter: class Counter {},
48+
HelloWorld: class HelloWorld {},
49+
Dashboard: class Dashboard {},
50+
};
51+
52+
const count = registry.registerComponents(components);
53+
54+
expect(count).toBe(3);
55+
expect(registry.hasComponent('Counter')).toBe(true);
56+
expect(registry.hasComponent('HelloWorld')).toBe(true);
57+
expect(registry.hasComponent('Dashboard')).toBe(true);
58+
expect(registry.getComponentNames()).toEqual(['Counter', 'HelloWorld', 'Dashboard']);
59+
});
4460
});

0 commit comments

Comments
 (0)