Skip to content

Commit 13838eb

Browse files
authored
Merge pull request #763 from codeMaestro78/fix/memory-leak-event-listeners
2 parents 2795d0c + 183294c commit 13838eb

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

packages/phoenix-ng/projects/phoenix-ui-components/lib/components/ui-menu/overlay/overlay.component.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Input,
55
ViewChild,
66
type AfterViewInit,
7+
type OnDestroy,
78
ViewEncapsulation,
89
} from '@angular/core';
910
import type { ElementRef } from '@angular/core';
@@ -21,7 +22,7 @@ import { EventDisplayService } from '../../../services/event-display.service';
2122
styleUrls: ['./overlay.component.scss'],
2223
encapsulation: ViewEncapsulation.None,
2324
})
24-
export class OverlayComponent implements AfterViewInit {
25+
export class OverlayComponent implements AfterViewInit, OnDestroy {
2526
/** Title of the overlay. */
2627
@Input() overlayTitle: string;
2728
/** If the overlay is open or not. */
@@ -39,6 +40,9 @@ export class OverlayComponent implements AfterViewInit {
3940
/** Aspect ratio of the overlay view. */
4041
aspectRatio: number = window.innerWidth / window.innerHeight;
4142

43+
/** Bound resize handler for cleanup. */
44+
private resizeHandler = () => this.resetHandlePosition();
45+
4246
// ********************************************************************************
4347
// * Below code is specific to the overlay resize feature. (LOOK INTO CSS RESIZE) *
4448
// ********************************************************************************
@@ -70,12 +74,17 @@ export class OverlayComponent implements AfterViewInit {
7074
this.resetHandlePosition();
7175
});
7276

73-
window.addEventListener('resize', () => {
74-
this.resetHandlePosition();
75-
});
77+
window.addEventListener('resize', this.resizeHandler);
7678
}
7779
}
7880

81+
/**
82+
* Clean up event listeners when the component is destroyed.
83+
*/
84+
ngOnDestroy() {
85+
window.removeEventListener('resize', this.resizeHandler);
86+
}
87+
7988
/**
8089
* Resize the overlay card when the resize handle is dragged.
8190
*/

packages/phoenix-ng/projects/phoenix-ui-components/lib/components/ui-menu/ss-mode/ss-mode.component.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import { type OnInit } from '@angular/core';
2-
import { Component, ViewEncapsulation } from '@angular/core';
1+
import {
2+
Component,
3+
ViewEncapsulation,
4+
type OnDestroy,
5+
type OnInit,
6+
} from '@angular/core';
37

48
@Component({
59
standalone: false,
@@ -8,7 +12,7 @@ import { Component, ViewEncapsulation } from '@angular/core';
812
styleUrls: ['./ss-mode.component.scss'],
913
encapsulation: ViewEncapsulation.None,
1014
})
11-
export class SSModeComponent implements OnInit {
15+
export class SSModeComponent implements OnInit, OnDestroy {
1216
ssMode: boolean = false;
1317

1418
ngOnInit() {
@@ -38,4 +42,22 @@ export class SSModeComponent implements OnInit {
3842
private onDocumentClick = () => {
3943
document.exitFullscreen?.();
4044
};
45+
46+
/**
47+
* Clean up event listeners and fullscreen state when the component is destroyed.
48+
*/
49+
ngOnDestroy() {
50+
// Prevent any further toggleSSMode calls from fullscreenchange during teardown.
51+
document.onfullscreenchange = null;
52+
// Remove input event listeners.
53+
document.removeEventListener('click', this.onDocumentClick);
54+
document.removeEventListener('touchstart', this.onDocumentClick);
55+
// If still in fullscreen or ssMode is active, exit fullscreen and reset UI state.
56+
if (document.fullscreenElement || this.ssMode) {
57+
document.exitFullscreen?.();
58+
}
59+
// Ensure the ss-mode class and internal state are cleared.
60+
document.body.classList.remove('ss-mode');
61+
this.ssMode = false;
62+
}
4163
}

0 commit comments

Comments
 (0)