From d6024da2bcf32c81b52be31ee4bbde274929a4c7 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Wed, 28 Feb 2018 10:21:51 -0800 Subject: [PATCH] Cancel events. 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. --- README.md | 1 + src/interrupt.js | 2 +- src/transition/schedule.js | 8 +++----- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 50e5166..7fb2d15 100644 --- a/README.md +++ b/README.md @@ -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! diff --git a/src/interrupt.js b/src/interrupt.js index 748d4e7..5583534 100644 --- a/src/interrupt.js +++ b/src/interrupt.js @@ -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]; } diff --git a/src/transition/schedule.js b/src/transition/schedule.js index d86d554..f4e88d7 100644 --- a/src/transition/schedule.js +++ b/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; @@ -82,7 +82,6 @@ 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(); @@ -90,12 +89,11 @@ function create(node, id, self) { 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]; } }