kimu-core 0.4.1 → 0.5.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.
- package/.editorconfig +116 -30
- package/.gitattributes +81 -11
- package/.github/FUNDING.yml +8 -8
- package/.github/kimu-copilot-instructions.md +3779 -3779
- package/.github/workflows/deploy-demo.yml +39 -39
- package/.nvmrc +1 -0
- package/.prettierignore +44 -0
- package/.prettierrc +16 -0
- package/FUNDING.md +31 -31
- package/icon.svg +10 -10
- package/kimu-core-0.5.0.tgz +0 -0
- package/package.json +10 -3
- package/scripts/minify-css-assets.js +82 -82
- package/src/core/index.ts +47 -47
- package/src/core/kimu-global-styles.ts +136 -136
- package/src/core/kimu-reactive.ts +196 -196
- package/src/extensions/{kimu-home → app-root}/component.ts +5 -5
- package/src/extensions/extensions-manifest.json +4 -4
- package/src/main.ts +3 -3
- package/src/modules-repository/api-axios/CHANGELOG.md +48 -48
- package/src/modules-repository/api-axios/QUICK-REFERENCE.md +178 -178
- package/src/modules-repository/api-axios/README.md +304 -304
- package/src/modules-repository/api-axios/api-axios-service.ts +355 -355
- package/src/modules-repository/api-axios/examples.ts +293 -293
- package/src/modules-repository/api-axios/index.ts +19 -19
- package/src/modules-repository/api-axios/interfaces.ts +71 -71
- package/src/modules-repository/api-axios/module.ts +41 -41
- package/src/modules-repository/api-core/CHANGELOG.md +42 -42
- package/src/modules-repository/api-core/QUICK-REFERENCE.md +192 -192
- package/src/modules-repository/api-core/README.md +435 -435
- package/src/modules-repository/api-core/api-core-service.ts +289 -289
- package/src/modules-repository/api-core/examples.ts +432 -432
- package/src/modules-repository/api-core/index.ts +8 -8
- package/src/modules-repository/api-core/interfaces.ts +83 -83
- package/src/modules-repository/api-core/module.ts +30 -30
- package/src/modules-repository/event-bus/README.md +273 -273
- package/src/modules-repository/event-bus/event-bus-service.ts +176 -176
- package/src/modules-repository/event-bus/module.ts +30 -30
- package/src/modules-repository/notification/README.md +423 -423
- package/src/modules-repository/notification/module.ts +30 -30
- package/src/modules-repository/notification/notification-service.ts +436 -436
- package/src/modules-repository/router/README.it.md +61 -10
- package/src/modules-repository/router/README.md +61 -10
- package/src/modules-repository/router/router-config.ts.example +61 -0
- package/src/modules-repository/router/router.ts +18 -0
- package/src/modules-repository/state/README.md +409 -409
- package/src/modules-repository/state/module.ts +30 -30
- package/src/modules-repository/state/state-service.ts +296 -296
- package/src/modules-repository/theme/README.md +311 -267
- package/src/modules-repository/theme/module.ts +30 -30
- package/src/modules-repository/theme/pre-build.js +40 -40
- package/src/modules-repository/theme/theme-service.ts +411 -389
- package/src/modules-repository/theme/themes/theme-cherry-blossom.css +78 -78
- package/src/modules-repository/theme/themes/theme-cozy.css +111 -111
- package/src/modules-repository/theme/themes/theme-cyberpunk.css +150 -150
- package/src/modules-repository/theme/themes/theme-dark.css +79 -79
- package/src/modules-repository/theme/themes/theme-forest.css +171 -171
- package/src/modules-repository/theme/themes/theme-gold.css +100 -100
- package/src/modules-repository/theme/themes/theme-high-contrast.css +126 -126
- package/src/modules-repository/theme/themes/theme-lava.css +101 -101
- package/src/modules-repository/theme/themes/theme-lavender.css +90 -90
- package/src/modules-repository/theme/themes/theme-light.css +79 -79
- package/src/modules-repository/theme/themes/theme-matrix.css +103 -103
- package/src/modules-repository/theme/themes/theme-midnight.css +81 -81
- package/src/modules-repository/theme/themes/theme-nord.css +94 -94
- package/src/modules-repository/theme/themes/theme-ocean.css +84 -84
- package/src/modules-repository/theme/themes/theme-retro80s.css +343 -343
- package/src/modules-repository/theme/themes/theme-sunset.css +62 -62
- package/src/modules-repository/theme/themes-config-default.json +19 -0
- package/src/modules-repository/theme/themes-config.d.ts +27 -27
- package/src/modules-repository/theme/{themes-config.json → themes-config.json.example} +223 -213
- /package/src/extensions/{kimu-home → app-root}/lang/en.json +0 -0
- /package/src/extensions/{kimu-home → app-root}/lang/it.json +0 -0
- /package/src/extensions/{kimu-home → app-root}/style.css +0 -0
- /package/src/extensions/{kimu-home → app-root}/view.html +0 -0
|
@@ -1,273 +1,273 @@
|
|
|
1
|
-
# Event Bus Module
|
|
2
|
-
|
|
3
|
-
## Overview
|
|
4
|
-
The Event Bus module provides a global publish/subscribe mechanism for component communication in KIMU-Core applications. It enables loose coupling between components by allowing them to emit and listen to custom events without direct references.
|
|
5
|
-
|
|
6
|
-
## Features
|
|
7
|
-
- ✅ **Global event bus** - Centralized communication hub
|
|
8
|
-
- ✅ **Type-safe callbacks** - TypeScript support for event handlers
|
|
9
|
-
- ✅ **Auto-unsubscribe** - Subscription objects with cleanup
|
|
10
|
-
- ✅ **Once subscriptions** - Auto-unsubscribe after first event
|
|
11
|
-
- ✅ **Debug mode** - Optional logging for development
|
|
12
|
-
- ✅ **Error handling** - Isolated callback errors don't affect other subscribers
|
|
13
|
-
- ✅ **Event introspection** - Query registered events and subscriber counts
|
|
14
|
-
|
|
15
|
-
## Installation
|
|
16
|
-
The Event Bus module is included in KIMU-Core. No additional installation required.
|
|
17
|
-
|
|
18
|
-
## Usage
|
|
19
|
-
|
|
20
|
-
### Basic Example
|
|
21
|
-
```typescript
|
|
22
|
-
import { eventBusService } from './modules/event-bus/event-bus-service';
|
|
23
|
-
|
|
24
|
-
// Subscribe to an event
|
|
25
|
-
const subscription = eventBusService.on('user:login', (user) => {
|
|
26
|
-
console.log('User logged in:', user);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
// Emit an event
|
|
30
|
-
eventBusService.emit('user:login', { id: 1, name: 'John Doe' });
|
|
31
|
-
|
|
32
|
-
// Unsubscribe when done
|
|
33
|
-
subscription.unsubscribe();
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
### In a Component
|
|
37
|
-
```typescript
|
|
38
|
-
import { KimuComponent } from '../core/kimu-component';
|
|
39
|
-
import { KimuComponentElement } from '../core/kimu-component-element';
|
|
40
|
-
import { eventBusService } from '../modules/event-bus/event-bus-service';
|
|
41
|
-
|
|
42
|
-
@KimuComponent({
|
|
43
|
-
tag: 'my-component',
|
|
44
|
-
name: 'My Component',
|
|
45
|
-
// ...
|
|
46
|
-
})
|
|
47
|
-
export class MyComponent extends KimuComponentElement {
|
|
48
|
-
private subscriptions: any[] = [];
|
|
49
|
-
|
|
50
|
-
onInit() {
|
|
51
|
-
// Subscribe to events
|
|
52
|
-
this.subscriptions.push(
|
|
53
|
-
eventBusService.on('data:updated', this.handleDataUpdate.bind(this))
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
this.subscriptions.push(
|
|
57
|
-
eventBusService.on('theme:changed', this.handleThemeChange.bind(this))
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
handleDataUpdate(data: any) {
|
|
62
|
-
console.log('Data updated:', data);
|
|
63
|
-
this.onRender();
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
handleThemeChange(theme: string) {
|
|
67
|
-
console.log('Theme changed:', theme);
|
|
68
|
-
// Update UI based on theme
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Emit events
|
|
72
|
-
saveData() {
|
|
73
|
-
// ... save logic
|
|
74
|
-
eventBusService.emit('data:saved', { timestamp: Date.now() });
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Clean up on component destroy
|
|
78
|
-
disconnectedCallback() {
|
|
79
|
-
this.subscriptions.forEach(sub => sub.unsubscribe());
|
|
80
|
-
super.disconnectedCallback();
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
### One-time Events
|
|
86
|
-
```typescript
|
|
87
|
-
// Subscribe only once
|
|
88
|
-
eventBusService.once('app:initialized', () => {
|
|
89
|
-
console.log('App initialized!');
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
// This will trigger the callback and auto-unsubscribe
|
|
93
|
-
eventBusService.emit('app:initialized');
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
### Debug Mode
|
|
97
|
-
```typescript
|
|
98
|
-
// Enable debug logging
|
|
99
|
-
eventBusService.setDebugMode(true);
|
|
100
|
-
|
|
101
|
-
// All subscriptions, emissions, and unsubscriptions will be logged
|
|
102
|
-
eventBusService.on('test:event', () => {});
|
|
103
|
-
// Console: [EventBus] Subscribed to: test:event
|
|
104
|
-
|
|
105
|
-
eventBusService.emit('test:event', 'data');
|
|
106
|
-
// Console: [EventBus] Emitting: test:event ['data']
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
### Event Introspection
|
|
110
|
-
```typescript
|
|
111
|
-
// Get list of all registered events
|
|
112
|
-
const events = eventBusService.getEventList();
|
|
113
|
-
console.log('Active events:', events);
|
|
114
|
-
|
|
115
|
-
// Check subscriber count
|
|
116
|
-
const count = eventBusService.getSubscriberCount('user:login');
|
|
117
|
-
console.log(`Subscribers for user:login: ${count}`);
|
|
118
|
-
|
|
119
|
-
// Check if event has subscribers
|
|
120
|
-
if (eventBusService.hasSubscribers('data:updated')) {
|
|
121
|
-
console.log('Someone is listening to data:updated');
|
|
122
|
-
}
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
## API Reference
|
|
126
|
-
|
|
127
|
-
### `eventBusService.on(event, callback)`
|
|
128
|
-
Subscribe to an event.
|
|
129
|
-
- **event**: `string` - Event name
|
|
130
|
-
- **callback**: `Function` - Handler function
|
|
131
|
-
- **Returns**: `EventSubscription` - Subscription object with `unsubscribe()` method
|
|
132
|
-
|
|
133
|
-
### `eventBusService.once(event, callback)`
|
|
134
|
-
Subscribe to an event once (auto-unsubscribe after first emission).
|
|
135
|
-
- **event**: `string` - Event name
|
|
136
|
-
- **callback**: `Function` - Handler function
|
|
137
|
-
- **Returns**: `EventSubscription` - Subscription object
|
|
138
|
-
|
|
139
|
-
### `eventBusService.emit(event, ...args)`
|
|
140
|
-
Emit an event to all subscribers.
|
|
141
|
-
- **event**: `string` - Event name
|
|
142
|
-
- **args**: `any[]` - Arguments to pass to callbacks
|
|
143
|
-
|
|
144
|
-
### `eventBusService.off(event, callback)`
|
|
145
|
-
Unsubscribe from an event.
|
|
146
|
-
- **event**: `string` - Event name
|
|
147
|
-
- **callback**: `Function` - Callback to remove
|
|
148
|
-
|
|
149
|
-
### `eventBusService.clear(event?)`
|
|
150
|
-
Remove all subscribers for a specific event or all events.
|
|
151
|
-
- **event**: `string` (optional) - Event name
|
|
152
|
-
|
|
153
|
-
### `eventBusService.setDebugMode(enabled)`
|
|
154
|
-
Enable or disable debug logging.
|
|
155
|
-
- **enabled**: `boolean` - Debug mode state
|
|
156
|
-
|
|
157
|
-
### `eventBusService.getEventList()`
|
|
158
|
-
Get list of all registered events.
|
|
159
|
-
- **Returns**: `string[]` - Array of event names
|
|
160
|
-
|
|
161
|
-
### `eventBusService.getSubscriberCount(event)`
|
|
162
|
-
Get number of subscribers for a specific event.
|
|
163
|
-
- **event**: `string` - Event name
|
|
164
|
-
- **Returns**: `number` - Subscriber count
|
|
165
|
-
|
|
166
|
-
### `eventBusService.hasSubscribers(event)`
|
|
167
|
-
Check if an event has any subscribers.
|
|
168
|
-
- **event**: `string` - Event name
|
|
169
|
-
- **Returns**: `boolean` - True if event has subscribers
|
|
170
|
-
|
|
171
|
-
## Event Naming Conventions
|
|
172
|
-
Use namespaced event names for better organization:
|
|
173
|
-
- `user:login`, `user:logout`, `user:updated`
|
|
174
|
-
- `data:loaded`, `data:saved`, `data:error`
|
|
175
|
-
- `ui:theme-changed`, `ui:modal-opened`, `ui:notification`
|
|
176
|
-
- `app:initialized`, `app:ready`, `app:shutdown`
|
|
177
|
-
|
|
178
|
-
## Best Practices
|
|
179
|
-
|
|
180
|
-
### 1. Always Unsubscribe
|
|
181
|
-
```typescript
|
|
182
|
-
class MyComponent extends KimuComponentElement {
|
|
183
|
-
private subscriptions: any[] = [];
|
|
184
|
-
|
|
185
|
-
onInit() {
|
|
186
|
-
this.subscriptions.push(
|
|
187
|
-
eventBusService.on('event', this.handler.bind(this))
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
disconnectedCallback() {
|
|
192
|
-
this.subscriptions.forEach(sub => sub.unsubscribe());
|
|
193
|
-
super.disconnectedCallback();
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
### 2. Use Namespaced Events
|
|
199
|
-
```typescript
|
|
200
|
-
// Good
|
|
201
|
-
eventBusService.emit('user:profile:updated', userData);
|
|
202
|
-
eventBusService.emit('notification:success', message);
|
|
203
|
-
|
|
204
|
-
// Avoid generic names
|
|
205
|
-
eventBusService.emit('update', data); // Too generic!
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
### 3. Document Event Payloads
|
|
209
|
-
```typescript
|
|
210
|
-
/**
|
|
211
|
-
* Event: user:login
|
|
212
|
-
* Payload: { id: number, name: string, email: string }
|
|
213
|
-
*/
|
|
214
|
-
eventBusService.emit('user:login', { id: 1, name: 'John', email: 'john@example.com' });
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
### 4. Handle Errors
|
|
218
|
-
```typescript
|
|
219
|
-
eventBusService.on('data:process', (data) => {
|
|
220
|
-
try {
|
|
221
|
-
processData(data);
|
|
222
|
-
} catch (error) {
|
|
223
|
-
eventBusService.emit('data:error', error);
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
```
|
|
227
|
-
|
|
228
|
-
## Common Use Cases
|
|
229
|
-
|
|
230
|
-
### 1. Component Communication
|
|
231
|
-
```typescript
|
|
232
|
-
// Component A emits
|
|
233
|
-
eventBusService.emit('cart:item-added', { id: 123, name: 'Product' });
|
|
234
|
-
|
|
235
|
-
// Component B listens
|
|
236
|
-
eventBusService.on('cart:item-added', (item) => {
|
|
237
|
-
updateCartCount();
|
|
238
|
-
});
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
### 2. State Synchronization
|
|
242
|
-
```typescript
|
|
243
|
-
// When state changes
|
|
244
|
-
eventBusService.emit('state:updated', newState);
|
|
245
|
-
|
|
246
|
-
// Components react to state changes
|
|
247
|
-
eventBusService.on('state:updated', (state) => {
|
|
248
|
-
this.updateUI(state);
|
|
249
|
-
});
|
|
250
|
-
```
|
|
251
|
-
|
|
252
|
-
### 3. User Actions
|
|
253
|
-
```typescript
|
|
254
|
-
// Button click
|
|
255
|
-
eventBusService.emit('button:save-clicked', { form: 'userProfile' });
|
|
256
|
-
|
|
257
|
-
// Multiple handlers can react
|
|
258
|
-
eventBusService.on('button:save-clicked', validateForm);
|
|
259
|
-
eventBusService.on('button:save-clicked', showLoadingIndicator);
|
|
260
|
-
eventBusService.on('button:save-clicked', trackAnalytics);
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
## Performance Considerations
|
|
264
|
-
- Event bus is synchronous - handlers execute immediately
|
|
265
|
-
- Use wisely for frequently emitted events (e.g., scroll, mousemove)
|
|
266
|
-
- Consider debouncing or throttling for high-frequency events
|
|
267
|
-
- Always clean up subscriptions to prevent memory leaks
|
|
268
|
-
|
|
269
|
-
## License
|
|
270
|
-
This module is part of KIMU-Core and follows the same license (MPL-2.0).
|
|
271
|
-
|
|
272
|
-
## Version History
|
|
273
|
-
- **1.0.0** - Initial release with core event bus functionality
|
|
1
|
+
# Event Bus Module
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
The Event Bus module provides a global publish/subscribe mechanism for component communication in KIMU-Core applications. It enables loose coupling between components by allowing them to emit and listen to custom events without direct references.
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
- ✅ **Global event bus** - Centralized communication hub
|
|
8
|
+
- ✅ **Type-safe callbacks** - TypeScript support for event handlers
|
|
9
|
+
- ✅ **Auto-unsubscribe** - Subscription objects with cleanup
|
|
10
|
+
- ✅ **Once subscriptions** - Auto-unsubscribe after first event
|
|
11
|
+
- ✅ **Debug mode** - Optional logging for development
|
|
12
|
+
- ✅ **Error handling** - Isolated callback errors don't affect other subscribers
|
|
13
|
+
- ✅ **Event introspection** - Query registered events and subscriber counts
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
The Event Bus module is included in KIMU-Core. No additional installation required.
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
### Basic Example
|
|
21
|
+
```typescript
|
|
22
|
+
import { eventBusService } from './modules/event-bus/event-bus-service';
|
|
23
|
+
|
|
24
|
+
// Subscribe to an event
|
|
25
|
+
const subscription = eventBusService.on('user:login', (user) => {
|
|
26
|
+
console.log('User logged in:', user);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Emit an event
|
|
30
|
+
eventBusService.emit('user:login', { id: 1, name: 'John Doe' });
|
|
31
|
+
|
|
32
|
+
// Unsubscribe when done
|
|
33
|
+
subscription.unsubscribe();
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### In a Component
|
|
37
|
+
```typescript
|
|
38
|
+
import { KimuComponent } from '../core/kimu-component';
|
|
39
|
+
import { KimuComponentElement } from '../core/kimu-component-element';
|
|
40
|
+
import { eventBusService } from '../modules/event-bus/event-bus-service';
|
|
41
|
+
|
|
42
|
+
@KimuComponent({
|
|
43
|
+
tag: 'my-component',
|
|
44
|
+
name: 'My Component',
|
|
45
|
+
// ...
|
|
46
|
+
})
|
|
47
|
+
export class MyComponent extends KimuComponentElement {
|
|
48
|
+
private subscriptions: any[] = [];
|
|
49
|
+
|
|
50
|
+
onInit() {
|
|
51
|
+
// Subscribe to events
|
|
52
|
+
this.subscriptions.push(
|
|
53
|
+
eventBusService.on('data:updated', this.handleDataUpdate.bind(this))
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
this.subscriptions.push(
|
|
57
|
+
eventBusService.on('theme:changed', this.handleThemeChange.bind(this))
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
handleDataUpdate(data: any) {
|
|
62
|
+
console.log('Data updated:', data);
|
|
63
|
+
this.onRender();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
handleThemeChange(theme: string) {
|
|
67
|
+
console.log('Theme changed:', theme);
|
|
68
|
+
// Update UI based on theme
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Emit events
|
|
72
|
+
saveData() {
|
|
73
|
+
// ... save logic
|
|
74
|
+
eventBusService.emit('data:saved', { timestamp: Date.now() });
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Clean up on component destroy
|
|
78
|
+
disconnectedCallback() {
|
|
79
|
+
this.subscriptions.forEach(sub => sub.unsubscribe());
|
|
80
|
+
super.disconnectedCallback();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### One-time Events
|
|
86
|
+
```typescript
|
|
87
|
+
// Subscribe only once
|
|
88
|
+
eventBusService.once('app:initialized', () => {
|
|
89
|
+
console.log('App initialized!');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// This will trigger the callback and auto-unsubscribe
|
|
93
|
+
eventBusService.emit('app:initialized');
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Debug Mode
|
|
97
|
+
```typescript
|
|
98
|
+
// Enable debug logging
|
|
99
|
+
eventBusService.setDebugMode(true);
|
|
100
|
+
|
|
101
|
+
// All subscriptions, emissions, and unsubscriptions will be logged
|
|
102
|
+
eventBusService.on('test:event', () => {});
|
|
103
|
+
// Console: [EventBus] Subscribed to: test:event
|
|
104
|
+
|
|
105
|
+
eventBusService.emit('test:event', 'data');
|
|
106
|
+
// Console: [EventBus] Emitting: test:event ['data']
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Event Introspection
|
|
110
|
+
```typescript
|
|
111
|
+
// Get list of all registered events
|
|
112
|
+
const events = eventBusService.getEventList();
|
|
113
|
+
console.log('Active events:', events);
|
|
114
|
+
|
|
115
|
+
// Check subscriber count
|
|
116
|
+
const count = eventBusService.getSubscriberCount('user:login');
|
|
117
|
+
console.log(`Subscribers for user:login: ${count}`);
|
|
118
|
+
|
|
119
|
+
// Check if event has subscribers
|
|
120
|
+
if (eventBusService.hasSubscribers('data:updated')) {
|
|
121
|
+
console.log('Someone is listening to data:updated');
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## API Reference
|
|
126
|
+
|
|
127
|
+
### `eventBusService.on(event, callback)`
|
|
128
|
+
Subscribe to an event.
|
|
129
|
+
- **event**: `string` - Event name
|
|
130
|
+
- **callback**: `Function` - Handler function
|
|
131
|
+
- **Returns**: `EventSubscription` - Subscription object with `unsubscribe()` method
|
|
132
|
+
|
|
133
|
+
### `eventBusService.once(event, callback)`
|
|
134
|
+
Subscribe to an event once (auto-unsubscribe after first emission).
|
|
135
|
+
- **event**: `string` - Event name
|
|
136
|
+
- **callback**: `Function` - Handler function
|
|
137
|
+
- **Returns**: `EventSubscription` - Subscription object
|
|
138
|
+
|
|
139
|
+
### `eventBusService.emit(event, ...args)`
|
|
140
|
+
Emit an event to all subscribers.
|
|
141
|
+
- **event**: `string` - Event name
|
|
142
|
+
- **args**: `any[]` - Arguments to pass to callbacks
|
|
143
|
+
|
|
144
|
+
### `eventBusService.off(event, callback)`
|
|
145
|
+
Unsubscribe from an event.
|
|
146
|
+
- **event**: `string` - Event name
|
|
147
|
+
- **callback**: `Function` - Callback to remove
|
|
148
|
+
|
|
149
|
+
### `eventBusService.clear(event?)`
|
|
150
|
+
Remove all subscribers for a specific event or all events.
|
|
151
|
+
- **event**: `string` (optional) - Event name
|
|
152
|
+
|
|
153
|
+
### `eventBusService.setDebugMode(enabled)`
|
|
154
|
+
Enable or disable debug logging.
|
|
155
|
+
- **enabled**: `boolean` - Debug mode state
|
|
156
|
+
|
|
157
|
+
### `eventBusService.getEventList()`
|
|
158
|
+
Get list of all registered events.
|
|
159
|
+
- **Returns**: `string[]` - Array of event names
|
|
160
|
+
|
|
161
|
+
### `eventBusService.getSubscriberCount(event)`
|
|
162
|
+
Get number of subscribers for a specific event.
|
|
163
|
+
- **event**: `string` - Event name
|
|
164
|
+
- **Returns**: `number` - Subscriber count
|
|
165
|
+
|
|
166
|
+
### `eventBusService.hasSubscribers(event)`
|
|
167
|
+
Check if an event has any subscribers.
|
|
168
|
+
- **event**: `string` - Event name
|
|
169
|
+
- **Returns**: `boolean` - True if event has subscribers
|
|
170
|
+
|
|
171
|
+
## Event Naming Conventions
|
|
172
|
+
Use namespaced event names for better organization:
|
|
173
|
+
- `user:login`, `user:logout`, `user:updated`
|
|
174
|
+
- `data:loaded`, `data:saved`, `data:error`
|
|
175
|
+
- `ui:theme-changed`, `ui:modal-opened`, `ui:notification`
|
|
176
|
+
- `app:initialized`, `app:ready`, `app:shutdown`
|
|
177
|
+
|
|
178
|
+
## Best Practices
|
|
179
|
+
|
|
180
|
+
### 1. Always Unsubscribe
|
|
181
|
+
```typescript
|
|
182
|
+
class MyComponent extends KimuComponentElement {
|
|
183
|
+
private subscriptions: any[] = [];
|
|
184
|
+
|
|
185
|
+
onInit() {
|
|
186
|
+
this.subscriptions.push(
|
|
187
|
+
eventBusService.on('event', this.handler.bind(this))
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
disconnectedCallback() {
|
|
192
|
+
this.subscriptions.forEach(sub => sub.unsubscribe());
|
|
193
|
+
super.disconnectedCallback();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### 2. Use Namespaced Events
|
|
199
|
+
```typescript
|
|
200
|
+
// Good
|
|
201
|
+
eventBusService.emit('user:profile:updated', userData);
|
|
202
|
+
eventBusService.emit('notification:success', message);
|
|
203
|
+
|
|
204
|
+
// Avoid generic names
|
|
205
|
+
eventBusService.emit('update', data); // Too generic!
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### 3. Document Event Payloads
|
|
209
|
+
```typescript
|
|
210
|
+
/**
|
|
211
|
+
* Event: user:login
|
|
212
|
+
* Payload: { id: number, name: string, email: string }
|
|
213
|
+
*/
|
|
214
|
+
eventBusService.emit('user:login', { id: 1, name: 'John', email: 'john@example.com' });
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### 4. Handle Errors
|
|
218
|
+
```typescript
|
|
219
|
+
eventBusService.on('data:process', (data) => {
|
|
220
|
+
try {
|
|
221
|
+
processData(data);
|
|
222
|
+
} catch (error) {
|
|
223
|
+
eventBusService.emit('data:error', error);
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## Common Use Cases
|
|
229
|
+
|
|
230
|
+
### 1. Component Communication
|
|
231
|
+
```typescript
|
|
232
|
+
// Component A emits
|
|
233
|
+
eventBusService.emit('cart:item-added', { id: 123, name: 'Product' });
|
|
234
|
+
|
|
235
|
+
// Component B listens
|
|
236
|
+
eventBusService.on('cart:item-added', (item) => {
|
|
237
|
+
updateCartCount();
|
|
238
|
+
});
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### 2. State Synchronization
|
|
242
|
+
```typescript
|
|
243
|
+
// When state changes
|
|
244
|
+
eventBusService.emit('state:updated', newState);
|
|
245
|
+
|
|
246
|
+
// Components react to state changes
|
|
247
|
+
eventBusService.on('state:updated', (state) => {
|
|
248
|
+
this.updateUI(state);
|
|
249
|
+
});
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### 3. User Actions
|
|
253
|
+
```typescript
|
|
254
|
+
// Button click
|
|
255
|
+
eventBusService.emit('button:save-clicked', { form: 'userProfile' });
|
|
256
|
+
|
|
257
|
+
// Multiple handlers can react
|
|
258
|
+
eventBusService.on('button:save-clicked', validateForm);
|
|
259
|
+
eventBusService.on('button:save-clicked', showLoadingIndicator);
|
|
260
|
+
eventBusService.on('button:save-clicked', trackAnalytics);
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
## Performance Considerations
|
|
264
|
+
- Event bus is synchronous - handlers execute immediately
|
|
265
|
+
- Use wisely for frequently emitted events (e.g., scroll, mousemove)
|
|
266
|
+
- Consider debouncing or throttling for high-frequency events
|
|
267
|
+
- Always clean up subscriptions to prevent memory leaks
|
|
268
|
+
|
|
269
|
+
## License
|
|
270
|
+
This module is part of KIMU-Core and follows the same license (MPL-2.0).
|
|
271
|
+
|
|
272
|
+
## Version History
|
|
273
|
+
- **1.0.0** - Initial release with core event bus functionality
|