@wireapp/priority-queue
Advanced tools
Comparing version 1.2.1 to 1.2.2
@@ -6,2 +6,10 @@ # Change Log | ||
## [1.2.2](https://github.com/wireapp/wire-web-packages/tree/master/packages/priority-queue/compare/@wireapp/priority-queue@1.2.1...@wireapp/priority-queue@1.2.2) (2019-07-10) | ||
**Note:** Version bump only for package @wireapp/priority-queue | ||
## [1.2.1](https://github.com/wireapp/wire-web-packages/tree/master/packages/priority-queue/compare/@wireapp/priority-queue@1.2.0...@wireapp/priority-queue@1.2.1) (2019-07-04) | ||
@@ -8,0 +16,0 @@ |
export declare class Item { | ||
/** original business logic */ | ||
fn: Function; | ||
label: string | undefined; | ||
priority: number; | ||
/** wrapped `reject` of `fn` */ | ||
reject: Function; | ||
/** wrapped `resolve` of `fn` */ | ||
resolve: Function; | ||
/** number of remaining retries for rejecting Promises */ | ||
@@ -8,8 +14,2 @@ retry: number; | ||
timestamp: number; | ||
/** original business logic */ | ||
fn: Function; | ||
/** wrapped `reject` of `fn` */ | ||
reject: Function; | ||
/** wrapped `resolve` of `fn` */ | ||
resolve: Function; | ||
} |
@@ -24,9 +24,5 @@ "use strict"; | ||
constructor() { | ||
this.priority = Priority_1.Priority.MEDIUM; | ||
/** number of remaining retries for rejecting Promises */ | ||
this.retry = Infinity; | ||
/** time when the item has been added to the queue */ | ||
this.timestamp = 0; | ||
/** original business logic */ | ||
this.fn = () => { }; | ||
this.priority = Priority_1.Priority.MEDIUM; | ||
/** wrapped `reject` of `fn` */ | ||
@@ -36,2 +32,6 @@ this.reject = () => { }; | ||
this.resolve = () => { }; | ||
/** number of remaining retries for rejecting Promises */ | ||
this.retry = Infinity; | ||
/** time when the item has been added to the queue */ | ||
this.timestamp = 0; | ||
} | ||
@@ -38,0 +38,0 @@ } |
@@ -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.priority=i.Priority.MEDIUM,this.retry=1/0,this.timestamp=0,this.fn=()=>{},this.reject=()=>{},this.resolve=()=>{}}}},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.isPending=!1,this.config={comparator:(e,t)=>e.priority===t.priority?e.timestamp-t.timestamp:t.priority-e.priority,maxRetries:1/0,retryDelay:1e3},this.queue=[],this.config=Object.assign({},this.config,e)}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}add(e,t=n.Priority.MEDIUM,r){return"function"!=typeof e&&(e=()=>e),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=[]}toString(){return this.queue.map((e,t)=>`"${t}": ${e.fn.toString().replace(/(\r\n|\n|\r|\s+)/gm,"")}`).join("\r\n")}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())}}}]); | ||
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({},this.config,e)}add(e,t=n.Priority.MEDIUM,r){return"function"!=typeof e&&(e=()=>e),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")}}}]); | ||
//# 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.priority=i.Priority.MEDIUM,this.retry=1/0,this.timestamp=0,this.fn=()=>{},this.reject=()=>{},this.resolve=()=>{}}}},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.isPending=!1,this.config={comparator:(e,t)=>e.priority===t.priority?e.timestamp-t.timestamp:t.priority-e.priority,maxRetries:1/0,retryDelay:1e3},this.queue=[],this.config=Object.assign({},this.config,e)}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}add(e,t=o.Priority.MEDIUM,r){return"function"!=typeof e&&(e=()=>e),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=[]}toString(){return this.queue.map((e,t)=>`"${t}": ${e.fn.toString().replace(/(\r\n|\n|\r|\s+)/gm,"")}`).join("\r\n")}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())}}},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(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({},this.config,e)}add(e,t=o.Priority.MEDIUM,r){return"function"!=typeof e&&(e=()=>e),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)})})})}]); | ||
//# sourceMappingURL=priority-queue.test.bundle.js.map |
@@ -5,8 +5,4 @@ import { Config } from './Config'; | ||
export declare class PriorityQueue { | ||
readonly all: Item[]; | ||
readonly first: Item; | ||
readonly last: Item; | ||
readonly size: number; | ||
private readonly config; | ||
isPending: boolean; | ||
private readonly config; | ||
private queue; | ||
@@ -17,5 +13,9 @@ constructor(config?: Config); | ||
deleteAll(): void; | ||
toString(): string; | ||
readonly all: Item[]; | ||
readonly first: Item; | ||
readonly last: Item; | ||
readonly size: number; | ||
private resolveItems; | ||
private run; | ||
toString(): string; | ||
} |
@@ -25,3 +25,2 @@ "use strict"; | ||
constructor(config) { | ||
this.isPending = false; | ||
this.config = { | ||
@@ -37,17 +36,6 @@ comparator: (a, b) => { | ||
}; | ||
this.isPending = false; | ||
this.queue = []; | ||
this.config = Object.assign({}, this.config, config); | ||
} | ||
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; | ||
} | ||
add(thunkedPromise, priority = Priority_1.Priority.MEDIUM, label) { | ||
@@ -77,9 +65,14 @@ if (typeof thunkedPromise !== 'function') { | ||
} | ||
toString() { | ||
return this.queue | ||
.map((item, index) => { | ||
return `"${index}": ${item.fn.toString().replace(/(\r\n|\n|\r|\s+)/gm, '')}`; | ||
}) | ||
.join('\r\n'); | ||
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() { | ||
@@ -127,4 +120,11 @@ const queueObject = this.first; | ||
} | ||
toString() { | ||
return this.queue | ||
.map((item, index) => { | ||
return `"${index}": ${item.fn.toString().replace(/(\r\n|\n|\r|\s+)/gm, '')}`; | ||
}) | ||
.join('\r\n'); | ||
} | ||
} | ||
exports.PriorityQueue = PriorityQueue; | ||
//# sourceMappingURL=PriorityQueue.js.map |
@@ -16,4 +16,4 @@ { | ||
"rimraf": "2.6.3", | ||
"typescript": "3.5.2", | ||
"webpack": "4.35.2", | ||
"typescript": "3.5.3", | ||
"webpack": "4.35.3", | ||
"webpack-cli": "3.3.5" | ||
@@ -40,4 +40,4 @@ }, | ||
}, | ||
"version": "1.2.1", | ||
"gitHead": "414786cf5c1a57a04fbed7a7f086c7617855e6e9" | ||
"version": "1.2.2", | ||
"gitHead": "43a1b9d8a9503e60b4e9452624e9e2b19741d590" | ||
} |
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
159453