From b30fe2fa42a717d6e89710cde82ecf7f419b2fe9 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Fri, 12 Dec 2014 11:48:09 +0100 Subject: ClientAPI: Demonstrate how we can build powerful and flexible API's on top of ResultEmitter --- client/clientapi.h | 45 +++++++++++++++++++++++++++++++++++++++++++ client/test/clientapitest.cpp | 12 +++--------- 2 files changed, 48 insertions(+), 9 deletions(-) (limited to 'client') diff --git a/client/clientapi.h b/client/clientapi.h index 592fa48..662f96c 100644 --- a/client/clientapi.h +++ b/client/clientapi.h @@ -6,6 +6,7 @@ #include #include #include +#include #include namespace async { @@ -64,6 +65,14 @@ namespace async { * The future side for the client. * * It does not directly hold the state. + * + * The advantage of this is that we can specialize it to: + * * do inline transformations to the data + * * directly store the state in a suitable datastructure: QList, QSet, std::list, QVector, ... + * * build async interfaces with signals + * * build sync interfaces that block when accessing the value + * + * TODO: This should probably be merged with daniels futurebase used in Async */ template class ResultEmitter { @@ -85,6 +94,42 @@ namespace async { std::function completeHandler; }; + + /* + * A result set specialization that provides a syncronous list + */ + template + class SyncListResult : public QList { + public: + SyncListResult(const QSharedPointer > &emitter) + :QList(), + mComplete(false), + mEmitter(emitter) + { + emitter->onAdded([this](const T &value) { + this->append(value); + }); + emitter->onComplete([this]() { + mComplete = true; + auto loop = mWaitLoop.toStrongRef(); + if (loop) { + loop->quit(); + } + }); + } + + void exec() + { + auto loop = QSharedPointer::create(); + mWaitLoop = loop; + loop->exec(QEventLoop::ExcludeUserInputEvents); + } + + private: + bool mComplete; + QWeakPointer mWaitLoop; + QSharedPointer > mEmitter; + }; } namespace Akonadi2 { diff --git a/client/test/clientapitest.cpp b/client/test/clientapitest.cpp index bff910b..2d1c238 100644 --- a/client/test/clientapitest.cpp +++ b/client/test/clientapitest.cpp @@ -37,15 +37,9 @@ private Q_SLOTS: Akonadi2::Query query; query.resources << "dummyresource"; - auto result = Akonadi2::Store::load(query); - - QList resultSet; - result->onAdded([&resultSet](const Akonadi2::Domain::Event::Ptr &event){ resultSet << event; qDebug() << "result added";}); - - bool complete; - result->onComplete([&complete]{ complete = true; qDebug() << "complete";}); - - QTRY_VERIFY(complete); + async::SyncListResult result(Akonadi2::Store::load(query)); + result.exec(); + QCOMPARE(result.size(), 1); } }; -- cgit v1.2.3