Skip to content

Commit

Permalink
Added maximum promise queue lenght
Browse files Browse the repository at this point in the history
  • Loading branch information
crycode-de committed Dec 19, 2023
1 parent 10a2718 commit 3b91474
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/pcf857x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export abstract class PCF857x<PinNumber extends PCF8574.PinNumber | PCF8575.PinN
private _currentlyPolling: boolean = false;

/** PromiseQueue to handle requested polls in order. */
private _pollQueue: PromiseQueue = new PromiseQueue();
private _pollQueue: PromiseQueue = new PromiseQueue(1);

/** Pin number of GPIO to detect interrupts, or null by default. */
private _gpioPin: number | null = null;
Expand Down Expand Up @@ -315,6 +315,7 @@ export abstract class PCF857x<PinNumber extends PCF8574.PinNumber | PCF8575.PinN
* If a change on an input is detected, an "input" Event will be emitted with a data object containing the "pin" and the new "value".
* This have to be called frequently enough if you don't use a GPIO for interrupt detection.
* If you poll again before the last poll was completed, the new poll will be queued up the be executed after the current poll.
* If you poll again while also a poll is queued, this will be rejected.
* @return {Promise}
*/
public doPoll (): Promise<void> {
Expand Down
24 changes: 23 additions & 1 deletion src/promise-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,36 @@ export class PromiseQueue {
*/
private working: boolean = false;

/**
* The maximum length of the queue.
*/
private maxQueueLength: number | undefined = undefined;

/**
* Create a Promise Queue.
* @param maxQueueLength Optional maximum allowed length of the queue.
*/
constructor (maxQueueLength?: number) {
if (typeof maxQueueLength === 'number') {
this.maxQueueLength = maxQueueLength;
}
}

/**
* Enqueue a Promise.
* This adds the given Promise to the queue. If the queue was empty the Promise
* will be started immediately.
* If the PromiseQueue was initialized with a maximum length and the Promise to
* enqueue would exceed the limit, a Promise rejection will be returned instant.
* @param promise Function which returns the Promise.
* @returns A Promise which will be resolves (or rejected) if the queued promise is done.
* @returns A Promise which will be resolved (or rejected) if the queued promise is done. Or an instant Promise rejection if the maximum allowed queue length is exceeded.
*/
public enqueue<T = void> (promise: () => Promise<T>): Promise<T> {
// check the maximum queue length
if (this.maxQueueLength !== undefined && this.queue.length >= this.maxQueueLength) {
return Promise.reject('Maximum queue length exceeded');
}

return new Promise((resolve, reject) => {
this.queue.push({
promise,
Expand Down

0 comments on commit 3b91474

Please sign in to comment.