Skip to content

Commit

Permalink
Merge pull request #474 from biati-digital/potential-memory-leak-fix
Browse files Browse the repository at this point in the history
perf(glightbox): Fixes potential memory leak with IntersectionObserver
  • Loading branch information
biati-digital authored Mar 29, 2024
2 parents 3f9a456 + c35831e commit 2748089
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-melons-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"glightbox": patch
---

Adds cleanup for IntersectionObserver
6 changes: 4 additions & 2 deletions packages/glightbox/src/glightbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default class GLightbox {
nextButton: HTMLButtonElement | null = null;
overlay: HTMLButtonElement | null = null;
slidesContainer: HTMLElement | null = null;
private observer: IntersectionObserver;

constructor(options: Partial<GLightboxOptions> = {}) {
this.options = mergeObjects(GLightboxDefaults, options);
Expand Down Expand Up @@ -288,7 +289,7 @@ export default class GLightbox {

this.processVariables(this.modal);

const observer = new IntersectionObserver((entries) => {
this.observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
removeClass(entry.target, 'visible');
if (entry.isIntersecting && this.state.get('open')) {
Expand Down Expand Up @@ -333,7 +334,7 @@ export default class GLightbox {
this.slidesContainer?.insertAdjacentHTML('beforeend', slideHTML);
const created = this.slidesContainer?.querySelectorAll('.gl-slide')[index];
if (created) {
observer.observe(created);
this.observer.observe(created);
}
index++;
}
Expand Down Expand Up @@ -399,6 +400,7 @@ export default class GLightbox {
public destroy(): void {
this.close();
this.clearAllEvents(true);
this.observer.disconnect();
}

public reload(): void {
Expand Down

0 comments on commit 2748089

Please sign in to comment.