Skip to content

Commit

Permalink
fix: MemoryFifo return null when empty and timeout 0
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Apr 18, 2024
1 parent 2422891 commit a93d2f3
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions yarn-project/foundation/src/fifo/memory_fifo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ export class MemoryFifo<T> {
}

/**
* Returns next item within the queue, or blocks until and item has been put into the queue.
* If given a timeout, the promise will reject if no item is received after `timeout` seconds.
* Returns next item within the queue, or blocks until an item has been put into the queue.
*
* If given a timeout, the promise will reject if no item is received after `timeoutSec` seconds.
* If the timeout is undefined (default), this call will block until an item is available or the queue is closed.
* If the timeout is 0 and there are no items available then the queue will immediately reject with a TimeoutError.
*
* If the queue is flushing, `null` is returned.
* @param timeout - The timeout in seconds.
* @param timeoutSec - The timeout in seconds.
* @returns A result promise.
*/
public get(timeout?: number): Promise<T | null> {
public get(timeoutSec?: number): Promise<T | null> {
if (this.items.length) {
return Promise.resolve(this.items.shift()!);
}
Expand All @@ -39,18 +43,24 @@ export class MemoryFifo<T> {
return Promise.resolve(null);
}

// if the caller doesn't want to wait for an item to be available
// immediately reject with a Timeout error
if (timeoutSec === 0) {
return Promise.reject(new TimeoutError('Timeout getting item from queue.'));
}

return new Promise<T | null>((resolve, reject) => {
this.waiting.push(resolve);

if (timeout) {
if (timeoutSec) {
setTimeout(() => {
const index = this.waiting.findIndex(r => r === resolve);
if (index > -1) {
this.waiting.splice(index, 1);
const err = new TimeoutError('Timeout getting item from queue.');
reject(err);
}
}, timeout * 1000);
}, timeoutSec * 1000);
}
});
}
Expand Down

0 comments on commit a93d2f3

Please sign in to comment.