Skip to content

Commit

Permalink
Cancel events.
Browse files Browse the repository at this point in the history
Like interrupt, except when a transition is cancelled before it starts. This is
necessary for transition promises (#77) since we will need to reject promises on
cancelled transitions.
  • Loading branch information
mbostock committed Jan 25, 2019
1 parent 5a939e1 commit d6024da
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -357,6 +357,7 @@ Adds or removes a *listener* to each selected element for the specified event *t
* `start` - when the transition starts.
* `end` - when the transition ends.
* `interrupt` - when the transition is interrupted.
* `cancel` - when the transition is cancelled.

See [The Life of a Transition](#the-life-of-a-transition) for more. Note that these are *not* native DOM events as implemented by [*selection*.on](https://github.com/d3/d3-selection#selection_on) and [*selection*.dispatch](https://github.com/d3/d3-selection#selection_dispatch), but transition events!

Expand Down
2 changes: 1 addition & 1 deletion src/interrupt.js
Expand Up @@ -16,7 +16,7 @@ export default function(node, name) {
active = schedule.state > STARTING && schedule.state < ENDING;
schedule.state = ENDED;
schedule.timer.stop();
if (active) schedule.on.call("interrupt", node, node.__data__, schedule.index, schedule.group);
schedule.on.call(active ? "interrupt" : "cancel", node, node.__data__, schedule.index, schedule.group);
delete schedules[i];
}

Expand Down
8 changes: 3 additions & 5 deletions src/transition/schedule.js
@@ -1,7 +1,7 @@
import {dispatch} from "d3-dispatch";
import {timer, timeout} from "d3-timer";

var emptyOn = dispatch("start", "end", "interrupt");
var emptyOn = dispatch("start", "end", "cancel", "interrupt");
var emptyTween = [];

export var CREATED = 0;
Expand Down Expand Up @@ -82,20 +82,18 @@ function create(node, id, self) {
if (o.state === STARTED) return timeout(start);

// Interrupt the active transition, if any.
// Dispatch the interrupt event.
if (o.state === RUNNING) {
o.state = ENDED;
o.timer.stop();
o.on.call("interrupt", node, node.__data__, o.index, o.group);
delete schedules[i];
}

// Cancel any pre-empted transitions. No interrupt event is dispatched
// because the cancelled transitions never started. Note that this also
// removes this transition from the pending list!
// Cancel any pre-empted transitions.
else if (+i < id) {
o.state = ENDED;
o.timer.stop();
o.on.call("cancel", node, node.__data__, o.index, o.group);
delete schedules[i];
}
}
Expand Down

0 comments on commit d6024da

Please sign in to comment.