Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace for timers #30

Closed
1wheel opened this issue Oct 12, 2017 · 3 comments
Closed

Namespace for timers #30

1wheel opened this issue Oct 12, 2017 · 3 comments

Comments

@1wheel
Copy link

1wheel commented Oct 12, 2017

I've started using hot reloading to see how my code changes the page without triggering a full refresh.

This requires some bookkeeping if there is a timer on the page to remove old timers:

if (window.__animationTimer) window.__animationTimer.stop()
window.__animationTimer = d3.timer(function(t){
  // cool animation code
})

Event listeners don't have this problem:

d3.select(window).on('resize.redraw', drawChart)

Timers could do something similar if they took a fourth argument:

d3.timer(function(t){
  // cool animation code
}, 0, null, 'animationTimer')

Not sure if you've found a better solution while working on express; it looked like you were using while (true) in a generator instead of a timer.

@mbostock
Copy link
Member

I recommend implementing this on top of d3-timer, rather than making it a feature intrinsic to d3-timer. Something like a “timer manager” that functions similarly to the d3.timer constructor but, like you say, takes a timer name and implicitly stops any existing timer with the same name before returning the new timer. (Or perhaps it would use timer.restart to preserve the invocation order of the existing timer.)

The timer manager would presumably also want to wrap timer.stop so that stopping a timer would remove it from the manager’s registry.

And yep, for @observablehq, we’re using generators rather than timers, and since generators are pull-based they cleanup after themselves naturally. You can still create a timer (or add an event listener etc.) inside a cell; the only caveat is that you have to use a try-finally block to stop your timer (or remove your event listener) when the generator is terminated.

@1wheel
Copy link
Author

1wheel commented Oct 12, 2017

Cool - I'll throw something like that into jetpack. thanks!

@1wheel 1wheel closed this as completed Oct 12, 2017
@mbostock
Copy link
Member

Excellent. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants