-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rework AsyncIter * Add tests for finishing iter on error * Improved error handling for and additional tests * Use new style of constructors * Handle future cancellation * Docs for constructors
- Loading branch information
Showing
14 changed files
with
694 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import pkg/questionable | ||
import pkg/questionable/results | ||
import pkg/chronos | ||
import pkg/chronicles | ||
import pkg/datastore/typedds | ||
|
||
import ../utils/asynciter | ||
|
||
type KeyVal[T] = tuple[key: Key, value: T] | ||
|
||
proc toAsyncIter*[T]( | ||
queryIter: QueryIter[T], | ||
finishOnErr: bool = true | ||
): Future[?!AsyncIter[?!QueryResponse[T]]] {.async.} = | ||
## Converts `QueryIter[T]` to `AsyncIter[?!QueryResponse[T]]` and automatically | ||
## runs dispose whenever `QueryIter` finishes or whenever an error occurs (only | ||
## if the flag finishOnErr is set to true) | ||
## | ||
|
||
if queryIter.finished: | ||
trace "Disposing iterator" | ||
if error =? (await queryIter.dispose()).errorOption: | ||
return failure(error) | ||
return success(AsyncIter[?!QueryResponse[T]].empty()) | ||
|
||
var errOccurred = false | ||
|
||
proc genNext: Future[?!QueryResponse[T]] {.async.} = | ||
let queryResOrErr = await queryIter.next() | ||
|
||
if queryResOrErr.isErr: | ||
errOccurred = true | ||
|
||
if queryIter.finished or (errOccurred and finishOnErr): | ||
trace "Disposing iterator" | ||
if error =? (await queryIter.dispose()).errorOption: | ||
return failure(error) | ||
|
||
return queryResOrErr | ||
|
||
proc isFinished(): bool = | ||
queryIter.finished or (errOccurred and finishOnErr) | ||
|
||
AsyncIter[?!QueryResponse[T]].new(genNext, isFinished).success | ||
|
||
proc filterSuccess*[T]( | ||
iter: AsyncIter[?!QueryResponse[T]] | ||
): Future[AsyncIter[tuple[key: Key, value: T]]] {.async.} = | ||
## Filters out any items that are not success | ||
|
||
proc mapping(resOrErr: ?!QueryResponse[T]): Future[?KeyVal[T]] {.async.} = | ||
without res =? resOrErr, error: | ||
error "Error occurred when getting QueryResponse", msg = error.msg | ||
return KeyVal[T].none | ||
|
||
without key =? res.key: | ||
warn "No key for a QueryResponse" | ||
return KeyVal[T].none | ||
|
||
without value =? res.value, error: | ||
error "Error occurred when getting a value from QueryResponse", msg = error.msg | ||
return KeyVal[T].none | ||
|
||
(key: key, value: value).some | ||
|
||
await mapFilter[?!QueryResponse[T], KeyVal[T]](iter, mapping) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.