Svelte: Fix FontInput component infinite loop

This commit is contained in:
Keavon Chambers
2023-02-12 01:01:40 -08:00
parent 717e9c861a
commit 33b5c0f5f4
3 changed files with 12 additions and 19 deletions
@@ -30,21 +30,16 @@
let open = false; let open = false;
let entries: MenuListEntry[] = []; let entries: MenuListEntry[] = [];
let activeEntry: MenuListEntry | undefined = undefined; let activeEntry: MenuListEntry | undefined = undefined;
let highlighted: MenuListEntry | undefined = undefined;
let minWidth = isStyle ? 0 : 300; let minWidth = isStyle ? 0 : 300;
$: fontFamily, $: fontFamily, fontStyle, watchFont();
(async () => {
entries = await getEntries(); async function watchFont(): Promise<void> {
activeEntry = getActiveEntry(entries); // We set this function's result to a local variable to avoid reading from `entries` which causes Svelte to trigger an update that results in an infinite loop
highlighted = activeEntry; const newEntries = await getEntries();
})(); entries = newEntries;
$: fontStyle, activeEntry = getActiveEntry(newEntries);
async () => { }
entries = await getEntries();
activeEntry = getActiveEntry(entries);
highlighted = activeEntry;
};
async function setOpen(): Promise<void> { async function setOpen(): Promise<void> {
open = true; open = true;
@@ -106,7 +101,6 @@
entries = await getEntries(); entries = await getEntries();
activeEntry = getActiveEntry(entries); activeEntry = getActiveEntry(entries);
highlighted = activeEntry;
}); });
</script> </script>
@@ -77,6 +77,7 @@
<div class="menu-bar-input" bind:this={self} data-menu-bar-input> <div class="menu-bar-input" bind:this={self} data-menu-bar-input>
{#each entries as entry, index (index)} {#each entries as entry, index (index)}
<div class="entry-container"> <div class="entry-container">
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
<div <div
on:click={(e) => clickEntry(entry, e)} on:click={(e) => clickEntry(entry, e)}
on:keydown={(e) => entry.ref?.keydown(e, false)} on:keydown={(e) => entry.ref?.keydown(e, false)}
@@ -94,7 +95,9 @@
</div> </div>
{#if entry.children && entry.children.length > 0} {#if entry.children && entry.children.length > 0}
<MenuList <MenuList
on:open={(e) => (entry.ref.isOpen = e.detail)} on:open={(e) => {
if (entry.ref) entry.ref.isOpen = e.detail;
}}
open={entry.ref?.isOpen || false} open={entry.ref?.isOpen || false}
entries={entry.children || []} entries={entry.children || []}
direction="Bottom" direction="Bottom"
@@ -105,7 +105,6 @@ export default defineComponent({
open: false, open: false,
entries: [] as MenuListEntry[], entries: [] as MenuListEntry[],
activeEntry: undefined as MenuListEntry | undefined, activeEntry: undefined as MenuListEntry | undefined,
highlighted: undefined as MenuListEntry | undefined,
entriesStart: 0, entriesStart: 0,
minWidth: this.isStyle ? 0 : 300, minWidth: this.isStyle ? 0 : 300,
}; };
@@ -113,7 +112,6 @@ export default defineComponent({
async mounted() { async mounted() {
this.entries = await this.getEntries(); this.entries = await this.getEntries();
this.activeEntry = this.getActiveEntry(this.entries); this.activeEntry = this.getActiveEntry(this.entries);
this.highlighted = this.activeEntry;
}, },
methods: { methods: {
async setOpen(): Promise<void> { async setOpen(): Promise<void> {
@@ -175,12 +173,10 @@ export default defineComponent({
async fontFamily() { async fontFamily() {
this.entries = await this.getEntries(); this.entries = await this.getEntries();
this.activeEntry = this.getActiveEntry(this.entries); this.activeEntry = this.getActiveEntry(this.entries);
this.highlighted = this.activeEntry;
}, },
async fontStyle() { async fontStyle() {
this.entries = await this.getEntries(); this.entries = await this.getEntries();
this.activeEntry = this.getActiveEntry(this.entries); this.activeEntry = this.getActiveEntry(this.entries);
this.highlighted = this.activeEntry;
}, },
}, },
components: { components: {