Skip to content

Commit

Permalink
Merge pull request #47 from cloudflare/issue-44-check-if-document-obj…
Browse files Browse the repository at this point in the history
…ect-before-using

Issue 44 - Check if document is defined before using
  • Loading branch information
yomnaebrahim authored Jan 7, 2025
2 parents 987d903 + 4007768 commit 30c7454
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/engines/BandwidthEngine/BandwidthEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ class BandwidthMeasurementEngine {
this.#throttleMs = throttleMs;
this.#estimatedServerTime = Math.max(0, estimatedServerTime);

document.addEventListener('visibilitychange', this.#handleVisibilityChange);
if (typeof document !== 'undefined') {
document.addEventListener(
'visibilitychange',
this.#handleVisibilityChange
);
}
}

// Public attributes
Expand All @@ -90,9 +95,15 @@ class BandwidthMeasurementEngine {
}

#handleVisibilityChange = () => {
if (document.visibilityState === 'hidden') {
if (
typeof document !== 'undefined' &&
document.visibilityState === 'hidden'
) {
this.pause();
} else if (document.visibilityState === 'visible') {
} else if (
typeof document !== 'undefined' &&
document.visibilityState === 'visible'
) {
this.play();
}
};
Expand Down Expand Up @@ -383,10 +394,12 @@ class BandwidthMeasurementEngine {
}

deleteEventListener() {
document.removeEventListener(
'visibilitychange',
this.#handleVisibilityChange
);
if (typeof document !== 'undefined') {
document.removeEventListener(
'visibilitychange',
this.#handleVisibilityChange
);
}
}
}

Expand Down

0 comments on commit 30c7454

Please sign in to comment.