Skip to content

Add peek to AsyncQueue#557

Open
bhartnett wants to merge 4 commits into
masterfrom
add-peak-to-async-queue
Open

Add peek to AsyncQueue#557
bhartnett wants to merge 4 commits into
masterfrom
add-peak-to-async-queue

Conversation

@bhartnett
Copy link
Copy Markdown

@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.

Comment thread chronos/asyncsync.nim Outdated
## Alias of ``popFirst()``.
aq.popFirst()

proc peak*[T](aq: AsyncQueue[T]): Future[T] {.
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread tests/testsync.nim
q.putNoWait(1)
q.putNoWait(2)

check:
Copy link
Copy Markdown
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
Copy Markdown
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