Skip to content

Commit c79ede1

Browse files
Add onDestroy for proper cleanup
1 parent c635d0c commit c79ede1

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

components/svelte/src/Particles.svelte

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<svelte:options accessors={true} />
22

33
<script lang="ts">
4-
import { afterUpdate, createEventDispatcher } from "svelte";
4+
import { afterUpdate, createEventDispatcher, onDestroy } from "svelte";
55
import type { ISourceOptions } from "tsparticles-engine";
66
import { tsParticles } from "tsparticles-engine";
77
@@ -21,20 +21,25 @@
2121
2222
let oldId = id;
2323
24-
afterUpdate(async () => {
25-
tsParticles.init();
26-
27-
if (particlesInit) {
28-
await particlesInit(tsParticles);
29-
}
30-
24+
function destroyOldContainer()
25+
{
3126
if (oldId) {
3227
const oldContainer = tsParticles.dom().find(c => c.id === oldId);
3328
3429
if (oldContainer) {
3530
oldContainer.destroy();
3631
}
3732
}
33+
}
34+
35+
afterUpdate(async () => {
36+
tsParticles.init();
37+
38+
if (particlesInit) {
39+
await particlesInit(tsParticles);
40+
}
41+
42+
destroyOldContainer();
3843
3944
if (id) {
4045
const cb = container => {
@@ -64,6 +69,8 @@
6469
});
6570
}
6671
});
72+
73+
onDestroy(destroyOldContainer);
6774
</script>
6875

6976
<div {id} class={cssClass} {style} />

0 commit comments

Comments
 (0)