Skip to content

Commit

Permalink
Fix: expander-sub-card loading times
Browse files Browse the repository at this point in the history
  • Loading branch information
Alia5 committed Sep 28, 2024
1 parent 10756ae commit dc04a44
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
52 changes: 23 additions & 29 deletions src/Card.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,38 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
-->
<svelte:options customElement="expander-sub-card" />
<!-- eslint-disable-next-line svelte/valid-compile -->
<svelte:options customElement='expander-sub-card' />

<script lang="ts">
import type { HomeAssistant, LovelaceCardConfig } from 'custom-card-helpers';
import { cardUtil } from './cardUtil';
import { getCardUtil } from './cardUtil.svelte';
import { onMount } from 'svelte';
export let type = 'div';
export let config: LovelaceCardConfig;
export let hass: HomeAssistant;
const {
type = 'div',
config,
hass
}: { type: string; config: LovelaceCardConfig; hass: HomeAssistant; self: HTMLElement } = $props();
let loading = true;
const uplift = (
node: HTMLElement,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
p: { type: string; hass: HomeAssistant }
) => ({
// eslint-disable-next-line no-shadow
update: (p: { type: string; hass: HomeAssistant }) => {
if (node) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((node.firstChild as any)?.tagName) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(node.firstChild as any).hass = p.hass;
return;
}
void (async () => {
const el = (await cardUtil).createCardElement(config);
el.hass = p.hass;
node.innerHTML = '';
node.appendChild(el);
loading = false;
})();
}
let container = $state<HTMLElement>();
let loading = $state(false);
onMount(async () => {
const util = await getCardUtil();
const el = util.createCardElement(config);
el.hass = hass;
if (!container) {
console.error('container doesn\'t exist');
return;
}
container.innerHTML = '';
container.appendChild(el);
loading = false;
});
</script>

<div use:uplift={{ type, hass }} ></div>
<svelte:element this={type} bind:this={container}/>
{#if loading}
<span style={'padding: 1em; display: block; '}> Loading... </span>
{/if}
Expand Down
17 changes: 15 additions & 2 deletions src/cardUtil.ts → src/cardUtil.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,18 @@ export interface CardUtil {
createCardElement: (config: LovelaceCardConfig) => LovelaceCard;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const cardUtil: Promise<CardUtil> = (() => ((window as any).loadCardHelpers() as Promise<any>).then((v) => v))();
let cardUtil: CardUtil|undefined = $state(undefined);
export const loadCardUtil = async (): Promise<CardUtil> => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
cardUtil = await (window as any).loadCardHelpers().then((v: any) => v);
return cardUtil as CardUtil;
};

export const getCardUtil = () => {
if (!cardUtil) {
return loadCardUtil();
}
return cardUtil;
};

// export const cardUtil: Promise<CardUtil> = (() => ((window as any).loadCardHelpers() as Promise<any>).then((v) => v))();
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ limitations under the License.
*/
export { default as default } from './ExpanderCard.svelte';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { cardUtil } from './cardUtil';
import { cardUtil } from './cardUtil.svelte';
// Make Typescript Happy!
declare global {
interface Window {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"sourceMap": true,
"strict": true,
"module": "esnext",
"lib": ["es2019", "dom"],
"moduleResolution": "bundler"
},
"include": ["src/**/*"],
Expand Down

0 comments on commit dc04a44

Please sign in to comment.