Skip to content

Commit

Permalink
improve load speed
Browse files Browse the repository at this point in the history
  • Loading branch information
xZetsubou authored Aug 19, 2023
1 parent e0061b0 commit e4d0c05
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions dist/ha-custom-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//------------------

!window.$customSidebarV2 &&
(window.$customSidebarV2 = { tryCounter: 0, Loaded: false });
(window.$customSidebarV2 = { tryCounter: 0, try_limit: 20, Loaded: false });

const ver = '301217_2359';

Expand Down Expand Up @@ -106,7 +106,8 @@
root = root && root.querySelector('home-assistant-main');
root = root && root.shadowRoot;
const drawerLayout = root && root.querySelector('ha-drawer');
!drawerLayout &&

if (!drawerLayout && window.$customSidebarV2.tryCounter > window.$customSidebarV2.tryCounter)
log(
'warn',
'Cannot find "home-assistant home-assistant-main ha-drawer" element'
Expand All @@ -120,14 +121,15 @@
return window.$customSidebarV2.SideBarElement;
}
const drawerLayout = getDrawerLayout();

let sidebar =
drawerLayout && drawerLayout.querySelector('ha-drawer ha-sidebar');
sidebar = sidebar && sidebar.shadowRoot;
window.$customSidebarV2.TitleElement =
sidebar && sidebar.querySelector('.title');
sidebar = sidebar && sidebar.querySelector('paper-listbox');

!sidebar &&
if (!sidebar && window.$customSidebarV2.tryCounter > window.$customSidebarV2.try_limit)
log('warn', 'Cannot find "ha-drawer ha-sidebar paper-listbox" element');

return (window.$customSidebarV2.SideBarElement = sidebar);
Expand Down Expand Up @@ -437,7 +439,7 @@

function finish(success, error) {
clearInterval(runInterval);
if (!success || error || window.$customSidebarV2.tryCounter > 10) {
if (!success || error || window.$customSidebarV2.tryCounter > window.$customSidebarV2.try_limit) {
window.$customSidebarV2.Loaded = 'error';
log('warn', 'Failed', error || '');
} else if (success) {
Expand Down Expand Up @@ -502,20 +504,24 @@
finish('Custom Sidebar already loaded');
}
if (
++window.$customSidebarV2.tryCounter > 10 &&
++window.$customSidebarV2.tryCounter > window.$customSidebarV2.try_limit &&
!window.$customSidebarV2.Loaded
) {
finish(false, 'Tried 10 times and gave up');
finish(false, 'Tried ' + window.$customSidebarV2.try_limit + 'times and gave up');
}
}
} catch (e) {
finish(false, e);
}
}

if (!window.$customSidebarV2.Loaded) {
runInterval = setInterval(run, 1000);
} else {
finish('Already loaded');
}
(async () => {
while (customElements.get("home-assistant") === undefined)
await new Promise(resolve => window.setTimeout(resolve, 100));

if (!window.$customSidebarV2.Loaded) {
runInterval = setInterval(run, 50);

}
})();
})();

0 comments on commit e4d0c05

Please sign in to comment.