Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add peak to AsyncQueue #557

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Add peak to AsyncQueue #557

wants to merge 4 commits into from

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 peak proc that waits for the first item to be added to the queue would be good. The std/deques supports peakFirst and peakLast as well so this PR adds these to the AsyncQueue.

@@ -377,6 +444,11 @@ proc get*[T](aq: AsyncQueue[T]): Future[T] {.
## 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.

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