-
Notifications
You must be signed in to change notification settings - Fork 2
/
promise.h
95 lines (81 loc) · 2.89 KB
/
promise.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef PROMISE_H
#define PROMISE_H
#include <QtCore>
/// private, don't use directly
class BasePromise : public QObject {
Q_OBJECT
public:
explicit BasePromise(QObject *parent = nullptr) : QObject(parent) {}
signals:
void resolve();
void reject(const QString &message);
};
template <class T> class Promise : public BasePromise {
public:
explicit Promise(QObject *parent = nullptr) : BasePromise(parent) {
connect(this, &BasePromise::resolve, this, &QObject::deleteLater);
connect(this, &BasePromise::reject, this, &QObject::deleteLater);
}
template <typename Function> Promise<T> &then(Function func) {
connect(this, &BasePromise::resolve, this, [this, func] { func(data); });
return *this;
}
template <typename Function> Promise<T> &onFailed(Function func) {
connect(this, &BasePromise::reject, this, func);
return *this;
}
template <typename Function> Promise<T> &finally(Function func) {
connect(this, &BasePromise::destroyed, this, func);
return *this;
}
void resolve(T value) {
data = value;
BasePromise::resolve();
}
T result() const { return data; }
static Promise<QList<T>> &all(QList<Promise<T> *> promises) {
QPointer<Promise<QList<T>>> promise = new Promise<QList<T>>;
auto count = new int(promises.size());
auto results = new QList<T>;
results->reserve(promises.size());
for (auto p : promises) {
p.then([count, results, promise, p](auto result) {
if (!promise) return;
results << result;
if (results->size() == count) {
promise->resolve(results);
delete count, delete results;
}
}).onFailed([count, results, promise](auto msg) {
if (!promise) return;
promise->reject(msg);
delete count, delete results;
});
}
return *promise;
}
static Promise<QList<T>> &allSettled(QList<Promise<T> *> promises) {
QPointer<Promise<QList<T>>> promise = new Promise<QList<T>>;
auto count = new int(promises.size());
auto outcomes = new QList<T>;
outcomes->reserve(promises.size());
for (auto p : promises) {
p.then([count, outcomes, promise, p](auto result) {
if (!promise) return;
outcomes << result;
if (outcomes->size() == count) {
promise->resolve(outcomes);
delete count, delete outcomes;
}
}).onFailed([count, outcomes, promise](auto msg) {
if (!promise) return;
promise->reject(msg);
delete count, delete outcomes;
});
}
return *promise;
}
private:
T data;
};
#endif // PROMISE_H