Skip to content

Commit 1ced3e2

Browse files
authored
Merge pull request #23 from longnguyen2004/main
Add `onDestroy` for proper cleanup + Update types
2 parents c635d0c + ef725f8 commit 1ced3e2

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-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} />

components/svelte/src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ declare module "svelte-particles" {
1010
url?: string;
1111
id?: string;
1212
class?: string;
13+
style?: string;
1314
particlesInit: (engine: Engine) => Promise<void>;
1415
};
1516
type ParticlesEvents = CustomEventWrapper<{

0 commit comments

Comments
 (0)