Skip to content

Commit

Permalink
fix(entry): update visiblity check
Browse files Browse the repository at this point in the history
  • Loading branch information
ThornWalli committed Dec 16, 2024
1 parent 49e7395 commit 962bf07
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
12 changes: 12 additions & 0 deletions src/runtime/utils/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ export const hasBatteryPerformanceIssue = async videoBlob => {
}
};

export const waitForVisibilty = () => {
const { promise, resolve } = Promise.withResolvers();
if (document.visibilityState === 'hidden') {
document.addEventListener('visibilitychange', resolve, {
once: true
});
} else {
resolve();
}
return promise;
};

/**
* Checks if battery still has enough energy.
* This check is for Chrome and all other browsers that support this setting.
Expand Down
14 changes: 0 additions & 14 deletions src/runtime/utils/performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,8 @@ export const getDefaultRunOptions = () => {
return { maxTime: 1000, threshold: 0.65 };
};

export const waitForVisibleDocument = () => {
return new Promise(resolve => {
if (document.visibilityState === 'hidden') {
document.addEventListener('visibilitychange', () => resolve(), {
once: true
});
} else {
resolve();
}
});
};

export const run = async (options = {}) => {
if (window.requestIdleCallback) {
await waitForVisibleDocument();

const { maxTime, threshold } = { ...getDefaultRunOptions(), ...options };
const fpsObserver = new CallbackObserver(rafTime);
const idleObserver = new CallbackObserver(idleTime);
Expand Down
4 changes: 3 additions & 1 deletion src/tmpl/entry.tmpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default options => {
let code = `import { ${
options.performanceCheck ? `run, ` : ``
}hasSufficientPerformance, setup } from '#booster/utils/performance';
import { triggerRunCallback, observeBoosterButton, setupBoosterLayer, updateBoosterLayerMessage, initReducedView, hasBatteryPerformanceIssue, deprecationWarningButtonSelector } from '#booster/utils/entry';
import { waitForVisibilty, triggerRunCallback, observeBoosterButton, setupBoosterLayer, updateBoosterLayerMessage, initReducedView, hasBatteryPerformanceIssue, deprecationWarningButtonSelector } from '#booster/utils/entry';
import Deferred from '#booster/classes/Deferred';
import { isSupportedBrowser } from '#booster/utils/browser';
import {video as videoBlob} from './blobs.mjs';
Expand Down Expand Up @@ -52,6 +52,8 @@ function client () {
deferred.resolve();
}
await waitForVisibilty();
document.documentElement.classList.remove('nuxt-booster-reduced-view');
`;
Expand Down

0 comments on commit 962bf07

Please sign in to comment.