From bf078ff6342b052e36995defcf641edea6bb2b8c Mon Sep 17 00:00:00 2001 From: Linus Jahn Date: Sat, 31 Dec 2022 23:29:59 +0100 Subject: OmemoManager: Replace multi-result futures with single result futures QXmppTask won't support multi-result and in the case of the OmemoManager it's not really needed. If you want to know when each single request is finished you can just subscribe/unsubscribe from each jid in a single request. Part of #502. --- src/omemo/QXmppOmemoManager.cpp | 58 +++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 23 deletions(-) (limited to 'src/omemo/QXmppOmemoManager.cpp') diff --git a/src/omemo/QXmppOmemoManager.cpp b/src/omemo/QXmppOmemoManager.cpp index 11a7ec36..dc2cd5a2 100644 --- a/src/omemo/QXmppOmemoManager.cpp +++ b/src/omemo/QXmppOmemoManager.cpp @@ -564,31 +564,38 @@ void Manager::setMaximumDevicesPerStanza(int maximum) /// /// \return the results of the requests for each JID /// -QFuture Manager::requestDeviceLists(const QList &jids) +QFuture> Manager::requestDeviceLists(const QList &jids) { if (const auto jidsCount = jids.size()) { - QFutureInterface interface(QFutureInterfaceBase::Started); - auto processedJidsCount = std::make_shared(0); + struct State { + int processed = 0; + int jidsCount = 0; + QFutureInterface> interface; + QVector devicesResults; + }; + + auto state = std::make_shared(); + state->jidsCount = jids.count(); for (const auto &jid : jids) { Q_ASSERT_X(jid != d->ownBareJid(), "Requesting contact's device list", "Own JID passed"); auto future = d->requestDeviceList(jid); - await(future, this, [=](auto result) mutable { - DevicesResult devicesResult { + await(future, this, [jid, state](auto result) mutable { + state->devicesResults << DevicesResult { jid, mapSuccess(std::move(result), [](QXmppOmemoDeviceListItem) { return Success(); }) }; - interface.reportResult(devicesResult); - if (++(*processedJidsCount) == jidsCount) { - interface.reportFinished(); + if (++(state->processed) == state->jidsCount) { + state->interface.reportResult(state->devicesResults); + state->interface.reportFinished(); } }); } - return interface.future(); + return state->interface.future(); } - return QFutureInterface(QFutureInterfaceBase::Finished).future(); + return makeReadyFuture(QVector()); } /// @@ -605,31 +612,36 @@ QFuture Manager::requestDeviceLists(const QList /// /// \return the results of the subscription for each JID /// -QFuture Manager::subscribeToDeviceLists(const QList &jids) +QFuture> Manager::subscribeToDeviceLists(const QList &jids) { - QFutureInterface interface(QFutureInterfaceBase::Started); - if (const auto jidsCount = jids.size()) { - auto processedJidsCount = std::make_shared(0); + struct State { + int processed = 0; + int jidsCount = 0; + QFutureInterface> interface; + QVector devicesResults; + }; + + auto state = std::make_shared(); + state->jidsCount = jids.size(); for (const auto &jid : jids) { auto future = d->subscribeToDeviceList(jid); - await(future, this, [=](QXmppPubSubManager::Result result) mutable { + await(future, this, [state, jid](QXmppPubSubManager::Result result) mutable { Manager::DevicesResult devicesResult; devicesResult.jid = jid; devicesResult.result = result; - interface.reportResult(devicesResult); + state->devicesResults << devicesResult; - if (++(*processedJidsCount) == jidsCount) { - interface.reportFinished(); + if (++(state->processed) == state->jidsCount) { + state->interface.reportResult(state->devicesResults); + state->interface.reportFinished(); } }); } - } else { - interface.reportFinished(); + return state->interface.future(); } - - return interface.future(); + return makeReadyFuture(QVector()); } /// @@ -641,7 +653,7 @@ QFuture Manager::subscribeToDeviceLists(const QList Manager::unsubscribeFromDeviceLists() +QFuture> Manager::unsubscribeFromDeviceLists() { return d->unsubscribeFromDeviceLists(d->jidsOfManuallySubscribedDevices); } -- cgit v1.2.3