mirror of
https://github.com/immich-app/immich.git
synced 2025-12-22 09:15:34 +03:00
refactor: hot module reload component (#22135)
This commit is contained in:
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>
|
||||
Reference in New Issue
Block a user