-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
... instead of rejecting them if an other poll is currently active Added method `isPolling()` to get the current polling state May fix #59
- Loading branch information
1 parent
98164bf
commit 05c2e0c
Showing
5 changed files
with
117 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Interface to describe a queued promise in a `PromiseQueue`. | ||
*/ | ||
interface QueuedPromise<T = any> { | ||
promise: () => Promise<T>; | ||
resolve: (value: T) => void; | ||
reject: (reason?: any) => void; | ||
} | ||
|
||
/** | ||
* A simple Promise Queue to allow the execution of some tasks in the correct order. | ||
* | ||
* (c) Peter Müller <[email protected]> | ||
*/ | ||
export class PromiseQueue { | ||
|
||
/** | ||
* Queued Promises. | ||
*/ | ||
private queue: QueuedPromise[] = []; | ||
|
||
/** | ||
* Indicator that we are working on a Promise. | ||
*/ | ||
private working: boolean = false; | ||
|
||
/** | ||
* Enqueue a Promise. | ||
* This adds the given Promise to the queue. If the queue was empty the Promise | ||
* will be started immediately. | ||
* @param promise Function which returns the Promise. | ||
* @returns A Promise which will be resolves (or rejected) if the queued promise is done. | ||
*/ | ||
public enqueue<T = void> (promise: () => Promise<T>): Promise<T> { | ||
return new Promise((resolve, reject) => { | ||
this.queue.push({ | ||
promise, | ||
resolve, | ||
reject, | ||
}); | ||
this.dequeue(); | ||
}); | ||
} | ||
|
||
/** | ||
* Returns if the queue is empty and no more Promises are queued. | ||
* @returns `true` if a Promise is active. | ||
*/ | ||
public isEmpty(): boolean { | ||
return !this.working && this.queue.length == 0; | ||
} | ||
|
||
/** | ||
* Get the first Promise of the queue and start it if there is no other | ||
* Promise currently active. | ||
* @returns `true` if Promise from the queue is started, `false` there is already an other active Promise or the queue is empty. | ||
*/ | ||
private dequeue (): boolean { | ||
if (this.working) { | ||
return false; | ||
} | ||
|
||
const item = this.queue.shift(); | ||
if (!item) { | ||
return false; | ||
} | ||
|
||
try { | ||
this.working = true; | ||
item.promise() | ||
.then((value) => { | ||
item.resolve(value); | ||
}) | ||
.catch((err) => { | ||
item.reject(err); | ||
}) | ||
.finally(() => { | ||
this.working = false; | ||
this.dequeue() | ||
}); | ||
|
||
} catch (err) { | ||
item.reject(err); | ||
this.working = false; | ||
this.dequeue(); | ||
} | ||
|
||
return true; | ||
} | ||
} |