Implement QPromise::timeout(msec, error)

This commit is contained in:
Simon Brunel
2017-09-02 12:23:42 +02:00
parent b47ca0569e
commit 18324d3f44
4 changed files with 109 additions and 0 deletions

View File

@ -46,6 +46,9 @@ public:
template <typename THandler>
inline QPromise<T> tap(THandler handler) const;
template <typename E = QPromiseTimeoutException>
inline QPromise<T> timeout(int msec, E&& error = E()) const;
inline QPromise<T> delay(int msec) const;
inline QPromise<T> wait() const;

View File

@ -150,6 +150,26 @@ inline QPromise<T> QPromiseBase<T>::tap(THandler handler) const
});
}
template <typename T>
template <typename E>
inline QPromise<T> QPromiseBase<T>::timeout(int msec, E&& error) const
{
QPromise<T> p = *this;
return QPromise<T>([&](
const QPromiseResolve<T>& resolve,
const QPromiseReject<T>& reject) {
QTimer::singleShot(msec, [=]() {
// we don't need to verify the current promise state, reject()
// takes care of checking if the promise is already resolved,
// and thus will ignore this rejection.
reject(std::move(error));
});
QtPromisePrivate::PromiseFulfill<QPromise<T> >::call(p, resolve, reject);
});
}
template <typename T>
inline QPromise<T> QPromiseBase<T>::delay(int msec) const
{

View File

@ -4,6 +4,9 @@
// QtPromise
#include "qpromiseglobal.h"
// Qt
#include <QException>
namespace QtPromise {
class QPromiseError
@ -53,6 +56,16 @@ private:
std::exception_ptr m_exception;
};
class QPromiseTimeoutException : public QException
{
public:
void raise() const Q_DECL_OVERRIDE { throw *this; }
QPromiseTimeoutException* clone() const Q_DECL_OVERRIDE
{
return new QPromiseTimeoutException(*this);
}
};
} // namespace QtPromise
#endif // QTPROMISE_QPROMISEERROR_H