Skip to content

Commit

Permalink
Add common chain function to return result or error
Browse files Browse the repository at this point in the history
  • Loading branch information
melvo committed Mar 27, 2024
1 parent 94aa896 commit f8ea030
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 41 deletions.
18 changes: 18 additions & 0 deletions src/base/QXmppFutureUtils_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ auto mapToSuccess(std::variant<T, Err> var)
});
}

template<typename T, typename Err, typename Converter>
auto mapTaskToSuccess(std::variant<T, Err> var, Converter task)
{
return mapSuccess(std::move(var), task);
}

template<typename Input>
auto chainSuccess(QXmppTask<Input> &&source, QObject *context) -> QXmppTask<QXmppClient::EmptyResult>
{
Expand All @@ -236,6 +242,18 @@ auto chainSuccess(QXmppTask<Input> &&source, QObject *context) -> QXmppTask<QXmp
return promise.task();
}

template<typename Result, typename Input, typename Converter>
auto chainOnSuccess(QXmppTask<Input> &&source, QObject *context, Converter task) -> QXmppTask<Result>
{
QXmppPromise<Result> promise;

source.then(context, [=](Input &&input) mutable {
promise.finish(mapTaskToSuccess(std::move(input), task));
});

return promise.task();
}

template<typename T>
static auto taskFromFuture(QFuture<T> &&future) -> QXmppTask<T>
{
Expand Down
53 changes: 12 additions & 41 deletions src/client/QXmppMixManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,7 @@ QXmppTask<QXmppMixManager::CreationResult> QXmppMixManager::createChannel(const
///
QXmppTask<QXmppMixManager::ChannelJidResult> QXmppMixManager::requestChannelJids(const QString &serviceJid)
{
return chain<ChannelJidResult>(d->discoveryManager->requestDiscoItems(serviceJid), this, [](QXmppDiscoveryManager::ItemsResult &&result) -> ChannelJidResult {
if (auto *error = std::get_if<QXmppError>(&result)) {
return std::move(*error);
}

const auto &items = std::get<QList<QXmppDiscoveryIq::Item>>(result);
return chainOnSuccess<ChannelJidResult>(d->discoveryManager->requestDiscoItems(serviceJid), this, [](QList<QXmppDiscoveryIq::Item> &&items) {
return transform<QVector<ChannelJid>>(items, [](const QXmppDiscoveryIq::Item &item) {
return item.jid();
});
Expand All @@ -452,17 +447,10 @@ QXmppTask<QXmppMixManager::ChannelJidResult> QXmppMixManager::requestChannelJids
///
QXmppTask<QXmppMixManager::ChannelNodeResult> QXmppMixManager::requestChannelNodes(const QString &channelJid)
{
return chain<ChannelNodeResult>(d->discoveryManager->requestDiscoItems(channelJid, MIX_SERVICE_DISCOVERY_NODE.toString()), this, [](QXmppDiscoveryManager::ItemsResult &&result) -> ChannelNodeResult {
if (auto *error = std::get_if<QXmppError>(&result)) {
return std::move(*error);
}

const auto &items = std::get<QList<QXmppDiscoveryIq::Item>>(result);
const auto nodes = transform<QVector<QString>>(items, [](const QXmppDiscoveryIq::Item &item) {
return chainOnSuccess<ChannelNodeResult>(d->discoveryManager->requestDiscoItems(channelJid, MIX_SERVICE_DISCOVERY_NODE.toString()), this, [](QList<QXmppDiscoveryIq::Item> &&items) {
return listToMixNodes(transform<QVector<QString>>(items, [](const QXmppDiscoveryIq::Item &item) {
return item.node();
});

return listToMixNodes(nodes);
}));
});
}

Expand All @@ -475,12 +463,8 @@ QXmppTask<QXmppMixManager::ChannelNodeResult> QXmppMixManager::requestChannelNod
///
QXmppTask<QXmppMixManager::ConfigurationResult> QXmppMixManager::requestChannelConfiguration(const QString &channelJid)
{
return chain<ConfigurationResult>(d->pubSubManager->requestItems<QXmppMixConfigItem>(channelJid, ns_mix_node_config.toString()), this, [](QXmppPubSubManager::ItemsResult<QXmppMixConfigItem> &&result) -> ConfigurationResult {
if (auto *error = std::get_if<QXmppError>(&result)) {
return std::move(*error);
}

return std::get<QXmppPubSubManager::Items<QXmppMixConfigItem>>(result).items.takeFirst();
return chainOnSuccess<ConfigurationResult>(d->pubSubManager->requestItems<QXmppMixConfigItem>(channelJid, ns_mix_node_config.toString()), this, [](QXmppPubSubManager::Items<QXmppMixConfigItem> &&items) {
return items.items.takeFirst();
});
}

Expand Down Expand Up @@ -520,12 +504,8 @@ QXmppTask<QXmppClient::EmptyResult> QXmppMixManager::updateChannelConfiguration(
///
QXmppTask<QXmppMixManager::InformationResult> QXmppMixManager::requestChannelInformation(const QString &channelJid)
{
return chain<InformationResult>(d->pubSubManager->requestItems<QXmppMixInfoItem>(channelJid, ns_mix_node_info.toString()), this, [](QXmppPubSubManager::ItemsResult<QXmppMixInfoItem> &&result) -> InformationResult {
if (auto *error = std::get_if<QXmppError>(&result)) {
return std::move(*error);
}

return std::get<QXmppPubSubManager::Items<QXmppMixInfoItem>>(result).items.takeFirst();
return chainOnSuccess<InformationResult>(d->pubSubManager->requestItems<QXmppMixInfoItem>(channelJid, ns_mix_node_info.toString()), this, [](QXmppPubSubManager::Items<QXmppMixInfoItem> &&items) {
return items.items.takeFirst();
});
}

Expand Down Expand Up @@ -885,12 +865,8 @@ QXmppTask<QXmppClient::EmptyResult> QXmppMixManager::unbanAllJids(const QString
///
QXmppTask<QXmppMixManager::ParticipantResult> QXmppMixManager::requestParticipants(const QString &channelJid)
{
return chain<ParticipantResult>(d->pubSubManager->requestItems<QXmppMixParticipantItem>(channelJid, ns_mix_node_participants.toString()), this, [](QXmppPubSubManager::ItemsResult<QXmppMixParticipantItem> &&result) -> ParticipantResult {
if (auto *error = std::get_if<QXmppError>(&result)) {
return std::move(*error);
}

return std::get<QXmppPubSubManager::Items<QXmppMixParticipantItem>>(std::move(result)).items;
return chainOnSuccess<ParticipantResult>(d->pubSubManager->requestItems<QXmppMixParticipantItem>(channelJid, ns_mix_node_participants.toString()), this, [](QXmppPubSubManager::Items<QXmppMixParticipantItem> &&items) {
return items.items;
});
}

Expand Down Expand Up @@ -1186,13 +1162,8 @@ QXmppTask<QXmppMixManager::JoiningResult> QXmppMixManager::joinChannel(QXmppMixI
///
QXmppTask<QXmppMixManager::JidResult> QXmppMixManager::requestJids(const QString &channelJid, const QString &node)
{
return chain<JidResult>(d->pubSubManager->requestItems(channelJid, node), this, [](QXmppPubSubManager::ItemsResult<QXmppPubSubBaseItem> &&result) -> JidResult {
if (auto error = std::get_if<QXmppError>(&result)) {
return std::move(*error);
}

const auto &items = std::get<QXmppPubSubManager::Items<QXmppPubSubBaseItem>>(result).items;
return transform<QVector<Jid>>(items, [](const QXmppPubSubBaseItem &item) {
return chainOnSuccess<JidResult>(d->pubSubManager->requestItems(channelJid, node), this, [](QXmppPubSubManager::Items<QXmppPubSubBaseItem> &&items) {
return transform<QVector<Jid>>(items.items, [](const QXmppPubSubBaseItem &item) {
return item.id();
});
});
Expand Down

0 comments on commit f8ea030

Please sign in to comment.