slicejs-web-framework 2.3.1 → 2.3.2

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.
@@ -1,329 +1,338 @@
1
- /**
2
- * EventManager - Sistema de eventos pub/sub para Slice.js
3
- * Ubicación: /Slice/Components/Structural/EventManager/EventManager.js
4
- *
5
- * Características:
6
- * - Suscripciones globales y vinculadas a componentes
7
- * - Auto-limpieza cuando componentes se destruyen
8
- * - API simple: subscribe, subscribeOnce, unsubscribe, emit
9
- */
10
- export default class EventManager {
11
- constructor() {
12
- // Map<eventName, Map<subscriptionId, { callback, componentSliceId, once }>>
13
- this.subscriptions = new Map();
14
-
15
- // Map<sliceId, Set<{ eventName, subscriptionId }>> - Para auto-cleanup
16
- this.componentSubscriptions = new Map();
17
-
18
- // Contador para IDs únicos
19
- this.idCounter = 0;
20
- }
21
-
22
- init() {
23
- return true;
24
- }
25
-
26
- // ============================================
27
- // API PRINCIPAL
28
- // ============================================
29
-
30
- /**
31
- * Suscribirse a un evento
32
- * @param {string} eventName - Nombre del evento
33
- * @param {Function} callback - Función a ejecutar cuando se emita el evento
34
- * @param {Object} options - Opciones: { component: SliceComponent }
35
- * @returns {string} subscriptionId - ID para desuscribirse
36
- *
37
- * @example
38
- * // Suscripción global
39
- * const id = slice.events.subscribe('user:login', (user) => {
40
- * console.log('Usuario:', user);
41
- * });
42
- *
43
- * // Suscripción con auto-cleanup
44
- * slice.events.subscribe('user:login', (user) => {
45
- * this.actualizar(user);
46
- * }, { component: this });
47
- */
48
- subscribe(eventName, callback, options = {}) {
49
- if (typeof callback !== 'function') {
50
- slice.logger.logError('EventManager', 'El callback debe ser una función');
51
- return null;
52
- }
53
-
54
- const subscriptionId = `evt_${++this.idCounter}`;
55
-
56
- // Crear Map para este evento si no existe
57
- if (!this.subscriptions.has(eventName)) {
58
- this.subscriptions.set(eventName, new Map());
59
- }
60
-
61
- // Guardar la suscripción
62
- this.subscriptions.get(eventName).set(subscriptionId, {
63
- callback,
64
- componentSliceId: options.component?.sliceId || null,
65
- once: false,
66
- });
67
-
68
- // Si hay componente, registrar para auto-cleanup
69
- if (options.component?.sliceId) {
70
- this._registerComponentSubscription(options.component.sliceId, eventName, subscriptionId);
71
- }
72
-
73
- slice.logger.logInfo('EventManager', `Suscrito a "${eventName}" [${subscriptionId}]`);
74
-
75
- return subscriptionId;
76
- }
77
-
78
- /**
79
- * Suscribirse a un evento una sola vez
80
- * @param {string} eventName - Nombre del evento
81
- * @param {Function} callback - Función a ejecutar
82
- * @param {Object} options - Opciones: { component: SliceComponent }
83
- * @returns {string} subscriptionId
84
- *
85
- * @example
86
- * slice.events.subscribeOnce('app:ready', () => {
87
- * console.log('App lista!');
88
- * });
89
- */
90
- subscribeOnce(eventName, callback, options = {}) {
91
- if (typeof callback !== 'function') {
92
- slice.logger.logError('EventManager', 'El callback debe ser una función');
93
- return null;
94
- }
95
-
96
- const subscriptionId = `evt_${++this.idCounter}`;
97
-
98
- if (!this.subscriptions.has(eventName)) {
99
- this.subscriptions.set(eventName, new Map());
100
- }
101
-
102
- this.subscriptions.get(eventName).set(subscriptionId, {
103
- callback,
104
- componentSliceId: options.component?.sliceId || null,
105
- once: true,
106
- });
107
-
108
- if (options.component?.sliceId) {
109
- this._registerComponentSubscription(options.component.sliceId, eventName, subscriptionId);
110
- }
111
-
112
- slice.logger.logInfo('EventManager', `Suscrito (once) a "${eventName}" [${subscriptionId}]`);
113
-
114
- return subscriptionId;
115
- }
116
-
117
- /**
118
- * Desuscribirse de un evento
119
- * @param {string} eventName - Nombre del evento
120
- * @param {string} subscriptionId - ID de la suscripción
121
- * @returns {boolean} true si se eliminó correctamente
122
- *
123
- * @example
124
- * const id = slice.events.subscribe('evento', callback);
125
- * // Después...
126
- * slice.events.unsubscribe('evento', id);
127
- */
128
- unsubscribe(eventName, subscriptionId) {
129
- if (!this.subscriptions.has(eventName)) {
130
- return false;
131
- }
132
-
133
- const removed = this.subscriptions.get(eventName).delete(subscriptionId);
134
-
135
- // Limpiar Map vacío
136
- if (this.subscriptions.get(eventName).size === 0) {
137
- this.subscriptions.delete(eventName);
138
- }
139
-
140
- if (removed) {
141
- slice.logger.logInfo('EventManager', `Desuscrito de "${eventName}" [${subscriptionId}]`);
142
- }
143
-
144
- return removed;
145
- }
146
-
147
- /**
148
- * Emitir un evento
149
- * @param {string} eventName - Nombre del evento
150
- * @param {*} data - Datos a pasar a los callbacks
151
- *
152
- * @example
153
- * slice.events.emit('user:login', { id: 123, name: 'Juan' });
154
- * slice.events.emit('cart:cleared'); // Sin datos
155
- */
156
- emit(eventName, data = null) {
157
- slice.logger.logInfo('EventManager', `Emitiendo "${eventName}"`, data);
158
-
159
- if (!this.subscriptions.has(eventName)) {
160
- return;
161
- }
162
-
163
- const toRemove = [];
164
-
165
- // Notificar a todos los suscriptores
166
- for (const [subscriptionId, subscription] of this.subscriptions.get(eventName)) {
167
- // Verificar que el componente aún existe (si aplica)
168
- if (subscription.componentSliceId) {
169
- if (!slice.controller.activeComponents.has(subscription.componentSliceId)) {
170
- // Componente ya no existe, marcar para eliminar
171
- toRemove.push(subscriptionId);
172
- continue;
173
- }
174
- }
175
-
176
- // Ejecutar callback
177
- try {
178
- subscription.callback(data);
179
- } catch (error) {
180
- slice.logger.logError('EventManager', `Error en callback de "${eventName}" [${subscriptionId}]`, error);
181
- }
182
-
183
- // Si es subscribeOnce, marcar para eliminar
184
- if (subscription.once) {
185
- toRemove.push(subscriptionId);
186
- }
187
- }
188
-
189
- // Limpiar suscripciones marcadas
190
- toRemove.forEach((id) => {
191
- this.subscriptions.get(eventName).delete(id);
192
- });
193
-
194
- // Limpiar Map vacío
195
- if (this.subscriptions.get(eventName).size === 0) {
196
- this.subscriptions.delete(eventName);
197
- }
198
- }
199
-
200
- // ============================================
201
- // BIND - Vinculación a Componente
202
- // ============================================
203
-
204
- /**
205
- * Vincular el EventManager a un componente para auto-cleanup
206
- * @param {HTMLElement} component - Componente Slice con sliceId
207
- * @returns {Object} API vinculada al componente
208
- *
209
- * @example
210
- * class MiComponente extends HTMLElement {
211
- * async init() {
212
- * this.events = slice.events.bind(this);
213
- *
214
- * this.events.subscribe('user:login', (user) => {
215
- * this.actualizar(user);
216
- * });
217
- * }
218
- * }
219
- */
220
- bind(component) {
221
- if (!component?.sliceId) {
222
- slice.logger.logError('EventManager', 'bind() requiere un componente Slice válido con sliceId');
223
- return null;
224
- }
225
-
226
- const self = this;
227
-
228
- return {
229
- /**
230
- * Suscribirse a un evento (auto-cleanup)
231
- */
232
- subscribe: (eventName, callback) => {
233
- return self.subscribe(eventName, callback, { component });
234
- },
235
-
236
- /**
237
- * Suscribirse una sola vez (auto-cleanup)
238
- */
239
- subscribeOnce: (eventName, callback) => {
240
- return self.subscribeOnce(eventName, callback, { component });
241
- },
242
-
243
- /**
244
- * Emitir un evento
245
- */
246
- emit: (eventName, data) => {
247
- self.emit(eventName, data);
248
- },
249
- };
250
- }
251
-
252
- // ============================================
253
- // AUTO-CLEANUP
254
- // ============================================
255
-
256
- /**
257
- * Registrar suscripción para un componente (interno)
258
- */
259
- _registerComponentSubscription(sliceId, eventName, subscriptionId) {
260
- if (!this.componentSubscriptions.has(sliceId)) {
261
- this.componentSubscriptions.set(sliceId, new Set());
262
- }
263
- this.componentSubscriptions.get(sliceId).add({ eventName, subscriptionId });
264
- }
265
-
266
- /**
267
- * Limpiar todas las suscripciones de un componente
268
- * Se llama automáticamente cuando el componente se destruye
269
- * @param {string} sliceId - ID del componente
270
- * @returns {number} Cantidad de suscripciones eliminadas
271
- */
272
- cleanupComponent(sliceId) {
273
- if (!this.componentSubscriptions.has(sliceId)) {
274
- return 0;
275
- }
276
-
277
- const subscriptions = this.componentSubscriptions.get(sliceId);
278
- let count = 0;
279
-
280
- for (const { eventName, subscriptionId } of subscriptions) {
281
- if (this.unsubscribe(eventName, subscriptionId)) {
282
- count++;
283
- }
284
- }
285
-
286
- this.componentSubscriptions.delete(sliceId);
287
-
288
- if (count > 0) {
289
- slice.logger.logInfo('EventManager', `Limpiadas ${count} suscripción(es) de ${sliceId}`);
290
- }
291
-
292
- return count;
293
- }
294
-
295
- // ============================================
296
- // UTILIDADES
297
- // ============================================
298
-
299
- /**
300
- * Verificar si hay suscriptores para un evento
301
- * @param {string} eventName - Nombre del evento
302
- * @returns {boolean}
303
- */
304
- hasSubscribers(eventName) {
305
- return this.subscriptions.has(eventName) && this.subscriptions.get(eventName).size > 0;
306
- }
307
-
308
- /**
309
- * Obtener cantidad de suscriptores para un evento
310
- * @param {string} eventName - Nombre del evento
311
- * @returns {number}
312
- */
313
- subscriberCount(eventName) {
314
- if (!this.subscriptions.has(eventName)) {
315
- return 0;
316
- }
317
- return this.subscriptions.get(eventName).size;
318
- }
319
-
320
- /**
321
- * Limpiar TODAS las suscripciones (usar con cuidado)
322
- */
323
- clear() {
324
- this.subscriptions.clear();
325
- this.componentSubscriptions.clear();
326
- this.idCounter = 0;
327
- slice.logger.logInfo('EventManager', 'Todas las suscripciones eliminadas');
328
- }
329
- }
1
+ /**
2
+ * EventManager - Sistema de eventos pub/sub para Slice.js
3
+ * Ubicación: /Slice/Components/Structural/EventManager/EventManager.js
4
+ *
5
+ * Características:
6
+ * - Suscripciones globales y vinculadas a componentes
7
+ * - Auto-limpieza cuando componentes se destruyen
8
+ * - API simple: subscribe, subscribeOnce, unsubscribe, emit
9
+ */
10
+ /**
11
+ * @typedef {Object} EventManagerBind
12
+ * @property {(eventName: string, callback: Function) => string|null} subscribe
13
+ * @property {(eventName: string, callback: Function) => string|null} subscribeOnce
14
+ * @property {(eventName: string, data?: any) => void} emit
15
+ */
16
+
17
+
18
+ export default class EventManager {
19
+ constructor() {
20
+ // Map<eventName, Map<subscriptionId, { callback, componentSliceId, once }>>
21
+ this.subscriptions = new Map();
22
+
23
+ // Map<sliceId, Set<{ eventName, subscriptionId }>> - Para auto-cleanup
24
+ this.componentSubscriptions = new Map();
25
+
26
+ // Contador para IDs únicos
27
+ this.idCounter = 0;
28
+ }
29
+
30
+ init() {
31
+ return true;
32
+ }
33
+
34
+ // ============================================
35
+ // API PRINCIPAL
36
+ // ============================================
37
+
38
+ /**
39
+ * Suscribirse a un evento
40
+ * @param {string} eventName - Nombre del evento
41
+ * @param {(data?: any) => void} callback - Funcion a ejecutar cuando se emita el evento
42
+ * @param {{ component?: HTMLElement }} [options]
43
+ * @returns {string|null} subscriptionId - ID para desuscribirse
44
+ *
45
+ * @example
46
+ * // Suscripcion global
47
+ * const id = slice.events.subscribe('user:login', (user) => {
48
+ * console.log('Usuario:', user);
49
+ * });
50
+ *
51
+ * // Suscripcion con auto-cleanup
52
+ * slice.events.subscribe('user:login', (user) => {
53
+ * this.actualizar(user);
54
+ * }, { component: this });
55
+ */
56
+ subscribe(eventName, callback, options = {}) {
57
+ if (typeof callback !== 'function') {
58
+ slice.logger.logError('EventManager', 'El callback debe ser una función');
59
+ return null;
60
+ }
61
+
62
+ const subscriptionId = `evt_${++this.idCounter}`;
63
+
64
+ // Crear Map para este evento si no existe
65
+ if (!this.subscriptions.has(eventName)) {
66
+ this.subscriptions.set(eventName, new Map());
67
+ }
68
+
69
+ // Guardar la suscripción
70
+ this.subscriptions.get(eventName).set(subscriptionId, {
71
+ callback,
72
+ componentSliceId: options.component?.sliceId || null,
73
+ once: false,
74
+ });
75
+
76
+ // Si hay componente, registrar para auto-cleanup
77
+ if (options.component?.sliceId) {
78
+ this._registerComponentSubscription(options.component.sliceId, eventName, subscriptionId);
79
+ }
80
+
81
+ slice.logger.logInfo('EventManager', `Suscrito a "${eventName}" [${subscriptionId}]`);
82
+
83
+ return subscriptionId;
84
+ }
85
+
86
+ /**
87
+ * Suscribirse a un evento una sola vez
88
+ * @param {string} eventName - Nombre del evento
89
+ * @param {(data?: any) => void} callback - Funcion a ejecutar
90
+ * @param {{ component?: HTMLElement }} [options]
91
+ * @returns {string|null} subscriptionId
92
+ *
93
+ * @example
94
+ * slice.events.subscribeOnce('app:ready', () => {
95
+ * console.log('App lista!');
96
+ * });
97
+ */
98
+ subscribeOnce(eventName, callback, options = {}) {
99
+ if (typeof callback !== 'function') {
100
+ slice.logger.logError('EventManager', 'El callback debe ser una función');
101
+ return null;
102
+ }
103
+
104
+ const subscriptionId = `evt_${++this.idCounter}`;
105
+
106
+ if (!this.subscriptions.has(eventName)) {
107
+ this.subscriptions.set(eventName, new Map());
108
+ }
109
+
110
+ this.subscriptions.get(eventName).set(subscriptionId, {
111
+ callback,
112
+ componentSliceId: options.component?.sliceId || null,
113
+ once: true,
114
+ });
115
+
116
+ if (options.component?.sliceId) {
117
+ this._registerComponentSubscription(options.component.sliceId, eventName, subscriptionId);
118
+ }
119
+
120
+ slice.logger.logInfo('EventManager', `Suscrito (once) a "${eventName}" [${subscriptionId}]`);
121
+
122
+ return subscriptionId;
123
+ }
124
+
125
+ /**
126
+ * Desuscribirse de un evento
127
+ * @param {string} eventName - Nombre del evento
128
+ * @param {string} subscriptionId - ID de la suscripcion
129
+ * @returns {boolean} true si se elimino correctamente
130
+ *
131
+ * @example
132
+ * const id = slice.events.subscribe('evento', callback);
133
+ * // Despues...
134
+ * slice.events.unsubscribe('evento', id);
135
+ */
136
+ unsubscribe(eventName, subscriptionId) {
137
+ if (!this.subscriptions.has(eventName)) {
138
+ return false;
139
+ }
140
+
141
+ const removed = this.subscriptions.get(eventName).delete(subscriptionId);
142
+
143
+ // Limpiar Map vacío
144
+ if (this.subscriptions.get(eventName).size === 0) {
145
+ this.subscriptions.delete(eventName);
146
+ }
147
+
148
+ if (removed) {
149
+ slice.logger.logInfo('EventManager', `Desuscrito de "${eventName}" [${subscriptionId}]`);
150
+ }
151
+
152
+ return removed;
153
+ }
154
+
155
+ /**
156
+ * Emitir un evento
157
+ * @param {string} eventName - Nombre del evento
158
+ * @param {any} [data] - Datos a pasar a los callbacks
159
+ * @returns {void}
160
+ *
161
+ * @example
162
+ * slice.events.emit('user:login', { id: 123, name: 'Juan' });
163
+ * slice.events.emit('cart:cleared'); // Sin datos
164
+ */
165
+ emit(eventName, data = null) {
166
+ slice.logger.logInfo('EventManager', `Emitiendo "${eventName}"`, data);
167
+
168
+ if (!this.subscriptions.has(eventName)) {
169
+ return;
170
+ }
171
+
172
+ const toRemove = [];
173
+
174
+ // Notificar a todos los suscriptores
175
+ for (const [subscriptionId, subscription] of this.subscriptions.get(eventName)) {
176
+ // Verificar que el componente aún existe (si aplica)
177
+ if (subscription.componentSliceId) {
178
+ if (!slice.controller.activeComponents.has(subscription.componentSliceId)) {
179
+ // Componente ya no existe, marcar para eliminar
180
+ toRemove.push(subscriptionId);
181
+ continue;
182
+ }
183
+ }
184
+
185
+ // Ejecutar callback
186
+ try {
187
+ subscription.callback(data);
188
+ } catch (error) {
189
+ slice.logger.logError('EventManager', `Error en callback de "${eventName}" [${subscriptionId}]`, error);
190
+ }
191
+
192
+ // Si es subscribeOnce, marcar para eliminar
193
+ if (subscription.once) {
194
+ toRemove.push(subscriptionId);
195
+ }
196
+ }
197
+
198
+ // Limpiar suscripciones marcadas
199
+ toRemove.forEach((id) => {
200
+ this.subscriptions.get(eventName).delete(id);
201
+ });
202
+
203
+ // Limpiar Map vacío
204
+ if (this.subscriptions.get(eventName).size === 0) {
205
+ this.subscriptions.delete(eventName);
206
+ }
207
+ }
208
+
209
+ // ============================================
210
+ // BIND - Vinculación a Componente
211
+ // ============================================
212
+
213
+ /**
214
+ * Vincular el EventManager a un componente para auto-cleanup
215
+ * @param {HTMLElement} component - Componente Slice con sliceId
216
+ * @returns {EventManagerBind|null} API vinculada al componente
217
+ *
218
+ * @example
219
+ * class MiComponente extends HTMLElement {
220
+ * async init() {
221
+ * this.events = slice.events.bind(this);
222
+ *
223
+ * this.events.subscribe('user:login', (user) => {
224
+ * this.actualizar(user);
225
+ * });
226
+ * }
227
+ * }
228
+ */
229
+ bind(component) {
230
+ if (!component?.sliceId) {
231
+ slice.logger.logError('EventManager', 'bind() requiere un componente Slice válido con sliceId');
232
+ return null;
233
+ }
234
+
235
+ const self = this;
236
+
237
+ return {
238
+ /**
239
+ * Suscribirse a un evento (auto-cleanup)
240
+ */
241
+ subscribe: (eventName, callback) => {
242
+ return self.subscribe(eventName, callback, { component });
243
+ },
244
+
245
+ /**
246
+ * Suscribirse una sola vez (auto-cleanup)
247
+ */
248
+ subscribeOnce: (eventName, callback) => {
249
+ return self.subscribeOnce(eventName, callback, { component });
250
+ },
251
+
252
+ /**
253
+ * Emitir un evento
254
+ */
255
+ emit: (eventName, data) => {
256
+ self.emit(eventName, data);
257
+ },
258
+ };
259
+ }
260
+
261
+ // ============================================
262
+ // AUTO-CLEANUP
263
+ // ============================================
264
+
265
+ /**
266
+ * Registrar suscripción para un componente (interno)
267
+ */
268
+ _registerComponentSubscription(sliceId, eventName, subscriptionId) {
269
+ if (!this.componentSubscriptions.has(sliceId)) {
270
+ this.componentSubscriptions.set(sliceId, new Set());
271
+ }
272
+ this.componentSubscriptions.get(sliceId).add({ eventName, subscriptionId });
273
+ }
274
+
275
+ /**
276
+ * Limpiar todas las suscripciones de un componente
277
+ * Se llama automáticamente cuando el componente se destruye
278
+ * @param {string} sliceId - ID del componente
279
+ * @returns {number} Cantidad de suscripciones eliminadas
280
+ */
281
+ cleanupComponent(sliceId) {
282
+ if (!this.componentSubscriptions.has(sliceId)) {
283
+ return 0;
284
+ }
285
+
286
+ const subscriptions = this.componentSubscriptions.get(sliceId);
287
+ let count = 0;
288
+
289
+ for (const { eventName, subscriptionId } of subscriptions) {
290
+ if (this.unsubscribe(eventName, subscriptionId)) {
291
+ count++;
292
+ }
293
+ }
294
+
295
+ this.componentSubscriptions.delete(sliceId);
296
+
297
+ if (count > 0) {
298
+ slice.logger.logInfo('EventManager', `Limpiadas ${count} suscripción(es) de ${sliceId}`);
299
+ }
300
+
301
+ return count;
302
+ }
303
+
304
+ // ============================================
305
+ // UTILIDADES
306
+ // ============================================
307
+
308
+ /**
309
+ * Verificar si hay suscriptores para un evento
310
+ * @param {string} eventName - Nombre del evento
311
+ * @returns {boolean}
312
+ */
313
+ hasSubscribers(eventName) {
314
+ return this.subscriptions.has(eventName) && this.subscriptions.get(eventName).size > 0;
315
+ }
316
+
317
+ /**
318
+ * Obtener cantidad de suscriptores para un evento
319
+ * @param {string} eventName - Nombre del evento
320
+ * @returns {number}
321
+ */
322
+ subscriberCount(eventName) {
323
+ if (!this.subscriptions.has(eventName)) {
324
+ return 0;
325
+ }
326
+ return this.subscriptions.get(eventName).size;
327
+ }
328
+
329
+ /**
330
+ * Limpiar TODAS las suscripciones (usar con cuidado)
331
+ */
332
+ clear() {
333
+ this.subscriptions.clear();
334
+ this.componentSubscriptions.clear();
335
+ this.idCounter = 0;
336
+ slice.logger.logInfo('EventManager', 'Todas las suscripciones eliminadas');
337
+ }
338
+ }