Пример #1
0
Queue.prototype.add = function(tasks) {
  var op, item, task, ref;

  this.cumulative = this.cumulative || 0;

  while (tasks.length) {
    item = tasks.shift();
    op = Object.keys(item).reduce(Task.deriveOp, "");

    this.cumulative += item[op];

    // For the last task, ensure that an "end" event is
    // emitted after the final callback is called.
    if (tasks.length === 0) {
      task = item.task;
      item.task = temporald => {
        task.call(this, temporald);

        // Emit the end event _from_ within the _last_ task
        // defined in the Queue tasks. Use the |tasks| array
        // object as the access key.
        this.emit("end", temporald);

        // Reset on last one in the queue
        this.cumulative = 0;
      };
    }

    if (op === "loop" && tasks.length === 0) {
      // When transitioning from a "delay" to a "loop", allow
      // the loop to iterate the amount of time given,
      // but still start at the correct offset.
      ref = exportable.delay(this.cumulative - item[op], () => {
        ref = exportable.loop(item[op], item.task);

        this.refs.push(ref);
      });
    } else {
      ref = exportable[op](this.cumulative, item.task);
    }

    this.refs.push(ref);
  }
};