Various fixes to FutureStreams based on PR feedback.
This commit is contained in:
parent
912d95a6ea
commit
f9cce32097
7 changed files with 72 additions and 48 deletions
|
|
@ -476,13 +476,16 @@ proc close*(f: AsyncFile) =
|
|||
if close(f.fd.cint) == -1:
|
||||
raiseOSError(osLastError())
|
||||
|
||||
proc writeFromStream(f: AsyncFile, fut: FutureStream[string]) {.async.} =
|
||||
proc writeFromStream*(f: AsyncFile, fs: FutureStream[string]) {.async.} =
|
||||
## Reads data from the specified future stream until it is completed.
|
||||
## The data which is read is written to the file immediately and
|
||||
## freed from memory.
|
||||
##
|
||||
## This procedure is perfect for saving streamed data to a file without
|
||||
## wasting memory.
|
||||
while true:
|
||||
let (hasValue, value) = await fut.take()
|
||||
let (hasValue, value) = await fs.read()
|
||||
if hasValue:
|
||||
await f.write(value)
|
||||
else:
|
||||
break
|
||||
|
||||
proc setWriteStream*(f: AsyncFile; fut: FutureStream[string]) {.async.} =
|
||||
await writeFromStream(f, fut)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue