fix(lifecycle-component): support 'once' option for event listeners

This commit is contained in:
2025-05-31 22:18:34 +00:00
parent 96c4de0f8a
commit e0f3e8a0ec
3 changed files with 26 additions and 8 deletions

View File

@ -91,19 +91,37 @@ export abstract class LifecycleComponent {
return;
}
// For 'once' listeners, we need to wrap the handler to remove it from our tracking
let actualHandler = handler;
if (options?.once) {
actualHandler = (...args: any[]) => {
// Call the original handler
handler(...args);
// Remove from our internal tracking
const index = this.listeners.findIndex(
l => l.target === target && l.event === event && l.handler === handler
);
if (index !== -1) {
this.listeners.splice(index, 1);
}
};
}
// Support both EventEmitter and DOM-style event targets
if (typeof target.on === 'function') {
if (options?.once) {
target.once(event, handler);
target.once(event, actualHandler);
} else {
target.on(event, handler);
target.on(event, actualHandler);
}
} else if (typeof target.addEventListener === 'function') {
target.addEventListener(event, handler, options);
target.addEventListener(event, actualHandler, options);
} else {
throw new Error('Target must support on() or addEventListener()');
}
// Store the original handler in our tracking (not the wrapped one)
this.listeners.push({
target,
event,