Skip to content

Conversation

@bhartnett
Copy link

@bhartnett bhartnett commented Oct 2, 2024

In some of my code I found the need to do something like this:

# wait for the first block data to be put on the queue
# so that we can access the first block once available
while blockDataQueue.empty():
  await sleepAsync(100.milliseconds)
# peek but don't remove it so that it can be processed later
let firstBlock = blockDataQueue[0]

Having an async peek proc that waits for the first item to be added to the queue would be good. The std/deques supports peekFirst and peekLast as well so this PR adds these to the AsyncQueue.

## Alias of ``popFirst()``.
aq.popFirst()

proc peak*[T](aq: AsyncQueue[T]): Future[T] {.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
proc peak*[T](aq: AsyncQueue[T]): Future[T] {.
proc peek*[T](aq: AsyncQueue[T]): Future[T] {.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, will fix the spelling shortly.

q.putNoWait(1)
q.putNoWait(2)

check:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there needs to be a test here that ensures that the following also works:

let a = q.peekFirst()
let b = q.popFirst()

both in that order and in the reverse order - where peek-after-pop should probably not trigger

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added more tests as suggested.

@bhartnett bhartnett requested a review from arnetheduck October 3, 2024 14:42
@bhartnett bhartnett changed the title Add peak to AsyncQueue Add peek to AsyncQueue Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants