mirror of
https://gitee.com/openharmony/applications_settings
synced 2024-11-26 16:10:49 +00:00
commit
6d48d4dfb3
@ -36,20 +36,17 @@ namespace Settings {
|
||||
|
||||
std::map<std::string, sptr<SettingsObserver>> g_observerMap;
|
||||
|
||||
void SettingsObserver::OnChange()
|
||||
void SettingsObserver::EnvObserver(void* arg)
|
||||
{
|
||||
uv_loop_s* loop = nullptr;
|
||||
napi_get_uv_event_loop(cbInfo->env, &loop);
|
||||
if (loop == nullptr) {
|
||||
SETTING_LOG_ERROR("%{public}s, fail to get uv loop.", __func__);
|
||||
AsyncCallbackInfo* callBackInfo = reinterpret_cast<AsyncCallbackInfo*>(arg);
|
||||
if (callBackInfo == nullptr || callBackInfo->env == nullptr) {
|
||||
return;
|
||||
}
|
||||
auto work = new (std::nothrow) uv_work_t;
|
||||
if (work == nullptr) {
|
||||
SETTING_LOG_ERROR("%{public}s, fail to get uv work.", __func__);
|
||||
return;
|
||||
}
|
||||
work->data = reinterpret_cast<void*>(cbInfo);
|
||||
callBackInfo->env = nullptr;
|
||||
}
|
||||
|
||||
int OnChangeAsync(uv_loop_s* loop, uv_work_t *work)
|
||||
{
|
||||
int ret = uv_queue_work(loop, work, [](uv_work_t *work) {},
|
||||
[](uv_work_t *work, int status) {
|
||||
AsyncCallbackInfo* cbInfo = reinterpret_cast<AsyncCallbackInfo*>(work->data);
|
||||
@ -58,6 +55,10 @@ namespace Settings {
|
||||
delete work;
|
||||
return;
|
||||
}
|
||||
if (cbInfo->env == nullptr) {
|
||||
delete work;
|
||||
return;
|
||||
}
|
||||
napi_handle_scope scope = nullptr;
|
||||
napi_open_handle_scope(cbInfo->env, &scope);
|
||||
napi_value callback = nullptr;
|
||||
@ -77,8 +78,29 @@ namespace Settings {
|
||||
napi_call_function(cbInfo->env, undefined, callback, PARAM2, result, &callResult);
|
||||
napi_close_handle_scope(cbInfo->env, scope);
|
||||
SETTING_LOG_INFO("%{public}s, uv_work success.", __func__);
|
||||
napi_remove_env_cleanup_hook(cbInfo->env, SettingsObserver::EnvObserver, cbInfo);
|
||||
delete work;
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SettingsObserver::OnChange()
|
||||
{
|
||||
uv_loop_s* loop = nullptr;
|
||||
napi_get_uv_event_loop(cbInfo->env, &loop);
|
||||
if (loop == nullptr) {
|
||||
SETTING_LOG_ERROR("%{public}s, fail to get uv loop.", __func__);
|
||||
return;
|
||||
}
|
||||
auto work = new (std::nothrow) uv_work_t;
|
||||
if (work == nullptr) {
|
||||
SETTING_LOG_ERROR("%{public}s, fail to get uv work.", __func__);
|
||||
return;
|
||||
}
|
||||
napi_add_env_cleanup_hook(cbInfo->env, SettingsObserver::EnvObserver, cbInfo);
|
||||
work->data = reinterpret_cast<void*>(cbInfo);
|
||||
|
||||
int ret = OnChangeAsync(loop, work);
|
||||
if (ret != 0) {
|
||||
SETTING_LOG_ERROR("%{public}s, uv_queue_work failed.", __func__);
|
||||
if (work != nullptr) {
|
||||
@ -129,6 +151,7 @@ namespace Settings {
|
||||
return wrap_bool_to_js(env, false);
|
||||
}
|
||||
AsyncCallbackInfo *callbackInfo = new AsyncCallbackInfo();
|
||||
|
||||
callbackInfo->env = env;
|
||||
callbackInfo->key = unwrap_string_from_js(env, args[PARAM1]);
|
||||
callbackInfo->tableName = unwrap_string_from_js(env, args[PARAM2]);
|
||||
|
@ -37,6 +37,7 @@ public:
|
||||
void OnChange();
|
||||
AsyncCallbackInfo* cbInfo;
|
||||
std::shared_ptr<OHOS::DataShare::DataShareHelper> dataShareHelper;
|
||||
static void EnvObserver(void* arg);
|
||||
};
|
||||
|
||||
napi_value npai_settings_register_observer(napi_env env, napi_callback_info info);
|
||||
|
Loading…
Reference in New Issue
Block a user