fix json value crash & timer not work

This commit is contained in:
王劲鹏 2021-04-06 17:58:23 +08:00 committed by osborn
parent fc0af6e992
commit c01bd1e98c
5 changed files with 36 additions and 22 deletions

View File

@ -11,8 +11,6 @@ DoricBridgeExtension::DoricBridgeExtension(QObject *parent) : QObject(parent) {}
void DoricBridgeExtension::callNative(QString contextId, QString module, void DoricBridgeExtension::callNative(QString contextId, QString module,
QString methodName, QString callbackId, QString methodName, QString callbackId,
QString argument) { QString argument) {
QJsonDocument document = QJsonDocument::fromJson(argument.toUtf8());
QJsonObject jsValue = document.object();
DoricContext *context = DoricContext *context =
DoricContextManager::getInstance()->getContext(contextId); DoricContextManager::getInstance()->getContext(contextId);
bool classRegistered = bool classRegistered =
@ -21,7 +19,7 @@ void DoricBridgeExtension::callNative(QString contextId, QString module,
QObject *plugin = context->obtainPlugin(module); QObject *plugin = context->obtainPlugin(module);
QMetaObject::invokeMethod(plugin, methodName.toUtf8(), Qt::DirectConnection, QMetaObject::invokeMethod(plugin, methodName.toUtf8(), Qt::DirectConnection,
QGenericReturnArgument(), QGenericReturnArgument(),
Q_ARG(QJsonObject *, &jsValue), Q_ARG(QString, argument),
Q_ARG(QString, callbackId)); Q_ARG(QString, callbackId));
} }
qDebug() << "contextId: " + contextId << "module: " + module qDebug() << "contextId: " + contextId << "module: " + module

View File

@ -1,3 +1,4 @@
#include <QCoreApplication>
#include <QTimer> #include <QTimer>
#include "../utils/DoricConstant.h" #include "../utils/DoricConstant.h"
@ -5,22 +6,29 @@
Q_INVOKABLE void DoricTimerExtension::setTimer(long timerId, int time, Q_INVOKABLE void DoricTimerExtension::setTimer(long timerId, int time,
bool repeat) { bool repeat) {
QTimer *timer = new QTimer(this);
connect(this, &DoricTimerExtension::startTimer, qApp, [=]() {
QTimer *timer = new QTimer();
timer->setSingleShot(!repeat); timer->setSingleShot(!repeat);
connect(timer, &QTimer::timeout, this, [=]() {
connect(timer, &QTimer::timeout, [=]() {
if (deletedTimerIds->contains(timerId)) { if (deletedTimerIds->contains(timerId)) {
deletedTimerIds->remove(timerId); deletedTimerIds->remove(timerId);
delete timer; timer->deleteLater();
} else { } else {
this->method(timerId); this->method(timerId);
if (!repeat) { if (!repeat) {
deletedTimerIds->remove(timerId); deletedTimerIds->remove(timerId);
delete timer; timer->deleteLater();
} }
} }
}); });
timer->start(time); timer->start(time);
});
emit startTimer();
} }
Q_INVOKABLE void DoricTimerExtension::clearTimer(long timerId) { Q_INVOKABLE void DoricTimerExtension::clearTimer(long timerId) {

View File

@ -21,5 +21,8 @@ public:
Q_INVOKABLE void setTimer(long timerId, int time, bool repeat); Q_INVOKABLE void setTimer(long timerId, int time, bool repeat);
Q_INVOKABLE void clearTimer(long timerId); Q_INVOKABLE void clearTimer(long timerId);
signals:
void startTimer();
}; };
#endif // NATIVETIMER_H #endif // NATIVETIMER_H

View File

@ -1,24 +1,29 @@
#include <QDebug> #include <QDebug>
#include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
#include "../shader/DoricRootNode.h" #include "../shader/DoricRootNode.h"
#include "DoricShaderPlugin.h" #include "DoricShaderPlugin.h"
void DoricShaderPlugin::render(QJsonObject *jsValue, QString callbackId) { void DoricShaderPlugin::render(QString jsValueString, QString callbackId) {
getContext()->getDriver()->asyncCall( getContext()->getDriver()->asyncCall(
[this, jsValue] { [this, jsValueString] {
try { try {
QString viewId = jsValue->value("id").toString(); QJsonDocument document =
QJsonDocument::fromJson(jsValueString.toUtf8());
QJsonValue jsValue = document.object();
QString viewId = jsValue["id"].toString();
DoricRootNode *rootNode = getContext()->getRootNode(); DoricRootNode *rootNode = getContext()->getRootNode();
if (rootNode->getId().isEmpty() && if (rootNode->getId().isEmpty() &&
jsValue->value("type").toString() == "Root") { jsValue["type"].toString() == "Root") {
rootNode->setId(viewId); rootNode->setId(viewId);
rootNode->blend(jsValue->value("props")); rootNode->blend(jsValue["props"]);
} else { } else {
DoricViewNode *viewNode = getContext()->targetViewNode(viewId); DoricViewNode *viewNode = getContext()->targetViewNode(viewId);
if (viewNode != nullptr) { if (viewNode != nullptr) {
viewNode->blend(jsValue->value("props")); viewNode->blend(jsValue["props"]);
} }
} }
} catch (...) { } catch (...) {

View File

@ -11,7 +11,7 @@ class DoricShaderPlugin : public DoricNativePlugin {
public: public:
using DoricNativePlugin::DoricNativePlugin; using DoricNativePlugin::DoricNativePlugin;
Q_INVOKABLE void render(QJsonObject *jsValue, QString callbackId); Q_INVOKABLE void render(QString jsValueString, QString callbackId);
}; };
#endif // SHADERPLUGIN_H #endif // SHADERPLUGIN_H