schedulerjs
Version:
A promise execution flow control library
139 lines (107 loc) • 5.01 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 _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');
/**
* Scheduler class. Starts Sequences in parallel
*/
var Scheduler = (function (_EventEmitter) {
_inherits(Scheduler, _EventEmitter);
/**
* Initializes the object
*/
function Scheduler() {
_classCallCheck(this, Scheduler);
_get(Object.getPrototypeOf(Scheduler.prototype), 'constructor', this).call(this);
this.sequences = [];
this.sequencesDone = 0;
}
/**
* Adds a sequence
* @param {Sequence} sequence The sequence to add
* @return {Scheduler} This object (chaining)
*/
_createClass(Scheduler, [{
key: 'add',
value: function add(sequence) {
var _this = this;
this.sequences.push(sequence);
sequence.on('error', function (reason) {
_this.emit('error', reason);
});
sequence.on('finished', function () {
_this._checkFinished();
});
return this;
}
/**
* Adds a named sequence
* @param {String} sequenceName The sequence name
* @param {Sequence} sequence The sequence to add
* @return {Scheduler} This object (chaining)
*/
}, {
key: 'set',
value: function set(sequenceName, sequence) {
this.add(sequence);
this[sequenceName] = sequence;
return this;
}
/**
* Starts all the sequences
*/
}, {
key: 'start',
value: function start() {
var _this2 = this;
// use nextTick to allow events after start
process.nextTick(function () {
_this2.emit('started');
if (_this2.sequences.length === 0) {
_this2.emit('finished');
}
_this2.sequences.forEach(function (sequence) {
sequence.start();
});
});
}
/**
* Stops all the sequences
*/
}, {
key: 'stop',
value: function stop() {
var _this3 = this;
process.nextTick(function () {
_this3.sequences.forEach(function (sequence) {
sequence.stop();
});
_this3.emit('stopped');
});
}
/**
* Checks wether all the sequences are completed or nto
* @private
*/
}, {
key: '_checkFinished',
value: function _checkFinished() {
var _this4 = this;
++this.sequencesDone;
if (this.sequencesDone === this.sequences.length) {
// ensure scheduler finish after all sequences
process.nextTick(function () {
_this4.emit('finished');
});
}
}
}]);
return Scheduler;
})(_events.EventEmitter);
exports['default'] = Scheduler;
module.exports = exports['default'];