event_stream.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include <rpl/producer.h>
  9. #include <rpl/range.h>
  10. #include <rpl/then.h>
  11. #include <rpl/range.h>
  12. #include <algorithm>
  13. #include <optional>
  14. #include "base/assertion.h"
  15. #include "base/index_based_iterator.h"
  16. namespace rpl {
  17. // Currently not thread-safe :(
  18. template <typename Value = empty_value, typename Error = no_error>
  19. class event_stream {
  20. public:
  21. event_stream() noexcept = default;
  22. event_stream(event_stream &&other);
  23. event_stream &operator=(event_stream &&other);
  24. template <typename OtherValue>
  25. void fire_forward(OtherValue &&value) const;
  26. void fire(Value &&value) const {
  27. return fire_forward(std::move(value));
  28. }
  29. void fire_copy(const Value &value) const {
  30. return fire_forward(value);
  31. }
  32. template <typename OtherError>
  33. void fire_error_forward(OtherError &&error) const;
  34. void fire_error(Error &&error) const {
  35. return fire_error_forward(std::move(error));
  36. }
  37. void fire_error_copy(const Error &error) const {
  38. return fire_error_forward(error);
  39. }
  40. void fire_done() const;
  41. #if defined _MSC_VER && _MSC_VER >= 1914 && _MSC_VER < 1916
  42. producer<Value, Error> events() const {
  43. #else // _MSC_VER >= 1914 && _MSC_VER < 1916
  44. auto events() const {
  45. #endif // _MSC_VER >= 1914 && _MSC_VER < 1916
  46. return make_producer<Value, Error>([weak = make_weak()](
  47. const auto &consumer) {
  48. if (const auto strong = weak.lock()) {
  49. auto result = [weak, consumer] {
  50. if (const auto strong = weak.lock()) {
  51. const auto it = std::find(
  52. strong->consumers.begin(),
  53. strong->consumers.end(),
  54. consumer);
  55. if (it != strong->consumers.end()) {
  56. it->terminate();
  57. }
  58. }
  59. };
  60. strong->consumers.push_back(std::move(consumer));
  61. return lifetime(std::move(result));
  62. }
  63. return lifetime();
  64. });
  65. }
  66. auto events_starting_with(Value &&value) const {
  67. return single<Value&&, Error>(std::move(value)) | then(events());
  68. }
  69. auto events_starting_with_copy(const Value &value) const {
  70. return single<const Value&, Error>(value) | then(events());
  71. }
  72. bool has_consumers() const {
  73. return (_data != nullptr) && !_data->consumers.empty();
  74. }
  75. ~event_stream();
  76. private:
  77. struct Data {
  78. std::vector<consumer<Value, Error>> consumers;
  79. int depth = 0;
  80. };
  81. std::weak_ptr<Data> make_weak() const;
  82. mutable std::shared_ptr<Data> _data;
  83. };
  84. template <typename Value, typename Error>
  85. inline event_stream<Value, Error>::event_stream(event_stream &&other)
  86. : _data(details::take(other._data)) {
  87. }
  88. template <typename Value, typename Error>
  89. inline event_stream<Value, Error> &event_stream<Value, Error>::operator=(
  90. event_stream &&other) {
  91. if (this != &other) {
  92. std::swap(_data, other._data);
  93. other.fire_done();
  94. }
  95. return *this;
  96. }
  97. template <typename Value, typename Error>
  98. template <typename OtherValue>
  99. inline void event_stream<Value, Error>::fire_forward(
  100. OtherValue &&value) const {
  101. if (!_data) {
  102. return;
  103. }
  104. const auto copy = _data;
  105. auto &consumers = copy->consumers;
  106. if (consumers.empty()) {
  107. return;
  108. }
  109. ++copy->depth;
  110. const auto begin = base::index_based_begin(consumers);
  111. const auto end = base::index_based_end(consumers);
  112. // Copy value for every consumer except the last.
  113. const auto prev = end - 1;
  114. auto staleFrom = std::remove_if(begin, prev, [&](const auto &consumer) {
  115. return !consumer.put_next_copy(value);
  116. });
  117. // Perhaps move value for the last consumer.
  118. if (prev->put_next_forward(std::forward<OtherValue>(value))) {
  119. if (staleFrom != prev) {
  120. *staleFrom++ = std::move(*prev);
  121. } else {
  122. ++staleFrom;
  123. }
  124. }
  125. if (staleFrom != end) {
  126. // Move new consumers.
  127. const auto newEnd = base::index_based_end(consumers);
  128. if (newEnd != end) {
  129. Assert(newEnd > end);
  130. for (auto i = end; i != newEnd;) {
  131. *staleFrom++ = *i++;
  132. }
  133. }
  134. // Erase stale consumers.
  135. if (copy->depth == 1) {
  136. consumers.erase(staleFrom.base(), consumers.end());
  137. }
  138. }
  139. --copy->depth;
  140. }
  141. template <typename Value, typename Error>
  142. template <typename OtherError>
  143. inline void event_stream<Value, Error>::fire_error_forward(
  144. OtherError &&error) const {
  145. if (!_data) {
  146. return;
  147. }
  148. const auto data = std::move(_data);
  149. const auto &consumers = data->consumers;
  150. if (consumers.empty()) {
  151. return;
  152. }
  153. const auto begin = base::index_based_begin(consumers);
  154. const auto end = base::index_based_end(consumers);
  155. // Copy error for every consumer except the last.
  156. const auto prev = end - 1;
  157. std::for_each(begin, prev, [&](const auto &consumer) {
  158. consumer.put_error_copy(error);
  159. });
  160. // Perhaps move error for the last consumer.
  161. prev->put_error_forward(std::forward<OtherError>(error));
  162. // Just drop any new consumers.
  163. }
  164. template <typename Value, typename Error>
  165. void event_stream<Value, Error>::fire_done() const {
  166. if (const auto data = details::take(_data)) {
  167. for (const auto &consumer : data->consumers) {
  168. consumer.put_done();
  169. }
  170. }
  171. }
  172. template <typename Value, typename Error>
  173. inline auto event_stream<Value, Error>::make_weak() const
  174. -> std::weak_ptr<Data> {
  175. if (!_data) {
  176. _data = std::make_shared<Data>();
  177. }
  178. return _data;
  179. }
  180. template <typename Value, typename Error>
  181. inline event_stream<Value, Error>::~event_stream() {
  182. fire_done();
  183. }
  184. template <typename Value, typename Error>
  185. inline auto start_to_stream(
  186. event_stream<Value, Error> &stream,
  187. lifetime &alive_while) {
  188. if constexpr (std::is_same_v<Error, no_error>) {
  189. return start_with_next([&](auto &&value) {
  190. stream.fire_forward(std::forward<decltype(value)>(value));
  191. }, alive_while);
  192. } else {
  193. return start_with_next_error([&](auto &&value) {
  194. stream.fire_forward(std::forward<decltype(value)>(value));
  195. }, [&](auto &&error) {
  196. stream.fire_error_forward(std::forward<decltype(error)>(error));
  197. }, alive_while);
  198. }
  199. }
  200. namespace details {
  201. class start_spawning_helper {
  202. public:
  203. start_spawning_helper(lifetime &alive_while)
  204. : _lifetime(alive_while) {
  205. }
  206. template <typename Value, typename Error, typename Generator>
  207. auto operator()(producer<Value, Error, Generator> &&initial) {
  208. auto stream = _lifetime.make_state<event_stream<Value, Error>>();
  209. auto values = std::vector<Value>();
  210. if constexpr (std::is_same_v<Error, rpl::no_error>) {
  211. auto collecting = stream->events().start(
  212. [&](Value &&value) { values.push_back(std::move(value)); },
  213. [](const Error &error) {},
  214. [] {});
  215. std::move(initial) | start_to_stream(*stream, _lifetime);
  216. collecting.destroy();
  217. return vector(std::move(values)) | then(stream->events());
  218. } else {
  219. auto maybeError = std::optional<Error>();
  220. auto collecting = stream->events().start(
  221. [&](Value && value) { values.push_back(std::move(value)); },
  222. [&](Error &&error) { maybeError = std::move(error); },
  223. [] {});
  224. std::move(initial) | start_to_stream(*stream, _lifetime);
  225. collecting.destroy();
  226. if (maybeError.has_value()) {
  227. return rpl::producer<Value, Error>([
  228. error = std::move(*maybeError)
  229. ](const auto &consumer) mutable {
  230. consumer.put_error(std::move(error));
  231. });
  232. }
  233. return rpl::producer<Value, Error>(vector<Value, Error>(
  234. std::move(values)
  235. ) | then(stream->events()));
  236. }
  237. }
  238. private:
  239. lifetime &_lifetime;
  240. };
  241. } // namespace details
  242. inline auto start_spawning(lifetime &alive_while)
  243. -> details::start_spawning_helper {
  244. return details::start_spawning_helper(alive_while);
  245. }
  246. } // namespace rpl