Skip to content

Commit

Permalink
only start the simulation when someone is listening
Browse files Browse the repository at this point in the history
fixes #45
  • Loading branch information
Fil committed Jun 25, 2020
1 parent 194dba9 commit 0bb7b4c
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/simulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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);
}
};
}

0 comments on commit 0bb7b4c

Please sign in to comment.