Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@wireapp/priority-queue

Package Overview
Dependencies
Maintainers
8
Versions
275
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wireapp/priority-queue - npm Package Compare versions

Comparing version 0.1.24 to 0.1.25

8

CHANGELOG.md

@@ -6,2 +6,10 @@ # Change Log

<a name="0.1.25"></a>
## [0.1.25](https://github.com/wireapp/wire-web-packages/tree/master/packages/priority-queue/compare/@wireapp/priority-queue@0.1.24...@wireapp/priority-queue@0.1.25) (2018-05-16)
**Note:** Version bump only for package @wireapp/priority-queue
<a name="0.1.24"></a>

@@ -8,0 +16,0 @@ ## [0.1.24](https://github.com/wireapp/wire-web-packages/tree/master/packages/priority-queue/compare/@wireapp/priority-queue@0.1.23...@wireapp/priority-queue@0.1.24) (2018-05-14)

262

dist/priority-queue.bundle.js

@@ -1,262 +0,2 @@

var PriorityQueue =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "/";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 2);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Priority;
(function (Priority) {
Priority[Priority["LOW"] = 0] = "LOW";
Priority[Priority["MEDIUM"] = 5] = "MEDIUM";
Priority[Priority["HIGH"] = 9] = "HIGH";
})(Priority || (Priority = {}));
exports.default = Priority;
//# sourceMappingURL=Priority.js.map
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Priority_1 = __webpack_require__(0);
var Item = (function () {
function Item() {
this.fn = function () { };
this.priority = Priority_1.default.MEDIUM;
this.reject = function () { };
this.resolve = function () { };
this.retry = Infinity;
this.timestamp = 0;
}
return Item;
}());
exports.default = Item;
//# sourceMappingURL=Item.js.map
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Item_1 = __webpack_require__(1);
exports.Item = Item_1.default;
var Priority_1 = __webpack_require__(0);
exports.Priority = Priority_1.default;
var PriorityQueue_1 = __webpack_require__(3);
exports.PriorityQueue = PriorityQueue_1.default;
//# sourceMappingURL=index.js.map
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var Item_1 = __webpack_require__(1);
var Priority_1 = __webpack_require__(0);
var PriorityQueue = (function () {
function PriorityQueue(config) {
this.config = {
comparator: function (a, b) {
if (a.priority === b.priority) {
return a.timestamp - b.timestamp;
}
return b.priority - a.priority;
},
maxRetries: Infinity,
retryDelay: 1000,
};
this.isPending = false;
this.queue = [];
this.config = __assign({}, this.config, config);
}
PriorityQueue.prototype.add = function (thunkedPromise, priority, label) {
var _this = this;
if (priority === void 0) { priority = Priority_1.default.MEDIUM; }
if (typeof thunkedPromise !== 'function') {
thunkedPromise = function () { return thunkedPromise; };
}
return new Promise(function (resolve, reject) {
var queueObject = new Item_1.default();
queueObject.fn = thunkedPromise;
queueObject.label = label;
queueObject.priority = priority;
queueObject.reject = reject;
queueObject.resolve = resolve;
queueObject.retry = Number(_this.config.maxRetries) >= 0 ? Number(_this.config.maxRetries) : queueObject.retry;
queueObject.timestamp = Date.now() + _this.size;
_this.queue.push(queueObject);
_this.queue.sort(_this.config.comparator);
_this.run();
});
};
PriorityQueue.prototype.delete = function (label) {
this.queue = this.queue.filter(function (item) { return item.label !== label; });
};
PriorityQueue.prototype.deleteAll = function () {
this.queue = [];
};
Object.defineProperty(PriorityQueue.prototype, "all", {
get: function () {
return this.queue;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PriorityQueue.prototype, "first", {
get: function () {
return this.queue[0];
},
enumerable: true,
configurable: true
});
Object.defineProperty(PriorityQueue.prototype, "last", {
get: function () {
return this.queue[this.queue.length - 1];
},
enumerable: true,
configurable: true
});
Object.defineProperty(PriorityQueue.prototype, "size", {
get: function () {
return this.queue.length;
},
enumerable: true,
configurable: true
});
PriorityQueue.prototype.resolveItems = function () {
var _this = this;
var queueObject = this.first;
if (!queueObject) {
return;
}
Promise.resolve(queueObject.fn())
.then(function (result) {
return { shouldContinue: true, wrappedResolve: function () { return queueObject.resolve(result); } };
})
.catch(function (error) {
if (queueObject.retry > 0) {
queueObject.retry -= 1;
setTimeout(function () { return _this.resolveItems(); }, _this.config.retryDelay || 1000);
return { shouldContinue: false };
}
else {
queueObject.reject(error);
return { shouldContinue: true };
}
})
.then(function (_a) {
var shouldContinue = _a.shouldContinue, wrappedResolve = _a.wrappedResolve;
if (shouldContinue) {
if (wrappedResolve) {
wrappedResolve();
}
_this.isPending = false;
var nextItem = _this.queue.shift();
if (nextItem) {
_this.resolveItems();
}
}
});
};
PriorityQueue.prototype.run = function () {
if (!this.isPending && this.first) {
this.isPending = true;
this.resolveItems();
}
};
PriorityQueue.prototype.toString = function () {
return this.queue
.map(function (item, index) {
return "\"" + index + "\": " + item.fn.toString().replace(/(\r\n|\n|\r|\s+)/gm, '');
})
.join('\r\n');
};
return PriorityQueue;
}());
exports.default = PriorityQueue;
//# sourceMappingURL=PriorityQueue.js.map
/***/ })
/******/ ]);
var PriorityQueue=function(e){var t={};function r(n){if(t[n])return t[n].exports;var i=t[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:n})},r.r=function(e){Object.defineProperty(e,"__esModule",{value:!0})},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="/",r(r.s=0)}([function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=r(2);t.Item=n.default;var i=r(1);t.Priority=i.default;var o=r(3);t.PriorityQueue=o.default},function(e,t,r){"use strict";var n;Object.defineProperty(t,"__esModule",{value:!0}),function(e){e[e.LOW=0]="LOW",e[e.MEDIUM=5]="MEDIUM",e[e.HIGH=9]="HIGH"}(n||(n={})),t.default=n},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=r(1),i=function(){return function(){this.fn=function(){},this.priority=n.default.MEDIUM,this.reject=function(){},this.resolve=function(){},this.retry=1/0,this.timestamp=0}}();t.default=i},function(e,t,r){"use strict";var n=this&&this.__assign||Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e};Object.defineProperty(t,"__esModule",{value:!0});var i=r(2),o=r(1),u=function(){function e(e){this.config={comparator:function(e,t){return e.priority===t.priority?e.timestamp-t.timestamp:t.priority-e.priority},maxRetries:1/0,retryDelay:1e3},this.isPending=!1,this.queue=[],this.config=n({},this.config,e)}return e.prototype.add=function(e,t,r){var n=this;return void 0===t&&(t=o.default.MEDIUM),"function"!=typeof e&&(e=function(){return e}),new Promise(function(o,u){var s=new i.default;s.fn=e,s.label=r,s.priority=t,s.reject=u,s.resolve=o,s.retry=Number(n.config.maxRetries)>=0?Number(n.config.maxRetries):s.retry,s.timestamp=Date.now()+n.size,n.queue.push(s),n.queue.sort(n.config.comparator),n.run()})},e.prototype.delete=function(e){this.queue=this.queue.filter(function(t){return t.label!==e})},e.prototype.deleteAll=function(){this.queue=[]},Object.defineProperty(e.prototype,"all",{get:function(){return this.queue},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"first",{get:function(){return this.queue[0]},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"last",{get:function(){return this.queue[this.queue.length-1]},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"size",{get:function(){return this.queue.length},enumerable:!0,configurable:!0}),e.prototype.resolveItems=function(){var e=this,t=this.first;t&&Promise.resolve(t.fn()).then(function(e){return{shouldContinue:!0,wrappedResolve:function(){return t.resolve(e)}}}).catch(function(r){return t.retry>0?(t.retry-=1,setTimeout(function(){return e.resolveItems()},e.config.retryDelay||1e3),{shouldContinue:!1}):(t.reject(r),{shouldContinue:!0})}).then(function(t){var r=t.shouldContinue,n=t.wrappedResolve;r&&(n&&n(),e.isPending=!1,e.queue.shift()&&e.resolveItems())})},e.prototype.run=function(){!this.isPending&&this.first&&(this.isPending=!0,this.resolveItems())},e.prototype.toString=function(){return this.queue.map(function(e,t){return'"'+t+'": '+e.fn.toString().replace(/(\r\n|\n|\r|\s+)/gm,"")}).join("\r\n")},e}();t.default=u}]);
//# sourceMappingURL=priority-queue.bundle.js.map

