asyncstreams.nim 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import asyncfutures
  2. import deques
  3. type
  4. FutureStream*[T] = ref object ## Special future that acts as
  5. ## a queue. Its API is still
  6. ## experimental and so is
  7. ## subject to change.
  8. queue: Deque[T]
  9. finished: bool
  10. cb: proc () {.closure, gcsafe.}
  11. proc newFutureStream*[T](fromProc = "unspecified"): FutureStream[T] =
  12. ## Create a new ``FutureStream``. This future's callback is activated when
  13. ## two events occur:
  14. ##
  15. ## * New data is written into the future stream.
  16. ## * The future stream is completed (this means that no more data will be
  17. ## written).
  18. ##
  19. ## Specifying ``fromProc``, which is a string specifying the name of the proc
  20. ## that this future belongs to, is a good habit as it helps with debugging.
  21. ##
  22. ## **Note:** The API of FutureStream is still new and so has a higher
  23. ## likelihood of changing in the future.
  24. result = FutureStream[T](finished: false, cb: nil)
  25. result.queue = initDeque[T]()
  26. proc complete*[T](future: FutureStream[T]) =
  27. ## Completes a ``FutureStream`` signalling the end of data.
  28. future.finished = true
  29. if not future.cb.isNil:
  30. future.cb()
  31. proc `callback=`*[T](future: FutureStream[T],
  32. cb: proc (future: FutureStream[T]) {.closure,gcsafe.}) =
  33. ## Sets the callback proc to be called when data was placed inside the
  34. ## future stream.
  35. ##
  36. ## The callback is also called when the future is completed. So you should
  37. ## use ``finished`` to check whether data is available.
  38. ##
  39. ## If the future stream already has data or is finished then ``cb`` will be
  40. ## called immediately.
  41. future.cb = proc () = cb(future)
  42. if future.queue.len > 0 or future.finished:
  43. callSoon(future.cb)
  44. proc finished*[T](future: FutureStream[T]): bool =
  45. ## Check if a ``FutureStream`` is finished. ``true`` value means that
  46. ## no more data will be placed inside the stream _and_ that there is
  47. ## no data waiting to be retrieved.
  48. result = future.finished and future.queue.len == 0
  49. proc write*[T](future: FutureStream[T], value: T): Future[void] =
  50. ## Writes the specified value inside the specified future stream.
  51. ##
  52. ## This will raise ``ValueError`` if ``future`` is finished.
  53. result = newFuture[void]("FutureStream.put")
  54. if future.finished:
  55. let msg = "FutureStream is finished and so no longer accepts new data."
  56. result.fail(newException(ValueError, msg))
  57. return
  58. # TODO: Implement limiting of the streams storage to prevent it growing
  59. # infinitely when no reads are occuring.
  60. future.queue.addLast(value)
  61. if not future.cb.isNil: future.cb()
  62. result.complete()
  63. proc read*[T](future: FutureStream[T]): Future[(bool, T)] =
  64. ## Returns a future that will complete when the ``FutureStream`` has data
  65. ## placed into it. The future will be completed with the oldest
  66. ## value stored inside the stream. The return value will also determine
  67. ## whether data was retrieved, ``false`` means that the future stream was
  68. ## completed and no data was retrieved.
  69. ##
  70. ## This function will remove the data that was returned from the underlying
  71. ## ``FutureStream``.
  72. var resFut = newFuture[(bool, T)]("FutureStream.take")
  73. let savedCb = future.cb
  74. future.callback =
  75. proc (fs: FutureStream[T]) =
  76. # Exit early if `resFut` is already complete. (See #8994).
  77. if resFut.finished: return
  78. # We don't want this callback called again.
  79. future.cb = nil
  80. # The return value depends on whether the FutureStream has finished.
  81. var res: (bool, T)
  82. if finished(fs):
  83. # Remember, this callback is called when the FutureStream is completed.
  84. res[0] = false
  85. else:
  86. res[0] = true
  87. res[1] = fs.queue.popFirst()
  88. resFut.complete(res)
  89. # If the saved callback isn't nil then let's call it.
  90. if not savedCb.isNil: savedCb()
  91. return resFut
  92. proc len*[T](future: FutureStream[T]): int =
  93. ## Returns the amount of data pieces inside the stream.
  94. future.queue.len