Skip to content

Commit

Permalink
Fix(Svelte): Refresh triggered on initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
BenSeage committed Mar 4, 2024
1 parent b9e73a4 commit 28a1435
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions packages/svelte/src/lib/CSSRuntimeProvider.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
initializing = false;
};
const waitInitialized = async () => {
const currentIdentifier = ++identifier;
if (initializing) {
await new Promise<void>((resolve) => {
const interval = setInterval(() => {
Expand All @@ -55,26 +56,41 @@
}, 10);
});
}
return currentIdentifier === identifier
};
onMount(async () => await init());
afterUpdate(async () => {
const currentIdentifier = ++identifier;
await waitInitialized();
if (currentIdentifier !== identifier) return;
const getRuntimeCSS = () => $runtimeCSS
$: {
const currentRuntimeCSS = getRuntimeCSS()
if (currentRuntimeCSS) {
(async () => {
if (!await waitInitialized())
return
const resolvedConfig = await getResolvedConfig();
if (
$runtimeCSS.root !== root &&
(root || $runtimeCSS.root !== document)
) {
$runtimeCSS.destroy();
await init(resolvedConfig);
} else {
$runtimeCSS.refresh(resolvedConfig);
if (
currentRuntimeCSS.root !== root &&
(root || currentRuntimeCSS.root !== document)
) {
currentRuntimeCSS.destroy();
await init(await getResolvedConfig());
}
})()
}
});
}
$: {
const currentRuntimeCSS = getRuntimeCSS()
if (currentRuntimeCSS) {
(async () => {
if (!await waitInitialized())
return
currentRuntimeCSS.refresh(config && await getResolvedConfig());
})()
}
}
onDestroy(() => {
if (!isExternalRuntimeCSS) {
Expand Down

0 comments on commit 28a1435

Please sign in to comment.