fetch item prices - throttle end points with promise queue

This commit is contained in:
2018-08-28 21:45:46 -05:00
parent 97acc0d4fa
commit 7ca8606284
17 changed files with 241 additions and 64 deletions

1
app/util/index.ts Normal file
View File

@@ -0,0 +1 @@
export * from './promise-queue';

43
app/util/promise-queue.ts Normal file
View File

@@ -0,0 +1,43 @@
/**
* Allow promises to be queued up and executed in order.
*/
export class PromiseQueue {
private queue: any[] = [];
private locked: boolean = false;
/** Add new promise to queue */
add(p: () => Promise<any>, delay?: number) {
this._add(p, false, delay);
}
/** Add new promise to the from of the queue */
addToBeginning(p: () => Promise<any>, delay?: number) {
this._add(p, true, delay);
}
/** Clear the queue and reset state. */
clearQueue() {
this.queue = [];
}
private delay = (ms: number) => () => new Promise(resolve => setTimeout(resolve, ms));
_add = (p: () => Promise<any>, beginning: boolean = false, delay?: number) => {
const newPromise = delay ? [p, this.delay(delay)] : [p];
beginning ? this.queue.unshift(newPromise) : this.queue.push(newPromise);
if (!this.locked) {
this.next();
}
};
private next() {
this.locked = true;
const p = this.queue.shift();
const finished = () => {
this.queue.length > 0 ? this.next() : (this.locked = false);
};
Promise.all(p.map((v: any) => v()))
.then(finished)
.catch(finished);
}
}