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); 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, return getDriver()->invokeContextEntityMethod(this->mContextId, methodName,
args); args);
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -77,8 +77,10 @@ public:
void onClick(); 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 #endif // DORICVIEWNODE_H