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

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

View File

@ -1,5 +1,5 @@
{
"expiryDate": "2025-08-27T14:28:53.471Z",
"issueDate": "2025-05-29T14:28:53.471Z",
"savedAt": "2025-05-29T14:28:53.473Z"
"expiryDate": "2025-08-29T18:29:48.329Z",
"issueDate": "2025-05-31T18:29:48.329Z",
"savedAt": "2025-05-31T18:29:48.330Z"
}

View File

@ -55,8 +55,8 @@ class TestComponent extends LifecycleComponent {
return this.clearInterval(timer);
}
public testAddEventListener(target: any, event: string, handler: Function): void {
return this.addEventListener(target, event, handler);
public testAddEventListener(target: any, event: string, handler: Function, options?: { once?: boolean }): void {
return this.addEventListener(target, event, handler, options);
}
public testIsShuttingDown(): boolean {

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,