@wireapp/priority-queue
Advanced tools
Comparing version 1.2.23 to 1.3.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [1.3.0](https://github.com/wireapp/wire-web-packages/tree/master/packages/priority-queue/compare/@wireapp/priority-queue@1.2.23...@wireapp/priority-queue@1.3.0) (2019-10-14) | ||
### Features | ||
* **priority-queue:** Growing retry delay ([#2423](https://github.com/wireapp/wire-web-packages/tree/master/packages/priority-queue/issues/2423)) ([5a60d0f](https://github.com/wireapp/wire-web-packages/tree/master/packages/priority-queue/commit/5a60d0f988e16414e487453f15a77c364c29fa15)) | ||
## [1.2.23](https://github.com/wireapp/wire-web-packages/tree/master/packages/priority-queue/compare/@wireapp/priority-queue@1.2.22...@wireapp/priority-queue@1.2.23) (2019-10-09) | ||
@@ -8,0 +19,0 @@ |
import { Item } from './Item'; | ||
export interface Config { | ||
comparator?: (a: Item, b: Item) => number; | ||
maxRetries?: number; | ||
retryDelay?: number; | ||
comparator: (a: Item, b: Item) => number; | ||
maxRetries: number; | ||
maxRetryDelay: number; | ||
retryDelay: number; | ||
retryGrowthFactor: number; | ||
} |
@@ -14,2 +14,3 @@ export declare class Item { | ||
timestamp: number; | ||
toString(): string; | ||
} |
@@ -36,4 +36,12 @@ "use strict"; | ||
} | ||
toString() { | ||
return ` | ||
label=${this.label}, | ||
priority=${this.priority}, | ||
timestamp=${this.timestamp}, | ||
retry=${this.retry}, | ||
fn=${this.fn.toString().replace(/(\r\n|\n|\r|\s+)/gm, ' ')},`; | ||
} | ||
} | ||
exports.Item = Item; | ||
//# sourceMappingURL=Item.js.map |
@@ -1,2 +0,2 @@ | ||
var PriorityQueue=function(e){var t={};function r(i){if(t[i])return t[i].exports;var n=t[i]={i:i,l:!1,exports:{}};return e[i].call(n.exports,n,n.exports,r),n.l=!0,n.exports}return r.m=e,r.c=t,r.d=function(e,t,i){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:i})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var i=Object.create(null);if(r.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)r.d(i,n,function(t){return e[t]}.bind(null,n));return i},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";function i(e){for(var r in e)t.hasOwnProperty(r)||(t[r]=e[r])}Object.defineProperty(t,"__esModule",{value:!0}),i(r(2)),i(r(1)),i(r(3))},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),function(e){e[e.LOW=0]="LOW",e[e.MEDIUM=5]="MEDIUM",e[e.HIGH=9]="HIGH"}(t.Priority||(t.Priority={}))},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});const i=r(1);t.Item=class{constructor(){this.fn=()=>{},this.priority=i.Priority.MEDIUM,this.reject=()=>{},this.resolve=()=>{},this.retry=1/0,this.timestamp=0}}},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});const i=r(2),n=r(1);t.PriorityQueue=class{constructor(e){this.config={comparator:(e,t)=>e.priority===t.priority?e.timestamp-t.timestamp:t.priority-e.priority,maxRetries:1/0,retryDelay:1e3},this.isPending=!1,this.queue=[],this.config=Object.assign(Object.assign({},this.config),e)}add(e,t=n.Priority.MEDIUM,r){return new Promise((n,s)=>{const o=new i.Item;o.fn=e,o.label=r,o.priority=t,o.reject=s,o.resolve=n,o.retry=Number(this.config.maxRetries)>=0?Number(this.config.maxRetries):o.retry,o.timestamp=Date.now()+this.size,this.queue.push(o),this.queue.sort(this.config.comparator),this.run()})}delete(e){this.queue=this.queue.filter(t=>t.label!==e)}deleteAll(){this.queue=[]}get all(){return this.queue}get first(){return this.queue[0]}get last(){return this.queue[this.queue.length-1]}get size(){return this.queue.length}resolveItems(){const e=this.first;e&&Promise.resolve(e.fn()).then(t=>({shouldContinue:!0,wrappedResolve:()=>e.resolve(t)})).catch(t=>e.retry>0?(e.retry-=1,setTimeout(()=>this.resolveItems(),this.config.retryDelay||1e3),{shouldContinue:!1}):(e.reject(t),{shouldContinue:!0})).then(({shouldContinue:e,wrappedResolve:t})=>{e&&(t&&t(),this.isPending=!1,this.queue.shift(),this.resolveItems())})}run(){!this.isPending&&this.first&&(this.isPending=!0,this.resolveItems())}toString(){return this.queue.map((e,t)=>`"${t}": ${e.fn.toString().replace(/(\r\n|\n|\r|\s+)/gm,"")}`).join("\r\n")}}}]); | ||
var PriorityQueue=function(t){var e={};function r(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)r.d(n,i,function(e){return t[e]}.bind(null,i));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="/",r(r.s=0)}([function(t,e,r){"use strict";function n(t){for(var r in t)e.hasOwnProperty(r)||(e[r]=t[r])}Object.defineProperty(e,"__esModule",{value:!0}),n(r(2)),n(r(1)),n(r(3))},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),function(t){t[t.LOW=0]="LOW",t[t.MEDIUM=5]="MEDIUM",t[t.HIGH=9]="HIGH"}(e.Priority||(e.Priority={}))},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});const n=r(1);e.Item=class{constructor(){this.fn=()=>{},this.priority=n.Priority.MEDIUM,this.reject=()=>{},this.resolve=()=>{},this.retry=1/0,this.timestamp=0}toString(){return`\n label=${this.label},\n priority=${this.priority},\n timestamp=${this.timestamp},\n retry=${this.retry},\n fn=${this.fn.toString().replace(/(\r\n|\n|\r|\s+)/gm," ")},`}}},function(t,e,r){"use strict";var n=this&&this.__awaiter||function(t,e,r,n){return new(r||(r=Promise))((function(i,o){function s(t){try{a(n.next(t))}catch(t){o(t)}}function u(t){try{a(n.throw(t))}catch(t){o(t)}}function a(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(s,u)}a((n=n.apply(t,e||[])).next())}))},i=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0});const o=i(r(4)),s=r(2),u=r(1);e.PriorityQueue=class{constructor(t){this.logger=o.default("@wireapp/priority-queue/PriorityQueue",{logger:console,markdown:!1}),this.config={comparator:(t,e)=>t.priority===e.priority?t.timestamp-e.timestamp:e.priority-t.priority,maxRetries:0,maxRetryDelay:Number.MAX_SAFE_INTEGER,retryDelay:1e3,retryGrowthFactor:1.3},this.isRunning=!1,this.queue=[],this.config=Object.assign(Object.assign({},this.config),t)}add(t,e=u.Priority.MEDIUM,r){return new Promise((n,i)=>{const o=new s.Item;o.fn=t,o.label=r,o.priority=e,o.reject=i,o.resolve=n,o.retry=0,o.timestamp=Date.now()+this.size,this.queue.push(o),this.queue.sort(this.config.comparator),this.isRunning||(this.isRunning=!0,this.processList())})}delete(t){this.queue=this.queue.filter(e=>e.label!==t)}deleteAll(){this.queue=[]}get all(){return this.queue}get first(){return this.queue[0]}get last(){return this.queue[this.queue.length-1]}get size(){return this.queue.length}processList(){return n(this,void 0,void 0,(function*(){const t=this.first;if(t)try{t.resolve(yield t.fn()),this.queue.shift(),this.processList()}catch(e){t.retry>=this.config.maxRetries?(this.queue.shift(),t.reject(e),this.processList()):(this.logger.log(`Retrying item "${t}"`),setTimeout(()=>this.processList(),this.getGrowingDelay(t.retry)),t.retry++)}else this.isRunning=!1}))}getGrowingDelay(t){const e=t<1?this.config.retryDelay:this.config.retryDelay*t*this.config.retryGrowthFactor;return Math.min(e,this.config.maxRetryDelay)}toString(){return this.queue.map((t,e)=>`"${e}": ${t.toString()}`).join("\r\n")}}},function(module,exports,__webpack_require__){var e;window,e=function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)r.d(n,i,function(e){return t[e]}.bind(null,i));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e,r){var n,i=r(1)(),o=r(3),s=r(5),u=r(8)();i.prefixColors=["#F2777A","#F99157","#FFCC66","#99CC99","#66CCCC","#6699CC","#CC99CC"],i._setPrefixRegExps=function(){try{u.localStorage&&"string"==typeof u.localStorage.getItem("debug")&&(i._prefixRegExps=[],u.localStorage.getItem("debug").split(",").forEach((function(t){var e="enable";"-"===(t=t.trim())[0]&&(t=t.substr(1),e="disable");var r=i._prepareRegExpForPrefixSearch(t);i._prefixRegExps.push({type:e,regExp:r})})))}catch(t){}},i._getNextPrefixColor=(n=0,function(){return n+=1,i.prefixColors[n%i.prefixColors.length]}),i.prototype._getDecoratedPrefix=function(){var t=[];return s()?(t.push("%c"+this.opts.prefix+"%c "),t.push("color:"+this.opts.prefixColor+"; font-weight:bold;","")):t.push("["+this.opts.prefix+"] "),t},i.prototype._prepareOutput=function(t){var e,r=this._getDecoratedPrefix();return"string"==typeof t[0]?this.opts.markdown&&s()?(e=o.parse(t[0]),r[0]=r[0]+e.text,r=r.concat(e.styles)):r[0]=r[0]+t[0]:r.push(t[0]),1<t.length&&(r=r.concat(t.slice(1))),r},i._setPrefixRegExps(),t.exports=i},function(t,e,r){var n=r(2);t.exports=function(){function t(e,r){return this instanceof t?t._isPrefixAlreadyInUse(e)?t._getInstanceByPrefix(e):(this.opts=t._normalizeOpts(e,r),this.state=t._getInitialState(this.opts),t._decorateLoggerMethods(this),t._instances.push(this),this):new t(e,r)}return t.transports=[],t._instances=[],t._prefixRegExps=[],t._prepareRegExpForPrefixSearch=function(t){return new RegExp("^"+t.replace(/\*/g,".*?")+"$")},t._isPrefixAlreadyInUse=function(e){return t._instances.some((function(t){return t.opts.prefix===e}))},t._getInstanceByPrefix=function(e){return t._instances.filter((function(t){return t.opts.prefix===e}))[0]},t._normalizeOpts=function(e,r){if("string"!=typeof e)throw new TypeError("prefix must be a string");var n=void 0===(r=r||{}).markdown||Boolean(r.markdown),i=r.prefixColor||t._getNextPrefixColor();return{logger:r.logger||console,markdown:n,prefix:e,prefixColor:i}},t._getInitialState=function(e){return{isEnabled:t._getEnableState(e)}},t._getEnableState=function(e){var r=!1;return t._prefixRegExps.forEach((function(t){"enable"===t.type&&t.regExp.test(e.prefix)?r=!0:"disable"===t.type&&t.regExp.test(e.prefix)&&(r=!1)})),r},t._decorateLoggerMethods=function(e){var r=e.opts.logger,i=Object.keys(r).filter((function(t){return"function"==typeof r[t]}));0===i.length&&(i=["log","warn","error","info"]),i.forEach((function(i){e[i]=function(){var e=n(arguments),o=this.opts.prefix;if(t.transports.length){var s="["+o+"] "+e.filter((function(t){return"object"!=typeof t})).join(" ");t.transports.forEach(function(t){t({state:this.state,instance:o,level:i,args:e,msg:s})}.bind(this))}if(this.state.isEnabled){var u=this._prepareOutput(e,i);r[i].apply(r,u)}}}))},t}},function(t,e){t.exports=function(t){return Array.prototype.slice.call(t,0)}},function(t,e,r){var n=[];function i(t){return function(e){return n.push(t),n.push(""),"%c"+e+"%c"}}var o=new(r(4))({renderer:{"*":i("font-weight:bold;"),_:i("font-style:italic;"),"`":i("background-color:rgba(255,204,102, 0.1);color:#FFCC66;padding:2px 5px;border-radius:2px;")}});t.exports={parse:function(t){var e={text:o.parse(t),styles:[].concat(n)};return n.length=0,e}}},function(t,e){var r=/([_*`\\]|[^_*`\\]+)/g,n=/[_*`]/;function i(t){this.renderer=t.renderer}function o(t){return n.test(t)}i.prototype.parse=function(t){var e,n,i,s=t.match(r),u=this.renderer,a="",c=[],f={},p=0;function l(t){for(var r="";e&&e.tag!==t;)r=e.tag+e.text+r,f[e.tag]=!1,e=c.pop();return r}for(;i=s[p];){if(n="",p++,o(i))if(f[i])n=l(i),n=u[e.tag](e.text+n),f[i]=!1,e=c.pop();else{var h="";if("`"===i){var g=s.indexOf(i,p);-1!==g&&(a+=l(),h=s.slice(p,g).join(""),p=g)}e&&c.push(e),f[i]=!0,e={tag:i,text:h}}else if("\\"===(n=i)){var y=s[p];(o(y)||"\\"===y)&&(n=y,p++)}n&&(e?e.text+=n:a+=n,n="")}return a+l()},t.exports=i},function(t,e,r){var n=r(6),i=r(7);t.exports=function(){return n()||i()}},function(t,e){t.exports=function(){try{return"WebkitAppearance"in document.documentElement.style&&!/Edge/.test(navigator.userAgent)}catch(t){return!1}}},function(t,e){t.exports=function(){try{return/firefox\/(\d+)/i.test(navigator.userAgent)}catch(t){return!1}}},function(t,e,r){(function(e){function r(t,e){return"object"==typeof t&&t.self===t&&t||"object"==typeof e&&e.global===e&&e||this}t.exports=r.bind(this,self,e),t.exports.getGlobal=r}).call(this,r(9))},function(ob,pb){var qb;qb=function(){return this}();try{qb=qb||Function("return this")()||eval("this")}catch(ob){"object"==typeof window&&(qb=window)}ob.exports=qb}])},module.exports=e()}]); | ||
//# sourceMappingURL=priority-queue.bundle.js.map |
@@ -1,2 +0,2 @@ | ||
var PriorityQueue=function(e){var t={};function r(i){if(t[i])return t[i].exports;var o=t[i]={i:i,l:!1,exports:{}};return e[i].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,i){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:i})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var i=Object.create(null);if(r.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(i,o,function(t){return e[t]}.bind(null,o));return i},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=4)}([function(e,t,r){"use strict";function i(e){for(var r in e)t.hasOwnProperty(r)||(t[r]=e[r])}Object.defineProperty(t,"__esModule",{value:!0}),i(r(2)),i(r(1)),i(r(3))},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),function(e){e[e.LOW=0]="LOW",e[e.MEDIUM=5]="MEDIUM",e[e.HIGH=9]="HIGH"}(t.Priority||(t.Priority={}))},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});const i=r(1);t.Item=class{constructor(){this.fn=()=>{},this.priority=i.Priority.MEDIUM,this.reject=()=>{},this.resolve=()=>{},this.retry=1/0,this.timestamp=0}}},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});const i=r(2),o=r(1);t.PriorityQueue=class{constructor(e){this.config={comparator:(e,t)=>e.priority===t.priority?e.timestamp-t.timestamp:t.priority-e.priority,maxRetries:1/0,retryDelay:1e3},this.isPending=!1,this.queue=[],this.config=Object.assign(Object.assign({},this.config),e)}add(e,t=o.Priority.MEDIUM,r){return new Promise((o,s)=>{const n=new i.Item;n.fn=e,n.label=r,n.priority=t,n.reject=s,n.resolve=o,n.retry=Number(this.config.maxRetries)>=0?Number(this.config.maxRetries):n.retry,n.timestamp=Date.now()+this.size,this.queue.push(n),this.queue.sort(this.config.comparator),this.run()})}delete(e){this.queue=this.queue.filter(t=>t.label!==e)}deleteAll(){this.queue=[]}get all(){return this.queue}get first(){return this.queue[0]}get last(){return this.queue[this.queue.length-1]}get size(){return this.queue.length}resolveItems(){const e=this.first;e&&Promise.resolve(e.fn()).then(t=>({shouldContinue:!0,wrappedResolve:()=>e.resolve(t)})).catch(t=>e.retry>0?(e.retry-=1,setTimeout(()=>this.resolveItems(),this.config.retryDelay||1e3),{shouldContinue:!1}):(e.reject(t),{shouldContinue:!0})).then(({shouldContinue:e,wrappedResolve:t})=>{e&&(t&&t(),this.isPending=!1,this.queue.shift(),this.resolveItems())})}run(){!this.isPending&&this.first&&(this.isPending=!0,this.resolveItems())}toString(){return this.queue.map((e,t)=>`"${t}": ${e.fn.toString().replace(/(\r\n|\n|\r|\s+)/gm,"")}`).join("\r\n")}}},function(e,t,r){const i=r(5);i.keys().forEach(i)},function(e,t,r){var i={"./PriorityQueue.test.js":6};function o(e){var t=s(e);return r(t)}function s(e){if(!r.o(i,e)){var t=new Error("Cannot find module '"+e+"'");throw t.code="MODULE_NOT_FOUND",t}return i[e]}o.keys=function(){return Object.keys(i)},o.resolve=s,e.exports=o,o.id=5},function(e,t,r){"use strict";r.r(t);var i=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 i.PriorityQueue({retryDelay:3e3}).add(()=>new Promise((r,i)=>{t?i(new Error("Promise is locked.")):(r("Promise successfully executed."),e())}))}),it("supports limiting the amount of retries",e=>{const t=new i.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 i.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",async()=>{const e=new i.PriorityQueue;e.isPending=!0,e.add(()=>"ape"),e.add(()=>"cat",i.Priority.LOW),e.add(()=>"dog",i.Priority.HIGH),e.add(()=>"zebra");const t=await e.first.fn();expect(t).toBe("dog");const r=await e.last.fn();expect(r).toBe("cat")}),it("works with thunked Promises",async()=>{const e=new i.PriorityQueue,t=await Promise.all([e.add(()=>Promise.resolve("ape")),e.add(()=>Promise.resolve("cat")),e.add(()=>Promise.resolve("dog")),e.add(()=>Promise.resolve("zebra"))]);expect(t[0]).toBe("ape"),expect(t[1]).toBe("cat"),expect(t[2]).toBe("dog"),expect(t[3]).toBe("zebra")})}),describe('"run"',()=>{it("works with primitive values",e=>{const t=new i.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 i.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 i.PriorityQueue;t.add(()=>Promise.resolve("one").then(e=>(expect(e).toBe("one"),"one")),i.Priority.HIGH),t.add(()=>Promise.resolve("two").then(e=>(expect(e).toBe("two"),"two")),i.Priority.MEDIUM),t.add(()=>Promise.resolve("three").then(t=>{expect(t).toBe("three"),e()}),i.Priority.LOW)}),it("retries on error until the error gets resolved",e=>{let t=!0;const r=()=>new Promise(e=>{t=!1,e()}),o=new i.PriorityQueue;o.add(()=>new Promise((r,i)=>{t?i(new Error("Promise is locked.")):(r("Promise successfully executed."),e())})),setTimeout(()=>o.add(r,i.Priority.HIGH),200)}),it("works with error-catching Promises",e=>{const t=new i.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)),i.Priority.HIGH),t.add(()=>Promise.resolve("two").then(e=>expect(e).toBe("two")),i.Priority.MEDIUM),t.add(()=>Promise.resolve("three").then(()=>e()),i.Priority.LOW)}),it("executes a high priority element prior to other running elements ",e=>{const t=new i.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,i.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()}),o=new i.PriorityQueue({maxRetries:1/0,retryDelay:100});setTimeout(()=>o.add(r,i.Priority.HIGH),1e3),o.add(()=>new Promise((e,r)=>{t?r(new Error("Promise is locked.")):e("Promise successfully executed.")})).then(()=>{expect(o.size).toBe(0),e()})})}),describe('"comparator"',()=>{it("uses a descending priority order by default",async()=>{const e=new i.PriorityQueue;e.isPending=!0,e.add(()=>"ape",i.Priority.HIGH),e.add(()=>"cat"),e.add(()=>"dog"),e.add(()=>"zebra",i.Priority.LOW);const t=await e.last.fn();expect(t).toBe("zebra");const r=await e.first.fn();expect(r).toBe("ape")}),it("supports a custom comparator",async()=>{const e=new i.PriorityQueue({comparator:(e,t)=>e.priority-t.priority});e.isPending=!0,e.add(()=>"ape",i.Priority.HIGH),e.add(()=>"cat"),e.add(()=>"dog"),e.add(()=>"zebra",i.Priority.LOW);const t=await e.first.fn();expect(t).toBe("zebra");const r=await e.last.fn();expect(r).toBe("ape")}),it("continues after the maximum amount of retries",e=>{const t=new i.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)})})})}]); | ||
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,{enumerable:!0,get:o})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(r.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)r.d(o,i,function(t){return e[t]}.bind(null,i));return o},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=5)}([function(e,t,r){"use strict";function o(e){for(var r in e)t.hasOwnProperty(r)||(t[r]=e[r])}Object.defineProperty(t,"__esModule",{value:!0}),o(r(2)),o(r(1)),o(r(3))},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),function(e){e[e.LOW=0]="LOW",e[e.MEDIUM=5]="MEDIUM",e[e.HIGH=9]="HIGH"}(t.Priority||(t.Priority={}))},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});const o=r(1);t.Item=class{constructor(){this.fn=()=>{},this.priority=o.Priority.MEDIUM,this.reject=()=>{},this.resolve=()=>{},this.retry=1/0,this.timestamp=0}toString(){return`\n label=${this.label},\n priority=${this.priority},\n timestamp=${this.timestamp},\n retry=${this.retry},\n fn=${this.fn.toString().replace(/(\r\n|\n|\r|\s+)/gm," ")},`}}},function(e,t,r){"use strict";var o=this&&this.__awaiter||function(e,t,r,o){return new(r||(r=Promise))((function(i,n){function s(e){try{u(o.next(e))}catch(e){n(e)}}function a(e){try{u(o.throw(e))}catch(e){n(e)}}function u(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,a)}u((o=o.apply(e,t||[])).next())}))},i=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const n=i(r(4)),s=r(2),a=r(1);t.PriorityQueue=class{constructor(e){this.logger=n.default("@wireapp/priority-queue/PriorityQueue",{logger:console,markdown:!1}),this.config={comparator:(e,t)=>e.priority===t.priority?e.timestamp-t.timestamp:t.priority-e.priority,maxRetries:0,maxRetryDelay:Number.MAX_SAFE_INTEGER,retryDelay:1e3,retryGrowthFactor:1.3},this.isRunning=!1,this.queue=[],this.config=Object.assign(Object.assign({},this.config),e)}add(e,t=a.Priority.MEDIUM,r){return new Promise((o,i)=>{const n=new s.Item;n.fn=e,n.label=r,n.priority=t,n.reject=i,n.resolve=o,n.retry=0,n.timestamp=Date.now()+this.size,this.queue.push(n),this.queue.sort(this.config.comparator),this.isRunning||(this.isRunning=!0,this.processList())})}delete(e){this.queue=this.queue.filter(t=>t.label!==e)}deleteAll(){this.queue=[]}get all(){return this.queue}get first(){return this.queue[0]}get last(){return this.queue[this.queue.length-1]}get size(){return this.queue.length}processList(){return o(this,void 0,void 0,(function*(){const e=this.first;if(e)try{e.resolve(yield e.fn()),this.queue.shift(),this.processList()}catch(t){e.retry>=this.config.maxRetries?(this.queue.shift(),e.reject(t),this.processList()):(this.logger.log(`Retrying item "${e}"`),setTimeout(()=>this.processList(),this.getGrowingDelay(e.retry)),e.retry++)}else this.isRunning=!1}))}getGrowingDelay(e){const t=e<1?this.config.retryDelay:this.config.retryDelay*e*this.config.retryGrowthFactor;return Math.min(t,this.config.maxRetryDelay)}toString(){return this.queue.map((e,t)=>`"${t}": ${e.toString()}`).join("\r\n")}}},function(module,exports,__webpack_require__){var e;window,e=function(){return 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,{enumerable:!0,get:o})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(r.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)r.d(o,i,function(t){return e[t]}.bind(null,i));return o},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){var o,i=r(1)(),n=r(3),s=r(5),a=r(8)();i.prefixColors=["#F2777A","#F99157","#FFCC66","#99CC99","#66CCCC","#6699CC","#CC99CC"],i._setPrefixRegExps=function(){try{a.localStorage&&"string"==typeof a.localStorage.getItem("debug")&&(i._prefixRegExps=[],a.localStorage.getItem("debug").split(",").forEach((function(e){var t="enable";"-"===(e=e.trim())[0]&&(e=e.substr(1),t="disable");var r=i._prepareRegExpForPrefixSearch(e);i._prefixRegExps.push({type:t,regExp:r})})))}catch(e){}},i._getNextPrefixColor=(o=0,function(){return o+=1,i.prefixColors[o%i.prefixColors.length]}),i.prototype._getDecoratedPrefix=function(){var e=[];return s()?(e.push("%c"+this.opts.prefix+"%c "),e.push("color:"+this.opts.prefixColor+"; font-weight:bold;","")):e.push("["+this.opts.prefix+"] "),e},i.prototype._prepareOutput=function(e){var t,r=this._getDecoratedPrefix();return"string"==typeof e[0]?this.opts.markdown&&s()?(t=n.parse(e[0]),r[0]=r[0]+t.text,r=r.concat(t.styles)):r[0]=r[0]+e[0]:r.push(e[0]),1<e.length&&(r=r.concat(e.slice(1))),r},i._setPrefixRegExps(),e.exports=i},function(e,t,r){var o=r(2);e.exports=function(){function e(t,r){return this instanceof e?e._isPrefixAlreadyInUse(t)?e._getInstanceByPrefix(t):(this.opts=e._normalizeOpts(t,r),this.state=e._getInitialState(this.opts),e._decorateLoggerMethods(this),e._instances.push(this),this):new e(t,r)}return e.transports=[],e._instances=[],e._prefixRegExps=[],e._prepareRegExpForPrefixSearch=function(e){return new RegExp("^"+e.replace(/\*/g,".*?")+"$")},e._isPrefixAlreadyInUse=function(t){return e._instances.some((function(e){return e.opts.prefix===t}))},e._getInstanceByPrefix=function(t){return e._instances.filter((function(e){return e.opts.prefix===t}))[0]},e._normalizeOpts=function(t,r){if("string"!=typeof t)throw new TypeError("prefix must be a string");var o=void 0===(r=r||{}).markdown||Boolean(r.markdown),i=r.prefixColor||e._getNextPrefixColor();return{logger:r.logger||console,markdown:o,prefix:t,prefixColor:i}},e._getInitialState=function(t){return{isEnabled:e._getEnableState(t)}},e._getEnableState=function(t){var r=!1;return e._prefixRegExps.forEach((function(e){"enable"===e.type&&e.regExp.test(t.prefix)?r=!0:"disable"===e.type&&e.regExp.test(t.prefix)&&(r=!1)})),r},e._decorateLoggerMethods=function(t){var r=t.opts.logger,i=Object.keys(r).filter((function(e){return"function"==typeof r[e]}));0===i.length&&(i=["log","warn","error","info"]),i.forEach((function(i){t[i]=function(){var t=o(arguments),n=this.opts.prefix;if(e.transports.length){var s="["+n+"] "+t.filter((function(e){return"object"!=typeof e})).join(" ");e.transports.forEach(function(e){e({state:this.state,instance:n,level:i,args:t,msg:s})}.bind(this))}if(this.state.isEnabled){var a=this._prepareOutput(t,i);r[i].apply(r,a)}}}))},e}},function(e,t){e.exports=function(e){return Array.prototype.slice.call(e,0)}},function(e,t,r){var o=[];function i(e){return function(t){return o.push(e),o.push(""),"%c"+t+"%c"}}var n=new(r(4))({renderer:{"*":i("font-weight:bold;"),_:i("font-style:italic;"),"`":i("background-color:rgba(255,204,102, 0.1);color:#FFCC66;padding:2px 5px;border-radius:2px;")}});e.exports={parse:function(e){var t={text:n.parse(e),styles:[].concat(o)};return o.length=0,t}}},function(e,t){var r=/([_*`\\]|[^_*`\\]+)/g,o=/[_*`]/;function i(e){this.renderer=e.renderer}function n(e){return o.test(e)}i.prototype.parse=function(e){var t,o,i,s=e.match(r),a=this.renderer,u="",c=[],f={},p=0;function l(e){for(var r="";t&&t.tag!==e;)r=t.tag+t.text+r,f[t.tag]=!1,t=c.pop();return r}for(;i=s[p];){if(o="",p++,n(i))if(f[i])o=l(i),o=a[t.tag](t.text+o),f[i]=!1,t=c.pop();else{var d="";if("`"===i){var h=s.indexOf(i,p);-1!==h&&(u+=l(),d=s.slice(p,h).join(""),p=h)}t&&c.push(t),f[i]=!0,t={tag:i,text:d}}else if("\\"===(o=i)){var y=s[p];(n(y)||"\\"===y)&&(o=y,p++)}o&&(t?t.text+=o:u+=o,o="")}return u+l()},e.exports=i},function(e,t,r){var o=r(6),i=r(7);e.exports=function(){return o()||i()}},function(e,t){e.exports=function(){try{return"WebkitAppearance"in document.documentElement.style&&!/Edge/.test(navigator.userAgent)}catch(e){return!1}}},function(e,t){e.exports=function(){try{return/firefox\/(\d+)/i.test(navigator.userAgent)}catch(e){return!1}}},function(e,t,r){(function(t){function r(e,t){return"object"==typeof e&&e.self===e&&e||"object"==typeof t&&t.global===t&&t||this}e.exports=r.bind(this,self,t),e.exports.getGlobal=r}).call(this,r(9))},function(ob,pb){var qb;qb=function(){return this}();try{qb=qb||Function("return this")()||eval("this")}catch(ob){"object"==typeof window&&(qb=window)}ob.exports=qb}])},module.exports=e()},function(e,t,r){const o=r(6);o.keys().forEach(o)},function(e,t,r){var o={"./PriorityQueue.test.js":7};function i(e){var t=n(e);return r(t)}function n(e){if(!r.o(o,e)){var t=new Error("Cannot find module '"+e+"'");throw t.code="MODULE_NOT_FOUND",t}return o[e]}i.keys=function(){return Object.keys(o)},i.resolve=n,e.exports=i,i.id=6},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({maxRetries:1/0,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",async()=>{const e=jasmine.createSpy().and.returnValue(Promise.reject(new Error("Error"))),t=new o.PriorityQueue({maxRetries:1});try{await t.add(e),fail()}catch(r){expect(t.size).toBe(0),expect(e.calls.count()).toBe(2)}})}),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",async()=>{const e=new o.PriorityQueue;e.isPending=!0,e.add(()=>"ape"),e.add(()=>"cat",o.Priority.LOW),e.add(()=>"dog",o.Priority.HIGH),e.add(()=>"zebra");const t=await e.first.fn();expect(t).toBe("dog");const r=await e.last.fn();expect(r).toBe("cat")}),it("works with thunked Promises",async()=>{const e=new o.PriorityQueue,t=await Promise.all([e.add(()=>Promise.resolve("ape")),e.add(()=>Promise.resolve("cat")),e.add(()=>Promise.resolve("dog")),e.add(()=>Promise.resolve("zebra"))]);expect(t[0]).toBe("ape"),expect(t[1]).toBe("cat"),expect(t[2]).toBe("dog"),expect(t[3]).toBe("zebra")})}),describe('"run"',()=>{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({maxRetries:1/0});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",async()=>{const e=new o.PriorityQueue;e.isRunning=!0,e.add(()=>"ape",o.Priority.HIGH),e.add(()=>"cat"),e.add(()=>"dog"),e.add(()=>"zebra",o.Priority.LOW);const t=await e.last.fn();expect(t).toBe("zebra");const r=await e.first.fn();expect(r).toBe("ape")}),it("supports a custom comparator",async()=>{const e=new o.PriorityQueue({comparator:(e,t)=>e.priority-t.priority});e.isRunning=!0,e.add(()=>"ape",o.Priority.HIGH),e.add(()=>"cat"),e.add(()=>"dog"),e.add(()=>"zebra",o.Priority.LOW);const t=await e.first.fn();expect(t).toBe("zebra");const r=await e.last.fn();expect(r).toBe("ape")}),it("continues after the maximum amount of retries",e=>{const t=new o.PriorityQueue({maxRetries:3}),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)})})})}]); | ||
//# sourceMappingURL=priority-queue.test.bundle.js.map |
@@ -5,6 +5,7 @@ import { Config } from './Config'; | ||
export declare class PriorityQueue { | ||
private readonly logger; | ||
private readonly config; | ||
isPending: boolean; | ||
private isRunning; | ||
private queue; | ||
constructor(config?: Config); | ||
constructor(config?: Partial<Config>); | ||
add<T>(thunkedPromise: () => T, priority?: Priority, label?: string): Promise<T>; | ||
@@ -17,5 +18,5 @@ delete(label: string): void; | ||
readonly size: number; | ||
private resolveItems; | ||
private run; | ||
private processList; | ||
private getGrowingDelay; | ||
toString(): string; | ||
} |
@@ -20,3 +20,16 @@ "use strict"; | ||
*/ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const logdown_1 = __importDefault(require("logdown")); | ||
const Item_1 = require("./Item"); | ||
@@ -26,2 +39,6 @@ const Priority_1 = require("./Priority"); | ||
constructor(config) { | ||
this.logger = logdown_1.default('@wireapp/priority-queue/PriorityQueue', { | ||
logger: console, | ||
markdown: false, | ||
}); | ||
this.config = { | ||
@@ -34,6 +51,8 @@ comparator: (a, b) => { | ||
}, | ||
maxRetries: Infinity, | ||
maxRetries: 0, | ||
maxRetryDelay: Number.MAX_SAFE_INTEGER, | ||
retryDelay: 1000, | ||
retryGrowthFactor: 1.3, | ||
}; | ||
this.isPending = false; | ||
this.isRunning = false; | ||
this.queue = []; | ||
@@ -50,7 +69,11 @@ this.config = Object.assign(Object.assign({}, this.config), config); | ||
queueObject.resolve = resolve; | ||
queueObject.retry = Number(this.config.maxRetries) >= 0 ? Number(this.config.maxRetries) : queueObject.retry; | ||
queueObject.retry = 0; | ||
queueObject.timestamp = Date.now() + this.size; | ||
this.queue.push(queueObject); | ||
this.queue.sort(this.config.comparator); | ||
this.run(); | ||
if (!this.isRunning) { | ||
this.isRunning = true; | ||
/* tslint:disable-next-line:no-floating-promises */ | ||
this.processList(); | ||
} | ||
}); | ||
@@ -76,41 +99,34 @@ } | ||
} | ||
resolveItems() { | ||
const queueObject = this.first; | ||
if (!queueObject) { | ||
return; | ||
} | ||
/* tslint:disable:no-floating-promises */ | ||
Promise.resolve(queueObject.fn()) | ||
.then((result) => { | ||
return { shouldContinue: true, wrappedResolve: () => queueObject.resolve(result) }; | ||
}) | ||
.catch((error) => { | ||
if (queueObject.retry > 0) { | ||
queueObject.retry -= 1; | ||
// TODO: Implement configurable reconnection delay (and reconnection delay growth factor) | ||
setTimeout(() => this.resolveItems(), this.config.retryDelay || 1000); | ||
return { shouldContinue: false }; | ||
processList() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const queueObject = this.first; | ||
if (!queueObject) { | ||
this.isRunning = false; | ||
return; | ||
} | ||
else { | ||
queueObject.reject(error); | ||
return { shouldContinue: true }; | ||
try { | ||
queueObject.resolve(yield queueObject.fn()); | ||
this.queue.shift(); | ||
/* tslint:disable-next-line:no-floating-promises */ | ||
this.processList(); | ||
} | ||
}) | ||
.then(({ shouldContinue, wrappedResolve }) => { | ||
if (shouldContinue) { | ||
if (wrappedResolve) { | ||
wrappedResolve(); | ||
catch (error) { | ||
// TODO: print soomething when retrying execution | ||
if (queueObject.retry >= this.config.maxRetries) { | ||
this.queue.shift(); | ||
queueObject.reject(error); | ||
/* tslint:disable-next-line:no-floating-promises */ | ||
this.processList(); | ||
} | ||
this.isPending = false; | ||
this.queue.shift(); | ||
this.resolveItems(); | ||
else { | ||
this.logger.log(`Retrying item "${queueObject}"`); | ||
setTimeout(() => this.processList(), this.getGrowingDelay(queueObject.retry)); | ||
queueObject.retry++; | ||
} | ||
} | ||
}); | ||
/* tslint:enable:no-floating-promises */ | ||
} | ||
run() { | ||
if (!this.isPending && this.first) { | ||
this.isPending = true; | ||
this.resolveItems(); | ||
} | ||
getGrowingDelay(currentRetry) { | ||
const delay = currentRetry < 1 ? this.config.retryDelay : this.config.retryDelay * currentRetry * this.config.retryGrowthFactor; | ||
return Math.min(delay, this.config.maxRetryDelay); | ||
} | ||
@@ -120,3 +136,3 @@ toString() { | ||
.map((item, index) => { | ||
return `"${index}": ${item.fn.toString().replace(/(\r\n|\n|\r|\s+)/gm, '')}`; | ||
return `"${index}": ${item.toString()}`; | ||
}) | ||
@@ -123,0 +139,0 @@ .join('\r\n'); |
{ | ||
"dependencies": { | ||
"@types/node": "~10", | ||
"@types/node": "~12", | ||
"logdown": "3.2.8", | ||
"pure-uuid": "1.5.7" | ||
@@ -16,3 +17,3 @@ }, | ||
"rimraf": "3.0.0", | ||
"typescript": "3.6.3", | ||
"typescript": "3.6.4", | ||
"webpack": "4.41.0", | ||
@@ -40,4 +41,4 @@ "webpack-cli": "3.3.9" | ||
}, | ||
"version": "1.2.23", | ||
"gitHead": "5ca401ac3b62f61db6401eaee6c23d903b10d6ab" | ||
"version": "1.3.0", | ||
"gitHead": "1e82226f2f8b04d741a9f435c8f2ff2a3a5de957" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
210435
394
3
+ Addedlogdown@3.2.8
+ Added@types/node@12.20.55(transitive)
+ Addedansi-styles@3.2.1(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedlogdown@3.2.8(transitive)
+ Addedsupports-color@5.5.0(transitive)
- Removed@types/node@10.17.60(transitive)
Updated@types/node@~12