complete pure call
This commit is contained in:
@@ -1,13 +1,31 @@
|
||||
#include "DoricAsyncResult.h"
|
||||
|
||||
#include <QWaitCondition>
|
||||
|
||||
DoricAsyncResult::DoricAsyncResult() {}
|
||||
|
||||
void DoricAsyncResult::setResult(QString result) { this->result = result; }
|
||||
void DoricAsyncResult::setResult(QString result) {
|
||||
this->result = result;
|
||||
this->resultCallback();
|
||||
}
|
||||
|
||||
void DoricAsyncResult::setError(QString exception) { this->result = exception; }
|
||||
|
||||
bool DoricAsyncResult::hasResult() { return !(result == EMPTY); }
|
||||
bool DoricAsyncResult::hasResult() { return !(result.isEmpty()); }
|
||||
|
||||
QJSValue DoricAsyncResult::getResult() { return this->result; }
|
||||
QString DoricAsyncResult::getResult() { return this->result; }
|
||||
|
||||
DoricSettableFuture *DoricAsyncResult::synchronous() { return NULL; }
|
||||
QString DoricAsyncResult::waitUntilResult() {
|
||||
if (hasResult()) {
|
||||
return this->result;
|
||||
}
|
||||
|
||||
QMutex mutex;
|
||||
QWaitCondition condition;
|
||||
this->resultCallback = [&condition]() { condition.wakeAll(); };
|
||||
|
||||
mutex.lock();
|
||||
condition.wait(&mutex);
|
||||
mutex.unlock();
|
||||
return this->result;
|
||||
}
|
||||
|
@@ -1,17 +1,13 @@
|
||||
#ifndef ASYNCRESULT_H
|
||||
#define ASYNCRESULT_H
|
||||
|
||||
#include <QJSValue>
|
||||
#include <QString>
|
||||
|
||||
#include "DoricExport.h"
|
||||
|
||||
#include "DoricSettableFuture.h"
|
||||
|
||||
static QString EMPTY("");
|
||||
|
||||
class DORIC_EXPORT DoricAsyncResult {
|
||||
private:
|
||||
QString result = EMPTY;
|
||||
QString result;
|
||||
|
||||
public:
|
||||
std::function<void()> resultCallback;
|
||||
@@ -26,9 +22,9 @@ public:
|
||||
|
||||
bool hasResult();
|
||||
|
||||
QJSValue getResult();
|
||||
QString getResult();
|
||||
|
||||
DoricSettableFuture *synchronous();
|
||||
QString waitUntilResult();
|
||||
};
|
||||
|
||||
#endif // ASYNCRESULT_H
|
||||
|
@@ -1,17 +0,0 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "DoricSettableFuture.h"
|
||||
|
||||
void DoricSettableFuture::set(QJSValue result) {
|
||||
if (mReadyLatch == NULL) {
|
||||
qDebug() << "Result has already been set!";
|
||||
return;
|
||||
}
|
||||
mResult = result;
|
||||
delete mReadyLatch;
|
||||
}
|
||||
|
||||
QJSValue DoricSettableFuture::get() {
|
||||
mReadyLatch->lock();
|
||||
return mResult;
|
||||
}
|
@@ -1,21 +0,0 @@
|
||||
#ifndef SETTABLE_FUTURE_H
|
||||
#define SETTABLE_FUTURE_H
|
||||
|
||||
#include <QJSValue>
|
||||
|
||||
#include "DoricExport.h"
|
||||
|
||||
#include "utils/DoricCountDownLatch.h"
|
||||
|
||||
class DORIC_EXPORT DoricSettableFuture {
|
||||
private:
|
||||
QJSValue mResult;
|
||||
DoricCountDownLatch *mReadyLatch = new DoricCountDownLatch();
|
||||
|
||||
public:
|
||||
void set(QJSValue result);
|
||||
|
||||
QJSValue get();
|
||||
};
|
||||
|
||||
#endif // SETTABLE_FUTURE_H
|
Reference in New Issue
Block a user