Repository navigation
Conversation
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 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.
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.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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