schedulerjs
Version:
A promise execution flow control library
147 lines (117 loc) • 5.57 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var _events = require('events');
/**
* Sequential promises
*/
var Sequence = (function (_EventEmitter) {
_inherits(Sequence, _EventEmitter);
/**
* Initializes the object
*/
function Sequence() {
_classCallCheck(this, Sequence);
_get(Object.getPrototypeOf(Sequence.prototype), 'constructor', this).call(this);
this.started = false;
this.queue = [];
this.pos = 0;
}
/**
* Adds a function to the queue
* @param {Function|Sequence} callback A function returning a promise or another sequence
* @return {Function} This object (chaining)
*/
_createClass(Sequence, [{
key: 'next',
value: function next(callback) {
if (callback instanceof Sequence) {
var _queue;
var otherSequenceToPush = callback.queue.slice(callback.pos);
(_queue = this.queue).push.apply(_queue, _toConsumableArray(otherSequenceToPush));
} else {
this.queue.push(callback);
}
return this;
}
/**
* Adds a delay function to the queue
* @param {Number} number The timeout amount
* @return {Function} This object (chaining)
*/
}, {
key: 'delay',
value: function delay(number) {
var timeoutPromise = function timeoutPromise() {
return new Promise(function (resolve) {
setTimeout(function () {
resolve();
}, number);
});
};
return this.next(timeoutPromise);
}
/**
* Stops the sequence after the next test is done
*/
}, {
key: 'stop',
value: function stop() {
this.stopped = true;
this.emit('stopped');
}
/**
* Starts the queue
*/
}, {
key: 'start',
value: function start() {
var _this = this;
// use nextTick to allow events after start
process.nextTick(function () {
if (!_this.started) {
_this.started = true;
_this.emit('started');
} else {
_this.emit('next', _this.pos, _this.queue.length);
}
if (_this.queue.length - _this.pos === 0) {
_this.emit('finished');
return;
}
var returnValue = _this.queue[_this.pos]();
if (returnValue instanceof Promise) {
returnValue.then(function () {
_this._callNext();
})['catch'](function (reason) {
_this.emit('error', reason);
});
} else {
_this._callNext();
}
});
}
/**
* Call the next on the queue, or not if stopped
* @private
*/
}, {
key: '_callNext',
value: function _callNext() {
if (this.stopped) {
return;
}
++this.pos;
this.start();
}
}]);
return Sequence;
})(_events.EventEmitter);
exports['default'] = Sequence;
module.exports = exports['default'];