mirror of
https://github.com/simonbrunel/qtpromise.git
synced 2025-07-05 08:45:23 +08:00
Implement QPromise::timeout(msec, error)
This commit is contained in:
@ -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;
|
||||
|
||||
|
@ -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
|
||||
{
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user