add shared ptr wrap for async result

This commit is contained in:
王劲鹏 2021-05-12 11:08:40 +08:00 committed by osborn
parent 3531a28f13
commit 33bec666a4
7 changed files with 54 additions and 33 deletions

View File

@ -54,7 +54,8 @@ void DoricContext::build(int width, int height) {
callEntity(DoricConstant::DORIC_ENTITY_BUILD, args);
}
void DoricContext::callEntity(QString methodName, QVariantList args) {
std::shared_ptr<DoricAsyncResult> DoricContext::callEntity(QString methodName,
QVariantList args) {
return getDriver()->invokeContextEntityMethod(this->mContextId, methodName,
args);
}

View File

@ -34,7 +34,8 @@ public:
void build(int width, int height);
void callEntity(QString methodName, QVariantList args);
std::shared_ptr<DoricAsyncResult> callEntity(QString methodName,
QVariantList args);
DoricInterfaceDriver *getDriver();

View File

@ -11,18 +11,21 @@
class DoricInterfaceDriver {
public:
virtual void invokeContextEntityMethod(QString contextId, QString method,
virtual std::shared_ptr<DoricAsyncResult>
invokeContextEntityMethod(QString contextId, QString method,
QVariantList args) = 0;
virtual void invokeDoricMethod(QString method, QVariantList args) = 0;
virtual std::shared_ptr<DoricAsyncResult>
invokeDoricMethod(QString method, QVariantList args) = 0;
virtual DoricAsyncResult *asyncCall(std::function<void()> lambda,
DoricThreadMode mode) = 0;
virtual std::shared_ptr<DoricAsyncResult>
asyncCall(std::function<void()> lambda, DoricThreadMode mode) = 0;
virtual void createContext(QString contextId, QString script,
QString source) = 0;
virtual std::shared_ptr<DoricAsyncResult>
createContext(QString contextId, QString script, QString source) = 0;
virtual void destroyContext(QString contextId) = 0;
virtual std::shared_ptr<DoricAsyncResult>
destroyContext(QString contextId) = 0;
virtual DoricRegistry *getRegistry() = 0;
};

View File

@ -4,21 +4,25 @@
#include "async/DoricAsyncCall.h"
#include "utils/DoricConstant.h"
void DoricNativeDriver::invokeContextEntityMethod(QString contextId,
QString method,
std::shared_ptr<DoricAsyncResult>
DoricNativeDriver::invokeContextEntityMethod(QString contextId, QString method,
QVariantList args) {
args.insert(0, QVariant(contextId));
args.insert(1, QVariant(method));
invokeDoricMethod(DoricConstant::DORIC_CONTEXT_INVOKE, args);
return invokeDoricMethod(DoricConstant::DORIC_CONTEXT_INVOKE, args);
}
void DoricNativeDriver::invokeDoricMethod(QString method, QVariantList args) {
return DoricAsyncCall::ensureRunInThreadPool(
std::shared_ptr<DoricAsyncResult>
DoricNativeDriver::invokeDoricMethod(QString method, QVariantList args) {
DoricAsyncCall::ensureRunInThreadPool(
&jsEngine.mJSThreadPool,
[this, method, args] { this->jsEngine.invokeDoricMethod(method, args); });
return std::make_shared<DoricAsyncResult>();
}
DoricAsyncResult *DoricNativeDriver::asyncCall(std::function<void()> lambda,
std::shared_ptr<DoricAsyncResult>
DoricNativeDriver::asyncCall(std::function<void()> lambda,
DoricThreadMode mode) {
switch (mode) {
case UI:
@ -31,18 +35,24 @@ DoricAsyncResult *DoricNativeDriver::asyncCall(std::function<void()> lambda,
return NULL;
}
void DoricNativeDriver::createContext(QString contextId, QString script,
std::shared_ptr<DoricAsyncResult>
DoricNativeDriver::createContext(QString contextId, QString script,
QString source) {
DoricAsyncCall::ensureRunInThreadPool(
&jsEngine.mJSThreadPool, [this, contextId, script, source] {
this->jsEngine.prepareContext(contextId, script, source);
});
return std::make_shared<DoricAsyncResult>();
}
void DoricNativeDriver::destroyContext(QString contextId) {
std::shared_ptr<DoricAsyncResult>
DoricNativeDriver::destroyContext(QString contextId) {
DoricAsyncCall::ensureRunInThreadPool(
&jsEngine.mJSThreadPool,
[this, contextId] { this->jsEngine.destroyContext(contextId); });
return std::make_shared<DoricAsyncResult>();
}
DoricRegistry *DoricNativeDriver::getRegistry() {

View File

@ -21,18 +21,20 @@ public:
return &instance;
}
void invokeContextEntityMethod(QString contextId, QString method,
std::shared_ptr<DoricAsyncResult>
invokeContextEntityMethod(QString contextId, QString method,
QVariantList args) override;
void invokeDoricMethod(QString method, QVariantList args) override;
std::shared_ptr<DoricAsyncResult>
invokeDoricMethod(QString method, QVariantList args) override;
DoricAsyncResult *asyncCall(std::function<void()> lambda,
std::shared_ptr<DoricAsyncResult> asyncCall(std::function<void()> lambda,
DoricThreadMode mode) override;
void createContext(QString contextId, QString script,
QString source) override;
std::shared_ptr<DoricAsyncResult>
createContext(QString contextId, QString script, QString source) override;
void destroyContext(QString contextId) override;
std::shared_ptr<DoricAsyncResult> destroyContext(QString contextId) override;
DoricRegistry *getRegistry() override;
};

View File

@ -208,7 +208,8 @@ QList<QString> DoricViewNode::getIdList() {
void DoricViewNode::requestLayout() {}
void DoricViewNode::callJSResponse(QString funcId, QVariantList args) {
std::shared_ptr<DoricAsyncResult>
DoricViewNode::callJSResponse(QString funcId, QVariantList args) {
QVariantList nArgs;
QList<QString> idList = getIdList();
nArgs.append(QVariant(idList));
@ -219,7 +220,8 @@ void DoricViewNode::callJSResponse(QString funcId, QVariantList args) {
return getContext()->callEntity(DoricConstant::DORIC_ENTITY_RESPONSE, nArgs);
}
void DoricViewNode::pureCallJSResponse(QString funcId, QVariantList args) {
std::shared_ptr<DoricAsyncResult>
DoricViewNode::pureCallJSResponse(QString funcId, QVariantList args) {
QVariantList nArgs;
nArgs.append(getContext()->getContextId());
nArgs.append(DoricConstant::DORIC_ENTITY_RESPONSE);

View File

@ -77,8 +77,10 @@ public:
void onClick();
void callJSResponse(QString funcId, QVariantList args);
std::shared_ptr<DoricAsyncResult> callJSResponse(QString funcId,
QVariantList args);
void pureCallJSResponse(QString funcId, QVariantList args);
std::shared_ptr<DoricAsyncResult> pureCallJSResponse(QString funcId,
QVariantList args);
};
#endif // DORICVIEWNODE_H