2017-09-03 15:57:51 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-09-03 15:57:51 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-09-03 15:57:51 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-09-12 13:58:14 +00:00
|
|
|
#include <rpl/producer.h>
|
2017-09-21 19:21:33 +00:00
|
|
|
#include <rpl/range.h>
|
2017-09-12 13:58:14 +00:00
|
|
|
#include <rpl/then.h>
|
2017-09-21 19:21:33 +00:00
|
|
|
#include <rpl/range.h>
|
2017-10-27 08:51:11 +00:00
|
|
|
#include <algorithm>
|
2019-02-16 12:47:52 +00:00
|
|
|
#include <optional>
|
2017-09-04 11:24:35 +00:00
|
|
|
#include "base/assertion.h"
|
2017-09-05 08:22:06 +00:00
|
|
|
#include "base/index_based_iterator.h"
|
2017-09-03 15:57:51 +00:00
|
|
|
|
|
|
|
namespace rpl {
|
|
|
|
|
2017-09-12 13:58:14 +00:00
|
|
|
// Currently not thread-safe :(
|
|
|
|
|
2019-02-16 12:47:52 +00:00
|
|
|
template <typename Value = empty_value, typename Error = no_error>
|
2017-09-03 15:57:51 +00:00
|
|
|
class event_stream {
|
|
|
|
public:
|
|
|
|
event_stream();
|
2017-09-04 11:24:35 +00:00
|
|
|
event_stream(event_stream &&other);
|
2017-10-31 07:03:18 +00:00
|
|
|
event_stream &operator=(event_stream &&other);
|
2017-09-03 15:57:51 +00:00
|
|
|
|
2017-09-14 19:27:41 +00:00
|
|
|
template <typename OtherValue>
|
2017-09-26 17:57:01 +00:00
|
|
|
void fire_forward(OtherValue &&value) const;
|
|
|
|
void fire(Value &&value) const {
|
2017-09-14 19:27:41 +00:00
|
|
|
return fire_forward(std::move(value));
|
|
|
|
}
|
2017-09-26 17:57:01 +00:00
|
|
|
void fire_copy(const Value &value) const {
|
2017-09-14 19:27:41 +00:00
|
|
|
return fire_forward(value);
|
2017-09-12 13:58:14 +00:00
|
|
|
}
|
2019-02-16 12:47:52 +00:00
|
|
|
|
|
|
|
template <typename OtherError>
|
|
|
|
void fire_error_forward(OtherError &&error) const;
|
|
|
|
void fire_error(Error &&error) const {
|
|
|
|
return fire_error_forward(std::move(error));
|
|
|
|
}
|
|
|
|
void fire_error_copy(const Error &error) const {
|
|
|
|
return fire_error_forward(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fire_done() const;
|
|
|
|
|
|
|
|
#if defined _MSC_VER && _MSC_VER >= 1914 && _MSC_VER < 1916
|
|
|
|
producer<Value, Error> events() const {
|
|
|
|
#else // _MSC_VER >= 1914 && _MSC_VER < 1916
|
2017-09-27 11:16:05 +00:00
|
|
|
auto events() const {
|
2019-02-16 12:47:52 +00:00
|
|
|
#endif // _MSC_VER >= 1914 && _MSC_VER < 1916
|
|
|
|
return make_producer<Value, Error>([weak = make_weak()](
|
2017-10-31 18:25:22 +00:00
|
|
|
const auto &consumer) {
|
2019-02-16 12:47:52 +00:00
|
|
|
if (const auto strong = weak.lock()) {
|
2017-09-27 11:16:05 +00:00
|
|
|
auto result = [weak, consumer] {
|
2019-02-16 12:47:52 +00:00
|
|
|
if (const auto strong = weak.lock()) {
|
|
|
|
const auto it = std::find(
|
2017-11-20 12:23:20 +00:00
|
|
|
strong->consumers.begin(),
|
|
|
|
strong->consumers.end(),
|
2017-10-31 18:25:22 +00:00
|
|
|
consumer);
|
|
|
|
if (it != strong->consumers.end()) {
|
2017-09-27 11:16:05 +00:00
|
|
|
it->terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-10-31 18:25:22 +00:00
|
|
|
strong->consumers.push_back(std::move(consumer));
|
2017-09-27 11:16:05 +00:00
|
|
|
return lifetime(std::move(result));
|
|
|
|
}
|
|
|
|
return lifetime();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
auto events_starting_with(Value &&value) const {
|
2019-02-20 13:28:48 +00:00
|
|
|
return single<Value&&, Error>(std::move(value)) | then(events());
|
2017-09-12 13:58:14 +00:00
|
|
|
}
|
2017-09-27 11:16:05 +00:00
|
|
|
auto events_starting_with_copy(const Value &value) const {
|
2019-02-20 13:28:48 +00:00
|
|
|
return single<const Value&, Error>(value) | then(events());
|
2017-09-12 13:58:14 +00:00
|
|
|
}
|
2018-08-29 13:23:16 +00:00
|
|
|
bool has_consumers() const {
|
|
|
|
return (_data != nullptr) && !_data->consumers.empty();
|
|
|
|
}
|
2017-09-03 15:57:51 +00:00
|
|
|
|
|
|
|
~event_stream();
|
|
|
|
|
|
|
|
private:
|
2017-10-31 18:25:22 +00:00
|
|
|
struct Data {
|
2019-02-16 12:47:52 +00:00
|
|
|
std::vector<consumer<Value, Error>> consumers;
|
2017-10-31 18:25:22 +00:00
|
|
|
int depth = 0;
|
|
|
|
};
|
2017-11-30 18:04:13 +00:00
|
|
|
std::weak_ptr<Data> make_weak() const;
|
2017-09-03 15:57:51 +00:00
|
|
|
|
2017-10-31 18:25:22 +00:00
|
|
|
mutable std::shared_ptr<Data> _data;
|
2017-09-03 15:57:51 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2019-02-16 12:47:52 +00:00
|
|
|
template <typename Value, typename Error>
|
|
|
|
inline event_stream<Value, Error>::event_stream() {
|
2017-09-04 11:24:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 12:47:52 +00:00
|
|
|
template <typename Value, typename Error>
|
|
|
|
inline event_stream<Value, Error>::event_stream(event_stream &&other)
|
2017-11-20 12:23:20 +00:00
|
|
|
: _data(details::take(other._data)) {
|
2017-09-03 15:57:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 12:47:52 +00:00
|
|
|
template <typename Value, typename Error>
|
|
|
|
inline event_stream<Value, Error> &event_stream<Value, Error>::operator=(
|
2017-10-31 07:03:18 +00:00
|
|
|
event_stream &&other) {
|
2019-02-16 12:47:52 +00:00
|
|
|
if (this != &other) {
|
2019-02-22 11:58:26 +00:00
|
|
|
std::swap(_data, other._data);
|
|
|
|
other.fire_done();
|
2019-02-16 12:47:52 +00:00
|
|
|
}
|
2017-10-31 07:03:18 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:47:52 +00:00
|
|
|
template <typename Value, typename Error>
|
2017-09-14 19:27:41 +00:00
|
|
|
template <typename OtherValue>
|
2019-02-16 12:47:52 +00:00
|
|
|
inline void event_stream<Value, Error>::fire_forward(
|
2017-09-26 17:57:01 +00:00
|
|
|
OtherValue &&value) const {
|
2019-02-16 12:47:52 +00:00
|
|
|
if (!_data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto copy = _data;
|
|
|
|
auto &consumers = copy->consumers;
|
|
|
|
if (consumers.empty()) {
|
2017-09-12 13:58:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-31 18:25:22 +00:00
|
|
|
++copy->depth;
|
2019-02-16 12:47:52 +00:00
|
|
|
const auto begin = base::index_based_begin(consumers);
|
|
|
|
const auto end = base::index_based_end(consumers);
|
2017-09-05 08:22:06 +00:00
|
|
|
|
2019-02-16 12:47:52 +00:00
|
|
|
// Copy value for every consumer except the last.
|
|
|
|
const auto prev = end - 1;
|
|
|
|
auto staleFrom = std::remove_if(begin, prev, [&](const auto &consumer) {
|
|
|
|
return !consumer.put_next_copy(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Perhaps move value for the last consumer.
|
|
|
|
if (prev->put_next_forward(std::forward<OtherValue>(value))) {
|
|
|
|
if (staleFrom != prev) {
|
|
|
|
*staleFrom++ = std::move(*prev);
|
|
|
|
} else {
|
|
|
|
++staleFrom;
|
2017-09-05 08:22:06 +00:00
|
|
|
}
|
2019-02-16 12:47:52 +00:00
|
|
|
}
|
2017-09-05 08:22:06 +00:00
|
|
|
|
2019-02-16 12:47:52 +00:00
|
|
|
if (staleFrom != end) {
|
|
|
|
// Move new consumers.
|
|
|
|
const auto newEnd = base::index_based_end(consumers);
|
|
|
|
if (newEnd != end) {
|
|
|
|
Assert(newEnd > end);
|
|
|
|
for (auto i = end; i != newEnd;) {
|
|
|
|
*staleFrom++ = *i++;
|
2017-09-05 08:22:06 +00:00
|
|
|
}
|
2019-02-16 12:47:52 +00:00
|
|
|
}
|
2017-09-05 08:22:06 +00:00
|
|
|
|
2019-02-16 12:47:52 +00:00
|
|
|
// Erase stale consumers.
|
|
|
|
if (copy->depth == 1) {
|
|
|
|
consumers.erase(staleFrom.base(), consumers.end());
|
2017-09-05 08:22:06 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-31 18:25:22 +00:00
|
|
|
--copy->depth;
|
2017-09-03 15:57:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 12:47:52 +00:00
|
|
|
template <typename Value, typename Error>
|
|
|
|
template <typename OtherError>
|
|
|
|
inline void event_stream<Value, Error>::fire_error_forward(
|
|
|
|
OtherError &&error) const {
|
2017-10-31 18:25:22 +00:00
|
|
|
if (!_data) {
|
2019-02-16 12:47:52 +00:00
|
|
|
return;
|
2017-09-12 13:58:14 +00:00
|
|
|
}
|
2019-02-16 12:47:52 +00:00
|
|
|
const auto data = std::move(_data);
|
|
|
|
const auto &consumers = data->consumers;
|
|
|
|
if (consumers.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto begin = base::index_based_begin(consumers);
|
|
|
|
const auto end = base::index_based_end(consumers);
|
|
|
|
|
|
|
|
// Copy error for every consumer except the last.
|
|
|
|
const auto prev = end - 1;
|
|
|
|
std::for_each(begin, prev, [&](const auto &consumer) {
|
|
|
|
consumer.put_error_copy(error);
|
|
|
|
});
|
2017-09-12 13:58:14 +00:00
|
|
|
|
2019-02-16 12:47:52 +00:00
|
|
|
// Perhaps move error for the last consumer.
|
|
|
|
prev->put_error_forward(std::forward<OtherError>(error));
|
2017-09-12 13:58:14 +00:00
|
|
|
|
2019-02-16 12:47:52 +00:00
|
|
|
// Just drop any new consumers.
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Value, typename Error>
|
|
|
|
void event_stream<Value, Error>::fire_done() const {
|
|
|
|
if (const auto data = details::take(_data)) {
|
|
|
|
for (const auto &consumer : data->consumers) {
|
2017-09-04 11:24:35 +00:00
|
|
|
consumer.put_done();
|
|
|
|
}
|
2017-09-03 15:57:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:47:52 +00:00
|
|
|
template <typename Value, typename Error>
|
|
|
|
inline auto event_stream<Value, Error>::make_weak() const
|
|
|
|
-> std::weak_ptr<Data> {
|
|
|
|
if (!_data) {
|
|
|
|
_data = std::make_shared<Data>();
|
|
|
|
}
|
|
|
|
return _data;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Value, typename Error>
|
|
|
|
inline event_stream<Value, Error>::~event_stream() {
|
|
|
|
fire_done();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Value, typename Error>
|
2017-09-21 19:21:33 +00:00
|
|
|
inline auto start_to_stream(
|
2019-02-16 12:47:52 +00:00
|
|
|
event_stream<Value, Error> &stream,
|
2017-09-21 19:21:33 +00:00
|
|
|
lifetime &alive_while) {
|
2019-02-16 12:47:52 +00:00
|
|
|
if constexpr (std::is_same_v<Error, no_error>) {
|
|
|
|
return start_with_next_done([&](auto &&value) {
|
|
|
|
stream.fire_forward(std::forward<decltype(value)>(value));
|
|
|
|
}, [&] {
|
|
|
|
stream.fire_done();
|
|
|
|
}, alive_while);
|
|
|
|
} else {
|
|
|
|
return start_with_next_error_done([&](auto &&value) {
|
|
|
|
stream.fire_forward(std::forward<decltype(value)>(value));
|
|
|
|
}, [&](auto &&error) {
|
|
|
|
stream.fire_error_forward(std::forward<decltype(error)>(error));
|
|
|
|
}, [&] {
|
|
|
|
stream.fire_done();
|
|
|
|
}, alive_while);
|
|
|
|
}
|
2017-09-21 19:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace details {
|
|
|
|
|
|
|
|
class start_spawning_helper {
|
|
|
|
public:
|
|
|
|
start_spawning_helper(lifetime &alive_while)
|
|
|
|
: _lifetime(alive_while) {
|
|
|
|
}
|
2017-09-27 11:16:05 +00:00
|
|
|
|
|
|
|
template <typename Value, typename Error, typename Generator>
|
|
|
|
auto operator()(producer<Value, Error, Generator> &&initial) {
|
2019-02-16 12:47:52 +00:00
|
|
|
auto stream = _lifetime.make_state<event_stream<Value, Error>>();
|
|
|
|
auto values = std::vector<Value>();
|
|
|
|
if constexpr (std::is_same_v<Error, rpl::no_error>) {
|
2017-09-21 19:21:33 +00:00
|
|
|
auto collecting = stream->events().start(
|
2019-02-16 12:47:52 +00:00
|
|
|
[&](Value &&value) { values.push_back(std::move(value)); },
|
2017-09-21 19:21:33 +00:00
|
|
|
[](const Error &error) {},
|
|
|
|
[] {});
|
|
|
|
std::move(initial) | start_to_stream(*stream, _lifetime);
|
2019-02-16 12:47:52 +00:00
|
|
|
collecting.destroy();
|
|
|
|
|
|
|
|
return vector(std::move(values)) | then(stream->events());
|
|
|
|
} else {
|
|
|
|
auto maybeError = std::optional<Error>();
|
|
|
|
auto collecting = stream->events().start(
|
|
|
|
[&](Value && value) { values.push_back(std::move(value)); },
|
2019-02-18 15:54:02 +00:00
|
|
|
[&](Error &&error) { maybeError = std::move(error); },
|
2019-02-16 12:47:52 +00:00
|
|
|
[] {});
|
|
|
|
std::move(initial) | start_to_stream(*stream, _lifetime);
|
|
|
|
collecting.destroy();
|
|
|
|
|
|
|
|
if (maybeError.has_value()) {
|
|
|
|
return rpl::producer<Value, Error>([
|
|
|
|
error = std::move(*maybeError)
|
|
|
|
](const auto &consumer) mutable {
|
|
|
|
consumer.put_error(std::move(error));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return rpl::producer<Value, Error>(vector<Value, Error>(
|
|
|
|
std::move(values)
|
|
|
|
) | then(stream->events()));
|
2017-09-21 19:21:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
lifetime &_lifetime;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace details
|
|
|
|
|
|
|
|
inline auto start_spawning(lifetime &alive_while)
|
|
|
|
-> details::start_spawning_helper {
|
|
|
|
return details::start_spawning_helper(alive_while);
|
2017-09-12 13:58:14 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 15:57:51 +00:00
|
|
|
} // namespace rpl
|