complete pure call

This commit is contained in:
王劲鹏 2021-05-31 16:00:06 +08:00 committed by osborn
parent 9f721aa1c8
commit 4c5cd39320
12 changed files with 66 additions and 68 deletions

View File

@ -59,15 +59,18 @@ void DoricDemoBridge::navigate(QVariant route) {
name = "SimpleDemo.js"; name = "SimpleDemo.js";
break; break;
case 14: case 14:
name = "Snake.js"; name = "SliderDemo.js";
break; break;
case 15: case 15:
name = "StorageDemo.js"; name = "Snake.js";
break; break;
case 16: case 16:
name = "SwitchDemo.js"; name = "StorageDemo.js";
break; break;
case 17: case 17:
name = "SwitchDemo.js";
break;
case 18:
name = "TextDemo.js"; name = "TextDemo.js";
break; break;
} }

View File

@ -15,7 +15,7 @@ ApplicationWindow {
ListView { ListView {
width: parent.width width: parent.width
model: 18 model: 19
delegate: Rectangle { delegate: Rectangle {
Column { Column {
anchors.centerIn: parent anchors.centerIn: parent
@ -51,12 +51,14 @@ ApplicationWindow {
case 13: case 13:
return "SimpleDemo.js" return "SimpleDemo.js"
case 14: case 14:
return "Snake.js" return "SliderDemo.js"
case 15: case 15:
return "StorageDemo.js" return "Snake.js"
case 16: case 16:
return "SwitchDemo.js" return "StorageDemo.js"
case 17: case 17:
return "SwitchDemo.js"
case 18:
return "TextDemo.js" return "TextDemo.js"
} }
} }

View File

@ -21,8 +21,9 @@
<file alias="ModularDemo.js">../../../doric-demo/bundle/src/ModularDemo.js</file> <file alias="ModularDemo.js">../../../doric-demo/bundle/src/ModularDemo.js</file>
<file alias="NetworkDemo.js">../../../doric-demo/bundle/src/NetworkDemo.js</file> <file alias="NetworkDemo.js">../../../doric-demo/bundle/src/NetworkDemo.js</file>
<file alias="PopoverDemo.js">../../../doric-demo/bundle/src/PopoverDemo.js</file> <file alias="PopoverDemo.js">../../../doric-demo/bundle/src/PopoverDemo.js</file>
<file alias="SimpleDemo.js">../../../doric-demo/bundle/src/SimpleDemo.js</file>
<file alias="ScrollerDemo.js">../../../doric-demo/bundle/src/ScrollerDemo.js</file> <file alias="ScrollerDemo.js">../../../doric-demo/bundle/src/ScrollerDemo.js</file>
<file alias="SimpleDemo.js">../../../doric-demo/bundle/src/SimpleDemo.js</file>
<file alias="SliderDemo.js">../../../doric-demo/bundle/src/SliderDemo.js</file>
<file alias="Snake.js">../../../doric-demo/bundle/src/Snake.js</file> <file alias="Snake.js">../../../doric-demo/bundle/src/Snake.js</file>
<file alias="StorageDemo.js">../../../doric-demo/bundle/src/StorageDemo.js</file> <file alias="StorageDemo.js">../../../doric-demo/bundle/src/StorageDemo.js</file>
<file alias="SwitchDemo.js">../../../doric-demo/bundle/src/SwitchDemo.js</file> <file alias="SwitchDemo.js">../../../doric-demo/bundle/src/SwitchDemo.js</file>

View File

@ -1,13 +1,31 @@
#include "DoricAsyncResult.h" #include "DoricAsyncResult.h"
#include <QWaitCondition>
DoricAsyncResult::DoricAsyncResult() {} 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; } 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;
}

View File

@ -1,17 +1,13 @@
#ifndef ASYNCRESULT_H #ifndef ASYNCRESULT_H
#define ASYNCRESULT_H #define ASYNCRESULT_H
#include <QJSValue> #include <QString>
#include "DoricExport.h" #include "DoricExport.h"
#include "DoricSettableFuture.h"
static QString EMPTY("");
class DORIC_EXPORT DoricAsyncResult { class DORIC_EXPORT DoricAsyncResult {
private: private:
QString result = EMPTY; QString result;
public: public:
std::function<void()> resultCallback; std::function<void()> resultCallback;
@ -26,9 +22,9 @@ public:
bool hasResult(); bool hasResult();
QJSValue getResult(); QString getResult();
DoricSettableFuture *synchronous(); QString waitUntilResult();
}; };
#endif // ASYNCRESULT_H #endif // ASYNCRESULT_H

View File

@ -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;
}

View File

@ -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

View File

@ -26,7 +26,6 @@ SOURCES += \
DoricPanel.cpp \ DoricPanel.cpp \
DoricRegistry.cpp \ DoricRegistry.cpp \
async/DoricAsyncResult.cpp \ async/DoricAsyncResult.cpp \
async/DoricSettableFuture.cpp \
engine/DoricBridgeExtension.cpp \ engine/DoricBridgeExtension.cpp \
engine/DoricJSEngine.cpp \ engine/DoricJSEngine.cpp \
engine/DoricNativeEmpty.cpp \ engine/DoricNativeEmpty.cpp \
@ -105,7 +104,6 @@ HEADERS += \
DoricRegistry.h \ DoricRegistry.h \
async/DoricAsyncCall.h \ async/DoricAsyncCall.h \
async/DoricAsyncResult.h \ async/DoricAsyncResult.h \
async/DoricSettableFuture.h \
engine/DoricBridgeExtension.h \ engine/DoricBridgeExtension.h \
engine/DoricInterfaceJSE.h \ engine/DoricInterfaceJSE.h \
engine/DoricJSEngine.h \ engine/DoricJSEngine.h \

View File

@ -1,6 +1,8 @@
import QtQuick 2.12 import QtQuick 2.12
import QtQuick.Controls 2.5 import QtQuick.Controls 2.5
import "util.mjs" as Util
SwipeView { SwipeView {
property var wrapper property var wrapper

View File

@ -232,8 +232,8 @@ DoricViewNode::pureCallJSResponse(QString funcId, QVariantList args) {
foreach (const QVariant &arg, args) foreach (const QVariant &arg, args)
nArgs.append(arg); nArgs.append(arg);
return getContext()->callEntity(DoricConstant::DORIC_CONTEXT_INVOKE_PURE, return getContext()->getDriver()->invokeDoricMethod(
nArgs); DoricConstant::DORIC_CONTEXT_INVOKE_PURE, nArgs);
} }
void DoricViewNode::onClick() { void DoricViewNode::onClick() {

View File

@ -32,15 +32,33 @@ void DoricSliderNode::blendSubNode(QJsonValue subProperties) {
} }
} }
void DoricSliderNode::blend(QJsonValue jsValue) {} void DoricSliderNode::blend(QQuickItem *view, QString name, QJsonValue prop) {
if (name == "itemCount") {
void DoricSliderNode::blend(QQuickItem *view, QString name, QJsonValue prop) {} this->itemCount = prop.toInt();
} else if (name == "renderPage") {
if (prop.toString() != this->renderPageFuncId) {
this->childNodes.clear();
this->renderPageFuncId = prop.toString();
}
} else if (name == "batchCount") {
this->batchCount = prop.toInt();
} else if (name == "onPageSlided") {
this->onPageSelectedFuncId = prop.toString();
} else if (name == "loop") {
this->loop = prop.toBool();
} else {
DoricViewNode::blend(view, name, prop);
}
}
void DoricSliderNode::afterBlended(QJsonValue prop) { void DoricSliderNode::afterBlended(QJsonValue prop) {
if (this->childNodes.length() != this->itemCount) { if (this->childNodes.length() != this->itemCount) {
QVariantList args; QVariantList args;
args.append(this->childNodes.length()); args.append(this->childNodes.length());
args.append(this->itemCount); args.append(this->itemCount);
this->pureCallJSResponse("renderBunchedItems", args); std::shared_ptr<DoricAsyncResult> asyncResult =
this->pureCallJSResponse("renderBunchedItems", args);
QString result = asyncResult->waitUntilResult();
qDebug() << result;
} }
} }

View File

@ -25,8 +25,6 @@ public:
virtual void blendSubNode(QJsonValue subProperties) override; virtual void blendSubNode(QJsonValue subProperties) override;
virtual void blend(QJsonValue jsValue) override;
virtual void blend(QQuickItem *view, QString name, QJsonValue prop) override; virtual void blend(QQuickItem *view, QString name, QJsonValue prop) override;
virtual void afterBlended(QJsonValue prop) override; virtual void afterBlended(QJsonValue prop) override;