Some checks failed
Docker. / Ubuntu (push) Has been cancelled
User-agent updater. / User-agent (push) Failing after 15s
Lock Threads / lock (push) Failing after 10s
Waiting for answer. / waiting-for-answer (push) Failing after 22s
Close stale issues and PRs / stale (push) Successful in 13s
Needs user action. / needs-user-action (push) Failing after 8s
Can't reproduce. / cant-reproduce (push) Failing after 8s
74 lines
1.7 KiB
C++
74 lines
1.7 KiB
C++
// This file is part of Desktop App Toolkit,
|
|
// a set of libraries for developing nice desktop applications.
|
|
//
|
|
// For license and copyright information please follow this link:
|
|
// https://github.com/desktop-app/legal/blob/master/LEGAL
|
|
//
|
|
#pragma once
|
|
|
|
#include <rpl/producer.h>
|
|
|
|
namespace rpl {
|
|
namespace details {
|
|
|
|
class flatten_latest_helper {
|
|
public:
|
|
template <
|
|
typename Value,
|
|
typename Error,
|
|
typename Generator,
|
|
typename MetaGenerator>
|
|
auto operator()(producer<
|
|
producer<Value, Error, Generator>,
|
|
Error,
|
|
MetaGenerator> &&initial) const {
|
|
return make_producer<Value, Error>([
|
|
initial = std::move(initial)
|
|
](const auto &consumer) mutable {
|
|
auto state = consumer.template make_state<State>();
|
|
return std::move(initial).start(
|
|
[consumer, state](producer<Value, Error> &&inner) {
|
|
state->finished = false;
|
|
state->alive = lifetime();
|
|
std::move(inner).start(
|
|
[consumer](auto &&value) {
|
|
consumer.put_next_forward(std::forward<decltype(value)>(value));
|
|
}, [consumer](auto &&error) {
|
|
consumer.put_error_forward(std::forward<decltype(error)>(error));
|
|
}, [consumer, state] {
|
|
if (state->finished) {
|
|
consumer.put_done();
|
|
} else {
|
|
state->finished = true;
|
|
}
|
|
}, state->alive);
|
|
}, [consumer](auto &&error) {
|
|
consumer.put_error_forward(std::forward<decltype(error)>(error));
|
|
}, [consumer, state] {
|
|
if (state->finished) {
|
|
consumer.put_done();
|
|
} else {
|
|
state->finished = true;
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
private:
|
|
struct State {
|
|
lifetime alive;
|
|
bool finished = false;
|
|
};
|
|
|
|
};
|
|
|
|
} // namespace details
|
|
|
|
inline auto flatten_latest()
|
|
-> details::flatten_latest_helper {
|
|
return details::flatten_latest_helper();
|
|
}
|
|
|
|
} // namespace rpl
|
|
|