!837 Feature:add sync call function

Merge pull request !837 from blue sky/sync_call
This commit is contained in:
openharmony_ci 2023-08-21 10:56:56 +00:00 committed by Gitee
commit 6c0e20f64c
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 68 additions and 21 deletions

View File

@ -30,6 +30,10 @@ namespace RelationalStoreJsKit {
using InputAction = std::function<void(napi_env, size_t, napi_value *, napi_value)>;
using OutputAction = std::function<void(napi_env, napi_value &)>;
using ExecuteAction = std::function<int()>;
static bool async = true;
static bool sync = !async;
#define ASYNC &async
#define SYNC &sync
class Context {
public:
@ -39,6 +43,7 @@ public:
virtual ~Context();
napi_env env_ = nullptr;
bool isAsync_ = true;
void *boundObj = nullptr;
std::shared_ptr<Error> error;
@ -50,6 +55,7 @@ public:
int execCode_ = OK;
OutputAction output_ = nullptr;
ExecuteAction exec_ = nullptr;
napi_value result_ = nullptr;
std::shared_ptr<Context> keep_;
};
@ -60,8 +66,12 @@ public:
private:
enum { ARG_ERROR, ARG_DATA, ARG_BUTT };
static void OnExecute(napi_env env, void *data);
static void OnComplete(napi_env env, void *data);
static void OnReturn(napi_env env, napi_status status, void *data);
static void OnComplete(napi_env env, napi_status status, void *data);
static void SetBusinessError(napi_env env, std::shared_ptr<Error> error, napi_value *businessError);
static napi_value Async(napi_env env, std::shared_ptr<Context> context);
static napi_value Sync(napi_env env, std::shared_ptr<Context> context);
};
} // namespace RelationalStoreJsKit
} // namespace OHOS

View File

