pending-queue
Version:
pending-queue ensures a certain asynchronous method only run once, and queues listeners which are registered to it.
103 lines (78 loc) • 3.99 kB
JavaScript
;
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; }; }();
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 _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
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 _require = require('events'),
EventEmitter = _require.EventEmitter;
var assert = require('assert');
module.exports = function (_EventEmitter) {
_inherits(Queue, _EventEmitter);
function Queue(options) {
_classCallCheck(this, Queue);
var _this = _possibleConstructorReturn(this, (Queue.__proto__ || Object.getPrototypeOf(Queue)).call(this));
_this._stringify = JSON.stringify;
_this.setMaxListeners(0);
assert(Object(options) === options, 'options must be an object');
var load = options.load,
stringify = options.stringify;
assert(typeof load === 'function', 'options.load must be a function');
_this._load = load;
if (stringify) {
_this._stringify = stringify;
}
return _this;
}
_createClass(Queue, [{
key: 'add',
value: function add() {
var stringify = this._stringify;
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return this._add(stringify(args), args);
}
}, {
key: 'addWithKey',
value: function addWithKey(key) {
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
args[_key2 - 1] = arguments[_key2];
}
return this._add(key, args);
}
}, {
key: '_add',
value: function _add(key, args) {
var _this2 = this;
return new Promise(function (resolve, reject) {
_this2._run(key, args, function (err, data) {
if (err) {
return reject(err);
}
resolve(data);
});
});
}
}, {
key: '_run',
value: function _run(key, args, callback) {
var _this3 = this;
this.on(key, callback);
if (this.listenerCount(key) !== 1) {
return;
}
// Avoid load method to access the context of pending-queue
var load = this._load;
Promise.resolve(load.apply(undefined, _toConsumableArray(args))).then(function (data) {
_this3.emit('load', key, data);
_this3.emit(key, null, data);
_this3.removeAllListeners(key);
}, function (err) {
_this3.emit(key, err);
_this3.removeAllListeners(key);
});
}
}]);
return Queue;
}(EventEmitter);