@@ -1,657 +0,2 @@

var PriorityQueue =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "/";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 4);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Priority;
(function (Priority) {
Priority[Priority["LOW"] = 0] = "LOW";
Priority[Priority["MEDIUM"] = 5] = "MEDIUM";
Priority[Priority["HIGH"] = 9] = "HIGH";
})(Priority || (Priority = {}));
exports.default = Priority;
//# sourceMappingURL=Priority.js.map
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Priority_1 = __webpack_require__(0);
var Item = (function () {
function Item() {
this.fn = function () { };
this.priority = Priority_1.default.MEDIUM;
this.reject = function () { };
this.resolve = function () { };
this.retry = Infinity;
this.timestamp = 0;
}
return Item;
}());
exports.default = Item;
//# sourceMappingURL=Item.js.map
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Item_1 = __webpack_require__(1);
exports.Item = Item_1.default;
var Priority_1 = __webpack_require__(0);
exports.Priority = Priority_1.default;
var PriorityQueue_1 = __webpack_require__(3);
exports.PriorityQueue = PriorityQueue_1.default;
//# sourceMappingURL=index.js.map
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var Item_1 = __webpack_require__(1);
var Priority_1 = __webpack_require__(0);
var PriorityQueue = (function () {
function PriorityQueue(config) {
this.config = {
comparator: function (a, b) {
if (a.priority === b.priority) {
return a.timestamp - b.timestamp;
}
return b.priority - a.priority;
},
maxRetries: Infinity,
retryDelay: 1000,
};
this.isPending = false;
this.queue = [];
this.config = __assign({}, this.config, config);
}
PriorityQueue.prototype.add = function (thunkedPromise, priority, label) {
var _this = this;
if (priority === void 0) { priority = Priority_1.default.MEDIUM; }
if (typeof thunkedPromise !== 'function') {
thunkedPromise = function () { return thunkedPromise; };
}
return new Promise(function (resolve, reject) {
var queueObject = new Item_1.default();
queueObject.fn = thunkedPromise;
queueObject.label = label;
queueObject.priority = priority;
queueObject.reject = reject;
queueObject.resolve = resolve;
queueObject.retry = Number(_this.config.maxRetries) >= 0 ? Number(_this.config.maxRetries) : queueObject.retry;
queueObject.timestamp = Date.now() + _this.size;
_this.queue.push(queueObject);
_this.queue.sort(_this.config.comparator);
_this.run();
});
};
PriorityQueue.prototype.delete = function (label) {
this.queue = this.queue.filter(function (item) { return item.label !== label; });
};
PriorityQueue.prototype.deleteAll = function () {
this.queue = [];
};
Object.defineProperty(PriorityQueue.prototype, "all", {
get: function () {
return this.queue;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PriorityQueue.prototype, "first", {
get: function () {
return this.queue[0];
},
enumerable: true,
configurable: true
});
Object.defineProperty(PriorityQueue.prototype, "last", {
get: function () {
return this.queue[this.queue.length - 1];
},
enumerable: true,
configurable: true
});
Object.defineProperty(PriorityQueue.prototype, "size", {
get: function () {
return this.queue.length;
},
enumerable: true,
configurable: true
});
PriorityQueue.prototype.resolveItems = function () {
var _this = this;
var queueObject = this.first;
if (!queueObject) {
return;
}
Promise.resolve(queueObject.fn())
.then(function (result) {
return { shouldContinue: true, wrappedResolve: function () { return queueObject.resolve(result); } };
})
.catch(function (error) {
if (queueObject.retry > 0) {
queueObject.retry -= 1;
setTimeout(function () { return _this.resolveItems(); }, _this.config.retryDelay || 1000);
return { shouldContinue: false };
}
else {
queueObject.reject(error);
return { shouldContinue: true };
}
})
.then(function (_a) {
var shouldContinue = _a.shouldContinue, wrappedResolve = _a.wrappedResolve;
if (shouldContinue) {
if (wrappedResolve) {
wrappedResolve();
}
_this.isPending = false;
var nextItem = _this.queue.shift();
if (nextItem) {
_this.resolveItems();
}
}
});
};
PriorityQueue.prototype.run = function () {
if (!this.isPending && this.first) {
this.isPending = true;
this.resolveItems();
}
};
PriorityQueue.prototype.toString = function () {
return this.queue
.map(function (item, index) {
return "\"" + index + "\": " + item.fn.toString().replace(/(\r\n|\n|\r|\s+)/gm, '');
})
.join('\r\n');
};
return PriorityQueue;
}());
exports.default = PriorityQueue;
//# sourceMappingURL=PriorityQueue.js.map
/***/ }),
/* 4 */
/***/ (function(module, exports, __webpack_require__) {
/*
* Wire
* Copyright (C) 2018 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/
// traverse all test files for webpack dependency resolution
const testsContext = __webpack_require__(5);
testsContext.keys().forEach(testsContext);
/***/ }),
/* 5 */
/***/ (function(module, exports, __webpack_require__) {
var map = {
"./PriorityQueue.test.js": 6
};
function webpackContext(req) {
return __webpack_require__(webpackContextResolve(req));
};
function webpackContextResolve(req) {
var id = map[req];
if(!(id + 1)) // check for number or string
throw new Error("Cannot find module '" + req + "'.");
return id;
};
webpackContext.keys = function webpackContextKeys() {
return Object.keys(map);
};
webpackContext.resolve = webpackContextResolve;
module.exports = webpackContext;
webpackContext.id = 5;
/***/ }),
/* 6 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__ = __webpack_require__(2);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__);
/* eslint-disable no-magic-numbers */
/*
* Wire
* Copyright (C) 2018 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/
beforeAll(() => (jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000));
describe('PriorityQueue', () => {
describe('"constructor"', () => {
it('supports a custom retry delay of 3 seconds', done => {
let isLocked = true;
const businessLogic = () => {
return new Promise((resolve, reject) => {
if (isLocked) {
reject(new Error('Promise is locked.'));
} else {
resolve('Promise successfully executed.');
done();
}
});
};
setTimeout(() => (isLocked = false), 1500);
const queue = new __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["PriorityQueue"]({retryDelay: 3000});
queue.add(businessLogic);
});
it('supports limiting the amount of retries', done => {
const businessLogic = () => Promise.reject(new Error('Error'));
const queue = new __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["PriorityQueue"]({maxRetries: 1});
queue
.add(businessLogic)
.then(done.fail)
.catch(() => {
expect(queue.size).toBe(0);
done();
});
});
});
describe('"add"', () => {
it('adds objects', () => {
const queue = new __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["PriorityQueue"]();
queue.isPending = true;
queue.add(() => 'ape');
queue.add(() => 'cat');
queue.add(() => 'dog');
queue.add(() => 'zebra');
expect(queue.size).toBe(4);
});
it('adds objects with priorities', done => {
const queue = new __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["PriorityQueue"]();
queue.isPending = true;
queue.add(() => 'ape');
queue.add(() => 'cat', __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["Priority"].LOW);
queue.add(() => 'dog', __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["Priority"].HIGH);
queue.add(() => 'zebra');
Promise.resolve()
.then(() => {
return queue.first.fn();
})
.then(value => {
expect(value).toBe('dog');
return queue.last.fn();
})
.then(value => {
expect(value).toBe('cat');
done();
});
});
it('works with thunked Promises', done => {
const queue = new __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["PriorityQueue"]();
Promise.all([
queue.add(() => Promise.resolve('ape')),
queue.add(() => Promise.resolve('cat')),
queue.add(() => Promise.resolve('dog')),
queue.add(() => Promise.resolve('zebra')),
]).then(results => {
expect(results[0]).toBe('ape');
expect(results[1]).toBe('cat');
expect(results[2]).toBe('dog');
expect(results[3]).toBe('zebra');
done();
});
});
});
describe('"run"', () => {
it('works with primitive values', done => {
const queue = new __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["PriorityQueue"]();
const zebra = () => Promise.resolve('zebra').then(done());
queue.add('ape');
queue.add('cat');
queue.add('dog');
queue.add(zebra);
});
it('executes an item from the queue', done => {
const queue = new __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["PriorityQueue"]();
const ape = () => Promise.resolve('ape').then(done());
queue.add(ape);
queue.add(() => 'cat');
queue.add(() => 'dog');
queue.add(() => 'zebra');
});
it('executes an item from the queue with different priorities', done => {
const queue = new __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["PriorityQueue"]();
const promise1 = () =>
Promise.resolve('one').then(item => {
expect(item).toBe('one');
return 'one';
});
const promise2 = () =>
Promise.resolve('two').then(item => {
expect(item).toBe('two');
return 'two';
});
const promise3 = () =>
Promise.resolve('three').then(item => {
expect(item).toBe('three');
done();
});
queue.add(promise1, __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["Priority"].HIGH);
queue.add(promise2, __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["Priority"].MEDIUM);
queue.add(promise3, __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["Priority"].LOW);
});
it('retries on error until the error gets resolved', done => {
let isLocked = true;
const businessLogic = () => {
return new Promise((resolve, reject) => {
if (isLocked) {
reject(new Error('Promise is locked.'));
} else {
resolve('Promise successfully executed.');
done();
}
});
};
const unlock = () => {
return new Promise(resolve => {
isLocked = false;
resolve();
});
};
const queue = new __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["PriorityQueue"]();
queue.add(businessLogic);
setTimeout(() => queue.add(unlock, __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["Priority"].HIGH), 200);
});
it('works with error-catching Promises', done => {
const queue = new __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["PriorityQueue"]();
function businessLogic(param) {
return new Promise((resolve, reject) => {
if (isNaN(param)) {
reject(new TypeError('Not a Number'));
} else {
resolve(param);
}
});
}
const promise1 = () =>
businessLogic('A')
.catch(() => businessLogic(42))
.then(item => expect(item).toBe(42));
const promise2 = () => Promise.resolve('two').then(item => expect(item).toBe('two'));
const promise3 = () => Promise.resolve('three').then(() => done());
queue.add(promise1, __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["Priority"].HIGH);
queue.add(promise2, __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["Priority"].MEDIUM);
queue.add(promise3, __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["Priority"].LOW);
});
it('executes a high priority element prior to other running elements ', done => {
const queue = new __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["PriorityQueue"]();
const promise1 = () => Promise.resolve('one').then(item => expect(item).toBe('one'));
const promise2 = () => Promise.reject(new Error('two'));
const promise3 = () =>
Promise.resolve('three').then(item => {
expect(item).toBe('three');
done();
});
queue.add(promise1);
queue.add(promise2);
setTimeout(() => queue.add(promise3, __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["Priority"].HIGH), 1000);
});
});
describe('"size"', () => {
it('returns the size of the items left after Promise execution', done => {
let isLocked = true;
const businessLogic = () => {
return new Promise((resolve, reject) => {
if (isLocked) {
reject(new Error('Promise is locked.'));
} else {
resolve('Promise successfully executed.');
}
});
};
const unlock = () => {
return new Promise(resolve => {
isLocked = false;
resolve();
});
};
const queue = new __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["PriorityQueue"]({maxRetries: Infinity, retryDelay: 100});
setTimeout(() => queue.add(unlock, __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["Priority"].HIGH), 1000);
queue.add(businessLogic).then(() => {
expect(queue.size).toBe(0);
done();
});
});
});
describe('"comparator"', () => {
it('uses a descending priority order by default', done => {
const queue = new __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["PriorityQueue"]();
queue.isPending = true;
queue.add(() => 'ape', __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["Priority"].HIGH);
queue.add(() => 'cat');
queue.add(() => 'dog');
queue.add(() => 'zebra', __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["Priority"].LOW);
Promise.resolve()
.then(() => {
return queue.last.fn();
})
.then(value => {
expect(value).toBe('zebra');
return queue.first.fn();
})
.then(value => {
expect(value).toBe('ape');
done();
});
});
it('supports a custom comparator', done => {
const ascendingPriority = (first, second) => first.priority - second.priority;
const queue = new __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["PriorityQueue"]({comparator: ascendingPriority});
queue.isPending = true;
queue.add(() => 'ape', __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["Priority"].HIGH);
queue.add(() => 'cat');
queue.add(() => 'dog');
queue.add(() => 'zebra', __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["Priority"].LOW);
Promise.resolve()
.then(() => {
return queue.first.fn();
})
.then(value => {
expect(value).toBe('zebra');
return queue.last.fn();
})
.then(value => {
expect(value).toBe('ape');
done();
});
});
it('continues after the maximum amount of retries', done => {
const queue = new __WEBPACK_IMPORTED_MODULE_0__wireapp_priority_queue__["PriorityQueue"]({maxRetries: 5});
const promise1 = () =>
Promise.resolve('one').then(item => {
expect(item).toBe('one');
return 'one';
});
const promise2 = () =>
Promise.reject(new Error('two')).then(item => {
expect(item).toBe('two');
return 'two';
});
const promise3 = () =>
Promise.resolve('three').then(item => {
expect(item).toBe('three');
done();
});
queue.add(promise1);
queue.add(promise2);
setTimeout(() => queue.add(promise3), 1000);
});
});
});
/***/ })
/******/ ]);
var PriorityQueue=function(e){var t={};function r(o){if(t[o])return t[o].exports;var i=t[o]={i:o,l:!1,exports:{}};return e[o].call(i.exports,i,i.exports,r),i.l=!0,i.exports}return r.m=e,r.c=t,r.d=function(e,t,o){r.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:o})},r.r=function(e){Object.defineProperty(e,"__esModule",{value:!0})},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="/",r(r.s=6)}([function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var o=r(2);t.Item=o.default;var i=r(1);t.Priority=i.default;var n=r(3);t.PriorityQueue=n.default},function(e,t,r){"use strict";var o;Object.defineProperty(t,"__esModule",{value:!0}),function(e){e[e.LOW=0]="LOW",e[e.MEDIUM=5]="MEDIUM",e[e.HIGH=9]="HIGH"}(o||(o={})),t.default=o},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var o=r(1),i=function(){return function(){this.fn=function(){},this.priority=o.default.MEDIUM,this.reject=function(){},this.resolve=function(){},this.retry=1/0,this.timestamp=0}}();t.default=i},function(e,t,r){"use strict";var o=this&&this.__assign||Object.assign||function(e){for(var t,r=1,o=arguments.length;r<o;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e};Object.defineProperty(t,"__esModule",{value:!0});var i=r(2),n=r(1),s=function(){function e(e){this.config={comparator:function(e,t){return e.priority===t.priority?e.timestamp-t.timestamp:t.priority-e.priority},maxRetries:1/0,retryDelay:1e3},this.isPending=!1,this.queue=[],this.config=o({},this.config,e)}return e.prototype.add=function(e,t,r){var o=this;return void 0===t&&(t=n.default.MEDIUM),"function"!=typeof e&&(e=function(){return e}),new Promise(function(n,s){var u=new i.default;u.fn=e,u.label=r,u.priority=t,u.reject=s,u.resolve=n,u.retry=Number(o.config.maxRetries)>=0?Number(o.config.maxRetries):u.retry,u.timestamp=Date.now()+o.size,o.queue.push(u),o.queue.sort(o.config.comparator),o.run()})},e.prototype.delete=function(e){this.queue=this.queue.filter(function(t){return t.label!==e})},e.prototype.deleteAll=function(){this.queue=[]},Object.defineProperty(e.prototype,"all",{get:function(){return this.queue},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"first",{get:function(){return this.queue[0]},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"last",{get:function(){return this.queue[this.queue.length-1]},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"size",{get:function(){return this.queue.length},enumerable:!0,configurable:!0}),e.prototype.resolveItems=function(){var e=this,t=this.first;t&&Promise.resolve(t.fn()).then(function(e){return{shouldContinue:!0,wrappedResolve:function(){return t.resolve(e)}}}).catch(function(r){return t.retry>0?(t.retry-=1,setTimeout(function(){return e.resolveItems()},e.config.retryDelay||1e3),{shouldContinue:!1}):(t.reject(r),{shouldContinue:!0})}).then(function(t){var r=t.shouldContinue,o=t.wrappedResolve;r&&(o&&o(),e.isPending=!1,e.queue.shift()&&e.resolveItems())})},e.prototype.run=function(){!this.isPending&&this.first&&(this.isPending=!0,this.resolveItems())},e.prototype.toString=function(){return this.queue.map(function(e,t){return'"'+t+'": '+e.fn.toString().replace(/(\r\n|\n|\r|\s+)/gm,"")}).join("\r\n")},e}();t.default=s},function(e,t,r){"use strict";r.r(t);var o=r(0);beforeAll(()=>jasmine.DEFAULT_TIMEOUT_INTERVAL=1e4),describe("PriorityQueue",()=>{describe('"constructor"',()=>{it("supports a custom retry delay of 3 seconds",e=>{let t=!0;setTimeout(()=>t=!1,1500),new o.PriorityQueue({retryDelay:3e3}).add(()=>new Promise((r,o)=>{t?o(new Error("Promise is locked.")):(r("Promise successfully executed."),e())}))}),it("supports limiting the amount of retries",e=>{const t=new o.PriorityQueue({maxRetries:1});t.add(()=>Promise.reject(new Error("Error"))).then(e.fail).catch(()=>{expect(t.size).toBe(0),e()})})}),describe('"add"',()=>{it("adds objects",()=>{const e=new o.PriorityQueue;e.isPending=!0,e.add(()=>"ape"),e.add(()=>"cat"),e.add(()=>"dog"),e.add(()=>"zebra"),expect(e.size).toBe(4)}),it("adds objects with priorities",e=>{const t=new o.PriorityQueue;t.isPending=!0,t.add(()=>"ape"),t.add(()=>"cat",o.Priority.LOW),t.add(()=>"dog",o.Priority.HIGH),t.add(()=>"zebra"),Promise.resolve().then(()=>t.first.fn()).then(e=>(expect(e).toBe("dog"),t.last.fn())).then(t=>{expect(t).toBe("cat"),e()})}),it("works with thunked Promises",e=>{const t=new o.PriorityQueue;Promise.all([t.add(()=>Promise.resolve("ape")),t.add(()=>Promise.resolve("cat")),t.add(()=>Promise.resolve("dog")),t.add(()=>Promise.resolve("zebra"))]).then(t=>{expect(t[0]).toBe("ape"),expect(t[1]).toBe("cat"),expect(t[2]).toBe("dog"),expect(t[3]).toBe("zebra"),e()})})}),describe('"run"',()=>{it("works with primitive values",e=>{const t=new o.PriorityQueue;t.add("ape"),t.add("cat"),t.add("dog"),t.add(()=>Promise.resolve("zebra").then(e()))}),it("executes an item from the queue",e=>{const t=new o.PriorityQueue;t.add(()=>Promise.resolve("ape").then(e())),t.add(()=>"cat"),t.add(()=>"dog"),t.add(()=>"zebra")}),it("executes an item from the queue with different priorities",e=>{const t=new o.PriorityQueue;t.add(()=>Promise.resolve("one").then(e=>(expect(e).toBe("one"),"one")),o.Priority.HIGH),t.add(()=>Promise.resolve("two").then(e=>(expect(e).toBe("two"),"two")),o.Priority.MEDIUM),t.add(()=>Promise.resolve("three").then(t=>{expect(t).toBe("three"),e()}),o.Priority.LOW)}),it("retries on error until the error gets resolved",e=>{let t=!0;const r=()=>new Promise(e=>{t=!1,e()}),i=new o.PriorityQueue;i.add(()=>new Promise((r,o)=>{t?o(new Error("Promise is locked.")):(r("Promise successfully executed."),e())})),setTimeout(()=>i.add(r,o.Priority.HIGH),200)}),it("works with error-catching Promises",e=>{const t=new o.PriorityQueue;function r(e){return new Promise((t,r)=>{isNaN(e)?r(new TypeError("Not a Number")):t(e)})}t.add(()=>r("A").catch(()=>r(42)).then(e=>expect(e).toBe(42)),o.Priority.HIGH),t.add(()=>Promise.resolve("two").then(e=>expect(e).toBe("two")),o.Priority.MEDIUM),t.add(()=>Promise.resolve("three").then(()=>e()),o.Priority.LOW)}),it("executes a high priority element prior to other running elements ",e=>{const t=new o.PriorityQueue,r=()=>Promise.resolve("three").then(t=>{expect(t).toBe("three"),e()});t.add(()=>Promise.resolve("one").then(e=>expect(e).toBe("one"))),t.add(()=>Promise.reject(new Error("two"))),setTimeout(()=>t.add(r,o.Priority.HIGH),1e3)})}),describe('"size"',()=>{it("returns the size of the items left after Promise execution",e=>{let t=!0;const r=()=>new Promise(e=>{t=!1,e()}),i=new o.PriorityQueue({maxRetries:1/0,retryDelay:100});setTimeout(()=>i.add(r,o.Priority.HIGH),1e3),i.add(()=>new Promise((e,r)=>{t?r(new Error("Promise is locked.")):e("Promise successfully executed.")})).then(()=>{expect(i.size).toBe(0),e()})})}),describe('"comparator"',()=>{it("uses a descending priority order by default",e=>{const t=new o.PriorityQueue;t.isPending=!0,t.add(()=>"ape",o.Priority.HIGH),t.add(()=>"cat"),t.add(()=>"dog"),t.add(()=>"zebra",o.Priority.LOW),Promise.resolve().then(()=>t.last.fn()).then(e=>(expect(e).toBe("zebra"),t.first.fn())).then(t=>{expect(t).toBe("ape"),e()})}),it("supports a custom comparator",e=>{const t=new o.PriorityQueue({comparator:(e,t)=>e.priority-t.priority});t.isPending=!0,t.add(()=>"ape",o.Priority.HIGH),t.add(()=>"cat"),t.add(()=>"dog"),t.add(()=>"zebra",o.Priority.LOW),Promise.resolve().then(()=>t.first.fn()).then(e=>(expect(e).toBe("zebra"),t.last.fn())).then(t=>{expect(t).toBe("ape"),e()})}),it("continues after the maximum amount of retries",e=>{const t=new o.PriorityQueue({maxRetries:5}),r=()=>Promise.resolve("three").then(t=>{expect(t).toBe("three"),e()});t.add(()=>Promise.resolve("one").then(e=>(expect(e).toBe("one"),"one"))),t.add(()=>Promise.reject(new Error("two")).then(e=>(expect(e).toBe("two"),"two"))),setTimeout(()=>t.add(r),1e3)})})})},function(e,t,r){var o={"./PriorityQueue.test.js":4};function i(e){var t=n(e);return r(t)}function n(e){var t=o[e];if(!(t+1)){var r=new Error('Cannot find module "'+e+'".');throw r.code="MODULE_NOT_FOUND",r}return t}i.keys=function(){return Object.keys(o)},i.resolve=n,e.exports=i,i.id=5},function(e,t,r){const o=r(5);o.keys().forEach(o)}]);
//# sourceMappingURL=priority-queue.test.bundle.js.map

@@ -16,3 +16,4 @@ {

"typescript": "2.8.3",
"webpack": "3.11.0"
"webpack": "4.8.3",
"webpack-cli": "2.1.3"
},

@@ -34,8 +35,8 @@ "description": "A Promise-based dynamic priority queue runner.",

"test": "yarn test:node && yarn test:browser",
"test:browser": "webpack && karma start",
"test:project": "yarn dist && yarn test",
"test:browser": "webpack && karma start",
"test:node": "cross-env JASMINE_CONFIG_PATH=src/test/node/support/jasmine.json jasmine"
},
"types": "./dist/index.d.ts",
"version": "0.1.24"
"version": "0.1.25"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc