import {set} from "./schedule.js";

export default function() {
  var on0, on1, that = this, id = that._id, size = that.size();
  return new Promise(function(resolve, reject) {
    var cancel = {value: reject},
        end = {value: function() { if (--size === 0) resolve(); }};

    that.each(function() {
      var schedule = set(this, id),
          on = schedule.on;

      // If this node shared a dispatch with the previous node,
      // just assign the updated shared dispatch and we’re done!
      // Otherwise, copy-on-write.
      if (on !== on0) {
        on1 = (on0 = on).copy();
        on1._.cancel.push(cancel);
        on1._.interrupt.push(cancel);
        on1._.end.push(end);
      }

      schedule.on = on1;
    });
  });
}

@@ -729,16 +729,99 @@
 %0A    %7D);
+%0A%0A    // The selection was empty, resolve end immediately%0A    if (!size) resolve();
 %0A  %7D);%0A%7D

resolve transition.end() immediately when the selection is empty fixes #105