diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2020-09-18 11:22:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-18 11:22:34 +0200 |
commit | 2671efab78e0c1a56d5f527b6a25f658575ca9a0 (patch) | |
tree | 87fa74f7eba5b3ab91d8b44371b34a34231ec5ea /lib/pure/asyncstreams.nim | |
parent | e56d50d747451ea222617902ff70f31b4bb3123c (diff) | |
download | Nim-2671efab78e0c1a56d5f527b6a25f658575ca9a0.tar.gz |
async: minor refactorings (#15354)
Diffstat (limited to 'lib/pure/asyncstreams.nim')
-rw-r--r-- | lib/pure/asyncstreams.nim | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/lib/pure/asyncstreams.nim b/lib/pure/asyncstreams.nim index 393262c4f..7ffde9c10 100644 --- a/lib/pure/asyncstreams.nim +++ b/lib/pure/asyncstreams.nim @@ -54,7 +54,8 @@ proc `callback=`*[T](future: FutureStream[T], ## ## If the future stream already has data or is finished then ``cb`` will be ## called immediately. - future.cb = proc () = cb(future) + proc named() = cb(future) + future.cb = named if future.queue.len > 0 or future.finished: callSoon(future.cb) @@ -90,27 +91,26 @@ proc read*[T](future: FutureStream[T]): owned(Future[(bool, T)]) = ## ``FutureStream``. var resFut = newFuture[(bool, T)]("FutureStream.take") let savedCb = future.cb - var newCb = - proc (fs: FutureStream[T]) = - # Exit early if `resFut` is already complete. (See #8994). - if resFut.finished: return - - # We don't want this callback called again. - #future.cb = nil - - # The return value depends on whether the FutureStream has finished. - var res: (bool, T) - if finished(fs): - # Remember, this callback is called when the FutureStream is completed. - res[0] = false - else: - res[0] = true - res[1] = fs.queue.popFirst() - - resFut.complete(res) - - # If the saved callback isn't nil then let's call it. - if not savedCb.isNil: savedCb() + proc newCb(fs: FutureStream[T]) = + # Exit early if `resFut` is already complete. (See #8994). + if resFut.finished: return + + # We don't want this callback called again. + #future.cb = nil + + # The return value depends on whether the FutureStream has finished. + var res: (bool, T) + if finished(fs): + # Remember, this callback is called when the FutureStream is completed. + res[0] = false + else: + res[0] = true + res[1] = fs.queue.popFirst() + + resFut.complete(res) + + # If the saved callback isn't nil then let's call it. + if not savedCb.isNil: savedCb() if future.queue.len > 0 or future.finished: newCb(future) |