@ -17,6 +17,7 @@
#include "logger.h"
#include "napi_rdb_trace.h"
#include "rdb_errno.h"
using namespace OHOS::Rdb;
using namespace OHOS::AppDataMgrJsKit;
@ -30,7 +31,8 @@ void Context::SetAction(
size_t argc = MAX_INPUT_COUNT;
napi_value self = nullptr;
napi_value argv[MAX_INPUT_COUNT] = { nullptr };
NAPI_CALL_RETURN_VOID(env, napi_get_cb_info(env, info, &argc, argv, &self, nullptr));
void *data = nullptr;
NAPI_CALL_RETURN_VOID(env, napi_get_cb_info(env, info, &argc, argv, &self, &data));
napi_valuetype valueType = napi_undefined;
napi_typeof(env, argv[argc - 1], &valueType);
@ -39,6 +41,10 @@ void Context::SetAction(
NAPI_CALL_RETURN_VOID(env, napi_create_reference(env, argv[argc - 1], 1, &callback_));
argc = argc - 1;
}
if (data) {
isAsync_ = *reinterpret_cast<bool *>(data);
}
// int -->input_(env, argc, argv, self)
input(env, argc, argv, self);
@ -104,6 +110,11 @@ void AsyncCall::SetBusinessError(napi_env env, std::shared_ptr<Error> error, nap
}
napi_value AsyncCall::Call(napi_env env, std::shared_ptr<Context> context)
{
return context->isAsync_ ? Async(env, context) : Sync(env, context);
}
napi_value AsyncCall::Async(napi_env env, std::shared_ptr<Context> context)
{
napi_value promise = nullptr;
if (context->callback_ == nullptr) {
@ -122,6 +133,13 @@ napi_value AsyncCall::Call(napi_env env, std::shared_ptr<Context> context)
return promise;
}
napi_value AsyncCall::Sync(napi_env env, std::shared_ptr<Context> context)
{
OnExecute(env, reinterpret_cast<void *>(context.get()));
OnComplete(env, reinterpret_cast<void *>(context.get()));
return context->result_;
}
void AsyncCall::OnExecute(napi_env env, void *data)
{
DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
@ -132,25 +150,35 @@ void AsyncCall::OnExecute(napi_env env, void *data)
context->exec_ = nullptr;
}
void AsyncCall::OnComplete(napi_env env, napi_status status, void *data)
void AsyncCall::OnComplete(napi_env env, void *data)
{
DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
Context *context = reinterpret_cast<Context *>(data);
if (context->execCode_ != NativeRdb::E_OK) {
context->SetError(std::make_shared<InnerError>(context->execCode_));
}
napi_value output = nullptr;
// if async execute status is not napi_ok then un-execute out function
if ((context->error == nullptr) && context->output_) {
context->output_(env, output);
context->output_(env, context->result_);
}
context->output_ = nullptr;
}
void AsyncCall::OnComplete(napi_env env, napi_status status, void *data)
{
DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
OnComplete(env, data);
OnReturn(env, status, data);
}
void AsyncCall::OnReturn(napi_env env, napi_status status, void *data)
{
Context *context = reinterpret_cast<Context *>(data);
napi_value result[ARG_BUTT] = { 0 };
// if out function status is ok then async renturn output data, else return error.
if (context->error == nullptr) {
napi_get_undefined(env, &result[ARG_ERROR]);
if (output != nullptr) {
result[ARG_DATA] = output;
if (context->result_ != nullptr) {
result[ARG_DATA] = context->result_;
} else {
napi_get_undefined(env, &result[ARG_DATA]);
}

View File

@ -129,24 +129,30 @@ bool IsNapiTypeString(napi_env env, size_t argc, napi_value *argv, size_t arg)
void RdbStoreProxy::Init(napi_env env, napi_value exports)
{
napi_property_descriptor descriptors[] = {
DECLARE_NAPI_FUNCTION("delete", Delete),
DECLARE_NAPI_FUNCTION("update", Update),
DECLARE_NAPI_FUNCTION("insert", Insert),
DECLARE_NAPI_FUNCTION("batchInsert", BatchInsert),
DECLARE_NAPI_FUNCTION("querySql", QuerySql),
DECLARE_NAPI_FUNCTION("query", Query),
#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM)
DECLARE_NAPI_FUNCTION("remoteQuery", RemoteQuery),
#endif
DECLARE_NAPI_FUNCTION("executeSql", ExecuteSql),
DECLARE_NAPI_FUNCTION("replace", Replace),
DECLARE_NAPI_FUNCTION_WITH_DATA("delete", Delete, ASYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("deleteSync", Delete, SYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("update", Update, ASYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("updateSync", Update, SYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("insert", Insert, ASYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("insertSync", Insert, SYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("batchInsert", BatchInsert, ASYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("batchInsertSync", BatchInsert, SYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("querySql", QuerySql, ASYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("querySqlSync", QuerySql, SYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("query", Query, ASYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("querySync", Query, SYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("executeSql", ExecuteSql, ASYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("executeSqlSync", ExecuteSql, SYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("replace", Replace, ASYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("replaceSync", Replace, SYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("queryByStep", QueryByStep, ASYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("queryByStepSync", QueryByStep, SYNC),
DECLARE_NAPI_FUNCTION("backup", Backup),
DECLARE_NAPI_FUNCTION("count", Count),
DECLARE_NAPI_FUNCTION("addAttach", Attach),
DECLARE_NAPI_FUNCTION("beginTransaction", BeginTransaction),
DECLARE_NAPI_FUNCTION("rollBack", RollBack),
DECLARE_NAPI_FUNCTION("commit", Commit),
DECLARE_NAPI_FUNCTION("queryByStep", QueryByStep),
DECLARE_NAPI_FUNCTION("restore", Restore),
DECLARE_NAPI_GETTER_SETTER("version", GetVersion, SetVersion),
DECLARE_NAPI_GETTER("isInTransaction", IsInTransaction),
@ -155,6 +161,7 @@ void RdbStoreProxy::Init(napi_env env, napi_value exports)
DECLARE_NAPI_GETTER("isReadOnly", IsReadOnly),
DECLARE_NAPI_GETTER("isMemoryRdb", IsMemoryRdb),
#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM)
DECLARE_NAPI_FUNCTION("remoteQuery", RemoteQuery),
DECLARE_NAPI_FUNCTION("setDistributedTables", SetDistributedTables),
DECLARE_NAPI_FUNCTION("obtainDistributedTableName", ObtainDistributedTableName),
DECLARE_NAPI_FUNCTION("sync", Sync),

View File

@ -260,8 +260,10 @@ napi_value InitRdbHelper(napi_env env, napi_value exports)
{
LOG_INFO("RelationalStoreJsKit::InitRdbHelper begin");
napi_property_descriptor properties[] = {
DECLARE_NAPI_FUNCTION("getRdbStore", GetRdbStore),
DECLARE_NAPI_FUNCTION("deleteRdbStore", DeleteRdbStore),
DECLARE_NAPI_FUNCTION_WITH_DATA("getRdbStore", GetRdbStore, ASYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("getRdbStoreSync", GetRdbStore, SYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("deleteRdbStore", DeleteRdbStore, ASYNC),
DECLARE_NAPI_FUNCTION_WITH_DATA("deleteRdbStoreSync", DeleteRdbStore, SYNC),
};
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(*properties), properties));
LOG_INFO("RelationalStoreJsKit::InitRdbHelper end");