diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-04-13 20:15:14 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-04-15 09:30:32 +0200 |
commit | c55054e899660f2d667af2c2e573a1267d47358e (patch) | |
tree | 0f547effcad0c20521f0bc047a9eb1d4130b052b /common/synclistresult.h | |
parent | 4652a39fc6869fc5af46367c35027b2b53478268 (diff) | |
download | sink-c55054e899660f2d667af2c2e573a1267d47358e.tar.gz sink-c55054e899660f2d667af2c2e573a1267d47358e.zip |
Use a queryrunner to execute queries.
The queryrunner is responsible for running queries and keeping them
up to date. This is required for self-updating queries.
To get this to work properly the ResultProvider/emitter had to be fixed.
The emitter now only lives as long as the client holds a reference to
it, allowing the provider to detect when it is no longer necessary to
keep the query alive (because noone is listening).
In the process various lifetime issues have been fixed, that we're
caused by lambdas capturing smartpointers, that then extended the
lifetime of the associated objects unpredictably.
Diffstat (limited to 'common/synclistresult.h')
-rw-r--r-- | common/synclistresult.h | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/common/synclistresult.h b/common/synclistresult.h new file mode 100644 index 0000000..5fa0efd --- /dev/null +++ b/common/synclistresult.h | |||
@@ -0,0 +1,54 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include <QList> | ||
4 | #include <functional> | ||
5 | #include <QSharedPointer> | ||
6 | #include <clientapi.h> | ||
7 | |||
8 | namespace async { | ||
9 | |||
10 | /* | ||
11 | * A result set specialization that provides a syncronous list. | ||
12 | * | ||
13 | * Only for testing purposes. | ||
14 | * | ||
15 | * WARNING: The nested eventloop can cause all sorts of trouble. Use only in testing code. | ||
16 | */ | ||
17 | template<class T> | ||
18 | class SyncListResult : public QList<T> { | ||
19 | public: | ||
20 | SyncListResult(const QSharedPointer<ResultEmitter<T> > &emitter) | ||
21 | :QList<T>(), | ||
22 | mComplete(false), | ||
23 | mEmitter(emitter) | ||
24 | { | ||
25 | emitter->onAdded([this](const T &value) { | ||
26 | this->append(value); | ||
27 | }); | ||
28 | emitter->onComplete([this]() { | ||
29 | mComplete = true; | ||
30 | if (eventLoopAborter) { | ||
31 | eventLoopAborter(); | ||
32 | //Be safe in case of a second invocation of the complete handler | ||
33 | eventLoopAborter = std::function<void()>(); | ||
34 | } | ||
35 | }); | ||
36 | emitter->onClear([this]() { | ||
37 | this->clear(); | ||
38 | }); | ||
39 | } | ||
40 | |||
41 | void exec() | ||
42 | { | ||
43 | QEventLoop eventLoop; | ||
44 | eventLoopAborter = [&eventLoop]() { eventLoop.quit(); }; | ||
45 | eventLoop.exec(); | ||
46 | } | ||
47 | |||
48 | private: | ||
49 | bool mComplete; | ||
50 | QSharedPointer<ResultEmitter<T> > mEmitter; | ||
51 | std::function<void()> eventLoopAborter; | ||
52 | }; | ||
53 | |||
54 | } | ||