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>
|
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 :(
|
|
|
|
|
|
|
|
template <typename Value = empty_value>
|
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
|
|
|
}
|
2017-09-27 11:16:05 +00:00
|
|
|
auto events() const {
|
2017-11-30 18:04:13 +00:00
|
|
|
return make_producer<Value>([weak = make_weak()](
|
2017-10-31 18:25:22 +00:00
|
|
|
const auto &consumer) {
|
2017-09-27 11:16:05 +00:00
|
|
|
if (auto strong = weak.lock()) {
|
|
|
|
auto result = [weak, consumer] {
|
|
|
|
if (auto strong = weak.lock()) {
|
2017-11-20 12:23:20 +00:00
|
|
|
auto it = std::find(
|
|
|
|
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 {
|
2017-09-12 13:58:14 +00:00
|
|
|
return single(std::move(value)) | then(events());
|
|
|
|
}
|
2017-09-27 11:16:05 +00:00
|
|
|
auto events_starting_with_copy(const Value &value) const {
|
2017-09-14 19:27:41 +00:00
|
|
|
return single(value) | then(events());
|
2017-09-12 13:58:14 +00:00
|
|
|
}
|
2017-09-03 15:57:51 +00:00
|
|
|
|
|
|
|
~event_stream();
|
|
|
|
|
|
|
|
private:
|
2017-10-31 18:25:22 +00:00
|
|
|
struct Data {
|
|
|
|
std::vector<consumer<Value, no_error>> consumers;
|
|
|
|
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
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Value>
|
2017-09-18 16:39:45 +00:00
|
|
|
inline event_stream<Value>::event_stream() {
|
2017-09-04 11:24:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Value>
|
2017-09-18 16:39:45 +00:00
|
|
|
inline event_stream<Value>::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
|
|
|
}
|
|
|
|
|
2017-10-31 07:03:18 +00:00
|
|
|
template <typename Value>
|
|
|
|
inline event_stream<Value> &event_stream<Value>::operator=(
|
|
|
|
event_stream &&other) {
|
2017-11-20 12:23:20 +00:00
|
|
|
_data = details::take(other._data);
|
2017-10-31 07:03:18 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-03 15:57:51 +00:00
|
|
|
template <typename Value>
|
2017-09-14 19:27:41 +00:00
|
|
|
template <typename OtherValue>
|
2017-09-26 17:57:01 +00:00
|
|
|
inline void event_stream<Value>::fire_forward(
|
|
|
|
OtherValue &&value) const {
|
2017-10-31 18:25:22 +00:00
|
|
|
auto copy = _data;
|
|
|
|
if (!copy) {
|
2017-09-12 13:58:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-31 18:25:22 +00:00
|
|
|
++copy->depth;
|
|
|
|
auto &consumers = copy->consumers;
|
2017-09-05 08:22:06 +00:00
|
|
|
auto begin = base::index_based_begin(consumers);
|
|
|
|
auto end = base::index_based_end(consumers);
|
|
|
|
if (begin != end) {
|
|
|
|
// Copy value for every consumer except the last.
|
|
|
|
auto prev = end - 1;
|
|
|
|
auto removeFrom = std::remove_if(begin, prev, [&](auto &consumer) {
|
|
|
|
return !consumer.put_next_copy(value);
|
|
|
|
});
|
|
|
|
|
2017-09-14 19:27:41 +00:00
|
|
|
// Perhaps move value for the last consumer.
|
|
|
|
if (prev->put_next_forward(std::forward<OtherValue>(value))) {
|
2017-09-05 08:22:06 +00:00
|
|
|
if (removeFrom != prev) {
|
|
|
|
*removeFrom++ = std::move(*prev);
|
|
|
|
} else {
|
|
|
|
++removeFrom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (removeFrom != end) {
|
|
|
|
// Move new consumers.
|
|
|
|
auto newEnd = base::index_based_end(consumers);
|
|
|
|
if (newEnd != end) {
|
|
|
|
Assert(newEnd > end);
|
|
|
|
while (end != newEnd) {
|
|
|
|
*removeFrom++ = *end++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Erase stale consumers.
|
2017-10-31 18:25:22 +00:00
|
|
|
if (copy->depth == 1) {
|
|
|
|
consumers.erase(removeFrom.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
|
|
|
}
|
|
|
|
|
2017-09-12 13:58:14 +00:00
|
|
|
template <typename Value>
|
2017-11-30 18:04:13 +00:00
|
|
|
inline auto event_stream<Value>::make_weak() const
|
2017-10-31 18:25:22 +00:00
|
|
|
-> std::weak_ptr<Data> {
|
|
|
|
if (!_data) {
|
|
|
|
_data = std::make_shared<Data>();
|
2017-09-12 13:58:14 +00:00
|
|
|
}
|
2017-10-31 18:25:22 +00:00
|
|
|
return _data;
|
2017-09-12 13:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-03 15:57:51 +00:00
|
|
|
template <typename Value>
|
2017-09-18 16:39:45 +00:00
|
|
|
inline event_stream<Value>::~event_stream() {
|
2017-11-20 12:23:20 +00:00
|
|
|
if (auto data = details::take(_data)) {
|
2017-10-31 18:25:22 +00:00
|
|
|
for (auto &consumer : data->consumers) {
|
2017-09-04 11:24:35 +00:00
|
|
|
consumer.put_done();
|
|
|
|
}
|
2017-09-03 15:57:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 13:58:14 +00:00
|
|
|
template <typename Value>
|
2017-09-21 19:21:33 +00:00
|
|
|
inline auto start_to_stream(
|
|
|
|
event_stream<Value> &stream,
|
|
|
|
lifetime &alive_while) {
|
2017-09-27 08:43:35 +00:00
|
|
|
return start_with_next([&stream](auto &&value) {
|
2017-09-14 19:27:41 +00:00
|
|
|
stream.fire_forward(std::forward<decltype(value)>(value));
|
2017-09-21 19:21:33 +00:00
|
|
|
}, alive_while);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2017-09-21 19:21:33 +00:00
|
|
|
auto stream = _lifetime.make_state<event_stream<Value>>();
|
|
|
|
auto collected = std::vector<Value>();
|
|
|
|
{
|
|
|
|
auto collecting = stream->events().start(
|
|
|
|
[&collected](Value &&value) {
|
|
|
|
collected.push_back(std::move(value));
|
|
|
|
},
|
|
|
|
[](const Error &error) {},
|
|
|
|
[] {});
|
|
|
|
std::move(initial) | start_to_stream(*stream, _lifetime);
|
|
|
|
}
|
2017-09-27 11:16:05 +00:00
|
|
|
return vector(std::move(collected))
|
2017-09-21 19:21:33 +00:00
|
|
|
| then(stream->events());
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|