summaryrefslogtreecommitdiffstats
path: root/client/clientapi.h
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2014-12-12 11:48:09 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2014-12-12 11:48:09 +0100
commitb30fe2fa42a717d6e89710cde82ecf7f419b2fe9 (patch)
tree01b0a7b48b09dc908985e9473d9588d91e5294cf /client/clientapi.h
parentc30e9145049c52feb2de719307ebbfee0650f01b (diff)
downloadsink-b30fe2fa42a717d6e89710cde82ecf7f419b2fe9.tar.gz
sink-b30fe2fa42a717d6e89710cde82ecf7f419b2fe9.zip
ClientAPI: Demonstrate how we can build powerful and flexible API's on top of ResultEmitter
Diffstat (limited to 'client/clientapi.h')
-rw-r--r--client/clientapi.h45
1 files changed, 45 insertions, 0 deletions
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 @@
6#include <QStandardPaths> 6#include <QStandardPaths>
7#include <QTimer> 7#include <QTimer>
8#include <QDebug> 8#include <QDebug>
9#include <QEventLoop>
9#include <functional> 10#include <functional>
10 11
11namespace async { 12namespace async {
@@ -64,6 +65,14 @@ namespace async {
64 * The future side for the client. 65 * The future side for the client.
65 * 66 *
66 * It does not directly hold the state. 67 * It does not directly hold the state.
68 *
69 * The advantage of this is that we can specialize it to:
70 * * do inline transformations to the data
71 * * directly store the state in a suitable datastructure: QList, QSet, std::list, QVector, ...
72 * * build async interfaces with signals
73 * * build sync interfaces that block when accessing the value
74 *
75 * TODO: This should probably be merged with daniels futurebase used in Async
67 */ 76 */
68 template<class DomainType> 77 template<class DomainType>
69 class ResultEmitter { 78 class ResultEmitter {
@@ -85,6 +94,42 @@ namespace async {
85 std::function<void(void)> completeHandler; 94 std::function<void(void)> completeHandler;
86 }; 95 };
87 96
97
98 /*
99 * A result set specialization that provides a syncronous list
100 */
101 template<class T>
102 class SyncListResult : public QList<T> {
103 public:
104 SyncListResult(const QSharedPointer<ResultEmitter<T> > &emitter)
105 :QList<T>(),
106 mComplete(false),
107 mEmitter(emitter)
108 {
109 emitter->onAdded([this](const T &value) {
110 this->append(value);
111 });
112 emitter->onComplete([this]() {
113 mComplete = true;
114 auto loop = mWaitLoop.toStrongRef();
115 if (loop) {
116 loop->quit();
117 }
118 });
119 }
120
121 void exec()
122 {
123 auto loop = QSharedPointer<QEventLoop>::create();
124 mWaitLoop = loop;
125 loop->exec(QEventLoop::ExcludeUserInputEvents);
126 }
127
128 private:
129 bool mComplete;
130 QWeakPointer<QEventLoop> mWaitLoop;
131 QSharedPointer<ResultEmitter<T> > mEmitter;
132 };
88} 133}
89 134
90namespace Akonadi2 { 135namespace Akonadi2 {