Skip to content

Commit

Permalink
Merge pull request NemesisRE#299 from NemesisRE/remove_get_promisable…
Browse files Browse the repository at this point in the history
…_element

Remove local getPromosableElement utility
  • Loading branch information
NemesisRE authored Nov 11, 2024
2 parents 4a817d5 + ea32b45 commit 7a4fc54
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 61 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"postversion": "git push && git push --tags"
},
"dependencies": {
"get-promisable-result": "^1.0.1",
"home-assistant-query-selector": "4.3.0",
"home-assistant-styles-manager": "3.0.0"
},
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 16 additions & 6 deletions src/kiosk-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
OnHistoryAndLogBookDialogOpenDetail
} from 'home-assistant-query-selector';
import { HomeAssistantStylesManager } from 'home-assistant-styles-manager';
import { getPromisableResult } from 'get-promisable-result';
import {
KioskModeRunner,
HomeAssistant,
Expand All @@ -32,14 +33,15 @@ import {
WINDOW_RESIZE_DELAY,
STYLES_PREFIX,
NAMESPACE,
NON_CRITICAL_WARNING
NON_CRITICAL_WARNING,
MAX_ATTEMPTS,
RETRY_DELAY
} from '@constants';
import {
queryString,
setCache,
cached,
getMenuTranslations,
getPromisableElement,
addMenuItemsDataSelectors,
parseVersion,
resetCache
Expand Down Expand Up @@ -86,10 +88,14 @@ class KioskMode implements KioskModeRunner {
this.appToolbar = await HEADER.selector.query(ELEMENT.TOOLBAR).element;
this.sideBarRoot = await HA_SIDEBAR.selector.$.element;

this.user = await getPromisableElement(
this.user = await getPromisableResult(
(): User => this.ha?.hass?.user,
(user: User) => !!user,
`${ELEMENT.HOME_ASSISTANT} > hass > user`
{
retries: MAX_ATTEMPTS,
delay: RETRY_DELAY,
rejectMessage: `${NAMESPACE}: Cannot select ${ELEMENT.HOME_ASSISTANT} > hass > user after {{ retries }} attempts. Giving up!`
}
);

this.version = parseVersion(this.ha.hass?.config?.version);
Expand Down Expand Up @@ -148,10 +154,14 @@ class KioskMode implements KioskModeRunner {
}

// Get the configuration and process it
return getPromisableElement(
return getPromisableResult(
() => lovelace?.lovelace?.config,
(config: Lovelace['lovelace']['config']) => !!config,
'Lovelace config'
{
retries: MAX_ATTEMPTS,
delay: RETRY_DELAY,
rejectMessage: `${NAMESPACE}: Cannot select Lovelace config after {{ retries }} attempts. Giving up!`
}
)
.then((config: Lovelace['lovelace']['config']) => {
return this.processConfig(
Expand Down
72 changes: 17 additions & 55 deletions src/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getPromisableResult } from 'get-promisable-result';
import { HomeAssistant, Version } from '@types';
import {
TRUE,
MENU_REFERENCES,
MAX_ATTEMPTS,
RETRY_DELAY,
NAMESPACE,
ELEMENT,
OPTION
} from '@constants';
Expand Down Expand Up @@ -56,38 +56,24 @@ export const getDisplayNoneRules = (...rules: string[]): Record<string, false> =
};

const getHAResources = (ha: HomeAssistant): Promise<Record<string, Record<string, string>>> => {
let attempts = 0;
const referencePaths = Object.values(MENU_REFERENCES);
return new Promise((resolve, reject) => {
const getResources = () => {
const resources = ha?.hass?.resources;
let success = false;
if (resources) {
const language = ha.hass.language;
// check if all the resources are available
const anyEmptyResource = referencePaths.find((path: string) => {
if (resources[language][path]) {
return false;
}
return true;
});
if (!anyEmptyResource) {
success = true;
}
return getPromisableResult(
() => ha?.hass?.resources,
(resources: Record<string, Record<string, string>>): boolean => {
const language = ha.hass.language;
const anyEmptyResource = referencePaths.find((path: string) => {
return !resources?.[language][path];
});
if (!anyEmptyResource) {
return true;
}
if (success) {
resolve(resources);
} else {
attempts++;
if (attempts < MAX_ATTEMPTS) {
setTimeout(getResources, RETRY_DELAY);
} else {
reject();
}
}
};
getResources();
});
return false;
},
{
retries: MAX_ATTEMPTS,
delay: RETRY_DELAY
}
);
};

export const getMenuTranslations = async(
Expand All @@ -104,30 +90,6 @@ export const getMenuTranslations = async(
return Object.fromEntries(menuTranslationsEntries);
};

export const getPromisableElement = <T>(
getElement: () => T,
check: (element: T) => boolean,
elementName: string
): Promise<T> => {
return new Promise<T>((resolve, reject) => {
let attempts = 0;
const select = () => {
const element: T = getElement();
if (element && check(element)) {
resolve(element);
} else {
attempts++;
if (attempts < MAX_ATTEMPTS) {
setTimeout(select, RETRY_DELAY);
} else {
reject(new Error(`${NAMESPACE}: Cannot select ${elementName} after ${MAX_ATTEMPTS} attempts. Giving up!`));
}
}
};
select();
});
};

export const addMenuItemsDataSelectors = (
menuItems: NodeListOf<HTMLElement>,
translations: Record<string, string>
Expand Down

0 comments on commit 7a4fc54

Please sign in to comment.