Remove immediate FutureStream procs and make 'put' awaitable.
This commit is contained in:
parent
ddd3d3f44a
commit
2f502e2a9e
2 changed files with 10 additions and 20 deletions
|
|
@ -249,30 +249,20 @@ proc failed*(future: FutureBase): bool =
|
||||||
## Determines whether ``future`` completed with an error.
|
## Determines whether ``future`` completed with an error.
|
||||||
return future.error != nil
|
return future.error != nil
|
||||||
|
|
||||||
proc take*[T](future: FutureStream[T]): T {.raises: [IndexError].} =
|
proc put*[T](future: FutureStream[T], value: T): Future[void] =
|
||||||
## Retrieves the oldest value stored inside the stream. If the stream
|
|
||||||
## contains no data then this function will fail with a ``IndexError``
|
|
||||||
## exception.
|
|
||||||
##
|
|
||||||
## This function will remove the data that was returned from the underlying
|
|
||||||
## ``FutureStream``.
|
|
||||||
return future.queue.dequeue()
|
|
||||||
|
|
||||||
proc put*[T](future: FutureStream[T], value: T) =
|
|
||||||
## Writes the specified value inside the specified future stream.
|
## Writes the specified value inside the specified future stream.
|
||||||
##
|
##
|
||||||
## This will raise ``ValueError`` if ``future`` is finished.
|
## This will raise ``ValueError`` if ``future`` is finished.
|
||||||
|
result = newFuture[void]("FutureStream.put")
|
||||||
if future.finished:
|
if future.finished:
|
||||||
let msg = "FutureStream is finished and so no longer accepts new data."
|
let msg = "FutureStream is finished and so no longer accepts new data."
|
||||||
raise newException(ValueError, msg)
|
result.fail(newException(ValueError, msg))
|
||||||
|
# TODO: Buffering.
|
||||||
future.queue.enqueue(value)
|
future.queue.enqueue(value)
|
||||||
if not future.cb.isNil: future.cb()
|
if not future.cb.isNil: future.cb()
|
||||||
|
result.complete()
|
||||||
|
|
||||||
proc peek*[T](future: FutureStream[T]): T =
|
proc take*[T](future: FutureStream[T]): Future[(bool, T)] =
|
||||||
## Returns the oldest value stored inside the specified future stream.
|
|
||||||
return future.queue.front()
|
|
||||||
|
|
||||||
proc takeAsync*[T](future: FutureStream[T]): Future[(bool, T)] =
|
|
||||||
## Returns a future that will complete when the ``FutureStream`` has data
|
## Returns a future that will complete when the ``FutureStream`` has data
|
||||||
## placed into it. The future will be completed with the oldest
|
## placed into it. The future will be completed with the oldest
|
||||||
## value stored inside the stream. The return value will also determine
|
## value stored inside the stream. The return value will also determine
|
||||||
|
|
@ -281,7 +271,7 @@ proc takeAsync*[T](future: FutureStream[T]): Future[(bool, T)] =
|
||||||
##
|
##
|
||||||
## This function will remove the data that was returned from the underlying
|
## This function will remove the data that was returned from the underlying
|
||||||
## ``FutureStream``.
|
## ``FutureStream``.
|
||||||
var resFut = newFuture[(bool, T)]("FutureStream.takeAsync")
|
var resFut = newFuture[(bool, T)]("FutureStream.take")
|
||||||
let savedCb = future.cb
|
let savedCb = future.cb
|
||||||
future.callback =
|
future.callback =
|
||||||
proc (fs: FutureStream[T]) =
|
proc (fs: FutureStream[T]) =
|
||||||
|
|
@ -295,7 +285,7 @@ proc takeAsync*[T](future: FutureStream[T]): Future[(bool, T)] =
|
||||||
res[0] = false
|
res[0] = false
|
||||||
else:
|
else:
|
||||||
res[0] = true
|
res[0] = true
|
||||||
res[1] = fs.take()
|
res[1] = fs.queue.dequeue()
|
||||||
|
|
||||||
if not resFut.finished:
|
if not resFut.finished:
|
||||||
resFut.complete(res)
|
resFut.complete(res)
|
||||||
|
|
|
||||||
|
|
@ -19,13 +19,13 @@ var fs = newFutureStream[int]()
|
||||||
proc alpha() {.async.} =
|
proc alpha() {.async.} =
|
||||||
for i in 0 .. 5:
|
for i in 0 .. 5:
|
||||||
await sleepAsync(1000)
|
await sleepAsync(1000)
|
||||||
fs.put(i)
|
await fs.put(i)
|
||||||
|
|
||||||
fs.complete()
|
fs.complete()
|
||||||
|
|
||||||
proc beta() {.async.} =
|
proc beta() {.async.} =
|
||||||
while not fs.finished:
|
while not fs.finished:
|
||||||
let (hasValue, value) = await fs.takeAsync()
|
let (hasValue, value) = await fs.take()
|
||||||
if hasValue:
|
if hasValue:
|
||||||
echo(value)
|
echo(value)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue