Skip to content

Timer, Stop! - #8

Merged
mbostock merged 21 commits into
masterfrom
cancel
Nov 5, 2015
Merged

mbostock merged 21 commits into
masterfrom
cancel

Conversation

@mbostock

@mbostock mbostock commented Nov 1, 2015

Copy link
Copy Markdown
Member

Rather than stopping timers by having the timer callback return true, timer(…) now returns a timer object with a stop method. Thus, you can now stop a timer synchronously outside of its callback, rather than having to wait until the timer is next called back. This will be useful for synchronously canceling transitions (d3/d3-transition#9 d3/d3#2189 d3/d3#2211). You can also reset a timer (changing its start time or callback) externally, too. Fixes #5.

For the sake of parsimony, timer.stop is now the only way to stop a timer: returning true from the callback has no effect.

This commit also wraps timer execution with a try-finally block. Fixes #4.

This commit also changes the sleep timing logic slightly. Rather than trying to compute the exact minimum wake-up time whenever a timer is scheduled, we simply make a pessimistic assumption and schedule the shortest nap (using requestAnimationFrame); on the next tick, we’ll compute the exact wake-up time. This has the advantage of avoiding timeout thrashing (setTimeout followed by clearTimeout, repeatedly) if a series of timers are scheduled with descending delays. Similarly, when a timer is stopped, we don’t bother to recalculate the minimum wake-up time, since it can only increase.

TODO

  • implement timer.stop
  • implement timer.restart
  • hide internal fields (next, etc.)
  • allow timer.callback to be set directly (no)?
  • do we need queueHead and queueTail to be objects (no)? or is null better (yes)?
  • rename timer.reset to timer.restart and allow stopped timer to be restarted
  • fix timer.stop during re-entrant timerFlush (defer deletion until after flush?)
  • fix tests that assume implementation details that have changed
  • test timer.stop
  • test timer.restart
  • test instanceof timer
  • update documentation

Rather than stopping timers by having the callback return true, timer(…) now
returns a timer object with a stop method. Thus, you can now stop a timer
synchronously outside of its callback, rather than having to wait until the
timer is next called back. You can also reset a timer (changing its start time
or callback) externally, too. Fixes #5.

This commit also wraps timer execution with a try-finally block, fixing #4.
Rather than remove the timer from the queue synchronously, we clear its callback
and remove it on the next tick. This means we can use a singly-linked list.
Rather than storing the timer linked list on the timer objects, we now store a
map of task-by-id and reference the task by timer.id. This is similar to how
setTimeout and setInterval work (although we still provide a convenient timer
object with methods rather than just a long identifier).

The reset method has been renamed to restart, and you can now restart a timer
that has been stopped. If the timer was stopped, this is equivalent to creating
a new timer in that the task is put at the end of the queue.

Also, fix instanceof timer.
@mbostock mbostock self-assigned this Nov 5, 2015
I think it’s better to encourage callers to retain a reference to the timer.
This makes the mechanism for canceling a timer from inside its callback the same
as from outside, rather than the former using `this.stop()`. (This also
simplifies our implementation slightly.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Cancel a scheduled timer? Handle exceptions inside of timer callbacks?

1 participant