mirror of
https://github.com/immich-app/immich
synced 2025-11-07 17:27:20 +00:00
refactor: hot module reload component (#22135)
This commit is contained in:
parent
0b20d1df9f
commit
53a6724039
2 changed files with 63 additions and 40 deletions
|
|
@ -12,6 +12,7 @@
|
||||||
setFocusTo as setFocusToInit,
|
setFocusTo as setFocusToInit,
|
||||||
} from '$lib/components/timeline/actions/focus-actions';
|
} from '$lib/components/timeline/actions/focus-actions';
|
||||||
import { AppRoute, AssetAction } from '$lib/constants';
|
import { AppRoute, AssetAction } from '$lib/constants';
|
||||||
|
import HotModuleReload from '$lib/elements/HotModuleReload.svelte';
|
||||||
import Portal from '$lib/elements/Portal.svelte';
|
import Portal from '$lib/elements/Portal.svelte';
|
||||||
import Skeleton from '$lib/elements/Skeleton.svelte';
|
import Skeleton from '$lib/elements/Skeleton.svelte';
|
||||||
import { authManager } from '$lib/managers/auth-manager.svelte';
|
import { authManager } from '$lib/managers/auth-manager.svelte';
|
||||||
|
|
@ -223,19 +224,12 @@
|
||||||
complete.then(completeNav, completeNav);
|
complete.then(completeNav, completeNav);
|
||||||
});
|
});
|
||||||
|
|
||||||
const hmrSupport = () => {
|
const handleAfterUpdate = (payload: UpdatePayload) => {
|
||||||
// when hmr happens, skeleton is initialized to true by default
|
const timelineUpdate = payload.updates.some(
|
||||||
// normally, loading timeline is part of a navigation event, and the completion of
|
|
||||||
// that event triggers a scroll-to-asset, if necessary, when then clears the skeleton.
|
|
||||||
// this handler will run the navigation/scroll-to-asset handler when hmr is performed,
|
|
||||||
// preventing skeleton from showing after hmr
|
|
||||||
if (import.meta && import.meta.hot) {
|
|
||||||
const afterApdate = (payload: UpdatePayload) => {
|
|
||||||
const assetGridUpdate = payload.updates.some(
|
|
||||||
(update) => update.path.endsWith('Timeline.svelte') || update.path.endsWith('assets-store.ts'),
|
(update) => update.path.endsWith('Timeline.svelte') || update.path.endsWith('assets-store.ts'),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (assetGridUpdate) {
|
if (timelineUpdate) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
const asset = $page.url.searchParams.get('at');
|
const asset = $page.url.searchParams.get('at');
|
||||||
if (asset) {
|
if (asset) {
|
||||||
|
|
@ -251,17 +245,12 @@
|
||||||
}, 500);
|
}, 500);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
import.meta.hot?.on('vite:afterUpdate', afterApdate);
|
|
||||||
import.meta.hot?.on('vite:beforeUpdate', (payload) => {
|
const handleBeforeUpdate = (payload: UpdatePayload) => {
|
||||||
const assetGridUpdate = payload.updates.some((update) => update.path.endsWith('Timeline.svelte'));
|
const timelineUpdate = payload.updates.some((update) => update.path.endsWith('Timeline.svelte'));
|
||||||
if (assetGridUpdate) {
|
if (timelineUpdate) {
|
||||||
timelineManager.destroy();
|
timelineManager.destroy();
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
return () => import.meta.hot?.off('vite:afterUpdate', afterApdate);
|
|
||||||
}
|
|
||||||
return () => void 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateIsScrolling = () => (timelineManager.scrolling = true);
|
const updateIsScrolling = () => (timelineManager.scrolling = true);
|
||||||
|
|
@ -287,10 +276,6 @@
|
||||||
if (!enableRouting) {
|
if (!enableRouting) {
|
||||||
showSkeleton = false;
|
showSkeleton = false;
|
||||||
}
|
}
|
||||||
const disposeHmr = hmrSupport();
|
|
||||||
return () => {
|
|
||||||
disposeHmr();
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const getMaxScrollPercent = () => {
|
const getMaxScrollPercent = () => {
|
||||||
|
|
@ -833,6 +818,8 @@
|
||||||
|
|
||||||
<svelte:document onkeydown={onKeyDown} onkeyup={onKeyUp} onselectstart={onSelectStart} use:shortcuts={shortcutList} />
|
<svelte:document onkeydown={onKeyDown} onkeyup={onKeyUp} onselectstart={onSelectStart} use:shortcuts={shortcutList} />
|
||||||
|
|
||||||
|
<HotModuleReload onAfterUpdate={handleAfterUpdate} onBeforeUpdate={handleBeforeUpdate} />
|
||||||
|
|
||||||
{#if isShowDeleteConfirmation}
|
{#if isShowDeleteConfirmation}
|
||||||
<DeleteAssetDialog
|
<DeleteAssetDialog
|
||||||
size={idsSelectedAssets.length}
|
size={idsSelectedAssets.length}
|
||||||
|
|
|
||||||
36
web/src/lib/elements/HotModuleReload.svelte
Normal file
36
web/src/lib/elements/HotModuleReload.svelte
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
<script lang="ts">
|
||||||
|
import { onDestroy, onMount } from 'svelte';
|
||||||
|
import type { UpdatePayload } from 'vite';
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
onBeforeUpdate?: (payload: UpdatePayload) => void;
|
||||||
|
onAfterUpdate?: (payload: UpdatePayload) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
let { onBeforeUpdate, onAfterUpdate }: Props = $props();
|
||||||
|
|
||||||
|
const unsubscribes: (() => void)[] = [];
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
const hot = import.meta.hot;
|
||||||
|
if (!hot) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (onBeforeUpdate) {
|
||||||
|
hot.on('vite:beforeUpdate', onBeforeUpdate);
|
||||||
|
unsubscribes.push(() => hot.off('vite:beforeUpdate', onBeforeUpdate));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (onAfterUpdate) {
|
||||||
|
hot.on('vite:afterUpdate', onAfterUpdate);
|
||||||
|
unsubscribes.push(() => hot.off('vite:afterUpdate', onAfterUpdate));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
onDestroy(() => {
|
||||||
|
for (const unsubscribe of unsubscribes) {
|
||||||
|
unsubscribe();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue