From 0bb7b4cab19677d0d03ff9a202651d71dc0ba93d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Thu, 25 Jun 2020 19:19:23 +0200 Subject: [PATCH] only start the simulation when someone is listening fixes #45 --- src/simulation.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/simulation.js b/src/simulation.js index f1e751d..2350e68 100644 --- a/src/simulation.js +++ b/src/simulation.js @@ -21,6 +21,7 @@ export default function(nodes) { velocityDecay = 0.6, forces = new Map(), stepper = timer(step), + started = stepper.stop() || 0, event = dispatch("tick", "end"); if (nodes == null) nodes = []; @@ -144,7 +145,9 @@ export default function(nodes) { }, on: function(name, _) { - return arguments.length > 1 ? (event.on(name, _), simulation) : event.on(name); + return arguments.length > 1 + ? (event.on(name, _), started++ || stepper.restart(step), simulation) + : event.on(name); } }; }