From 950214d61864d6b2300c833b634f071f3a63104f Mon Sep 17 00:00:00 2001 From: Vincent Esche Date: Sat, 7 Jun 2025 16:51:08 +0200 Subject: [PATCH] fix(force-simulation): Fix a bug that would sometimes keep a simulation running, when its inputs change, even if `alpha < alphaMin` --- .changeset/khaki-pugs-hammer.md | 5 +++++ .../src/lib/components/ForceSimulation.svelte | 15 +++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 .changeset/khaki-pugs-hammer.md diff --git a/.changeset/khaki-pugs-hammer.md b/.changeset/khaki-pugs-hammer.md new file mode 100644 index 000000000..948dc5c34 --- /dev/null +++ b/.changeset/khaki-pugs-hammer.md @@ -0,0 +1,5 @@ +--- +'layerchart': patch +--- + +fix(force-simulation): Fixed a bug that would sometimes keep a simulation running, when its inputs change, even if `alpha < alphaMin` diff --git a/packages/layerchart/src/lib/components/ForceSimulation.svelte b/packages/layerchart/src/lib/components/ForceSimulation.svelte index 1787ef4d0..8aabbd574 100644 --- a/packages/layerchart/src/lib/components/ForceSimulation.svelte +++ b/packages/layerchart/src/lib/components/ForceSimulation.svelte @@ -258,12 +258,8 @@ // pass it to the internal d3 simulation object: pushAlphaToSimulation(alpha); - // Only resume the simulation as long as `alpha` - // is above the cut-off threshold of `alphaMin`, - // otherwise our simulation will never terminate: - if (simulation.alpha() >= simulation.alphaMin()) { - runOrResumeSimulation(); - } + // Then we attempt to resume the simulation: + runOrResumeSimulation(); } ); @@ -404,6 +400,13 @@ return; } + if (simulation.alpha() < simulation.alphaMin()) { + // Only resume the simulation as long as `alpha` + // is above the cut-off threshold of `alphaMin`, + // otherwise our simulation will never terminate: + return; + } + onStart(); simulation.restart();