fix(lifecycle-component): support 'once' option for event listeners
This commit is contained in:
parent
96c4de0f8a
commit
e0f3e8a0ec
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"expiryDate": "2025-08-27T14:28:53.471Z",
|
"expiryDate": "2025-08-29T18:29:48.329Z",
|
||||||
"issueDate": "2025-05-29T14:28:53.471Z",
|
"issueDate": "2025-05-31T18:29:48.329Z",
|
||||||
"savedAt": "2025-05-29T14:28:53.473Z"
|
"savedAt": "2025-05-31T18:29:48.330Z"
|
||||||
}
|
}
|
@ -55,8 +55,8 @@ class TestComponent extends LifecycleComponent {
|
|||||||
return this.clearInterval(timer);
|
return this.clearInterval(timer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public testAddEventListener(target: any, event: string, handler: Function): void {
|
public testAddEventListener(target: any, event: string, handler: Function, options?: { once?: boolean }): void {
|
||||||
return this.addEventListener(target, event, handler);
|
return this.addEventListener(target, event, handler, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
public testIsShuttingDown(): boolean {
|
public testIsShuttingDown(): boolean {
|
||||||
|
@ -91,19 +91,37 @@ export abstract class LifecycleComponent {
|
|||||||
return;
|
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
|
// Support both EventEmitter and DOM-style event targets
|
||||||
if (typeof target.on === 'function') {
|
if (typeof target.on === 'function') {
|
||||||
if (options?.once) {
|
if (options?.once) {
|
||||||
target.once(event, handler);
|
target.once(event, actualHandler);
|
||||||
} else {
|
} else {
|
||||||
target.on(event, handler);
|
target.on(event, actualHandler);
|
||||||
}
|
}
|
||||||
} else if (typeof target.addEventListener === 'function') {
|
} else if (typeof target.addEventListener === 'function') {
|
||||||
target.addEventListener(event, handler, options);
|
target.addEventListener(event, actualHandler, options);
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Target must support on() or addEventListener()');
|
throw new Error('Target must support on() or addEventListener()');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Store the original handler in our tracking (not the wrapped one)
|
||||||
this.listeners.push({
|
this.listeners.push({
|
||||||
target,
|
target,
|
||||||
event,
|
event,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user