Merge pull request !1617 from 陈涵韬/local
This commit is contained in:
openharmony_ci 2024-11-10 08:47:38 +00:00 committed by Gitee
commit 286c315b2e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 19 additions and 12 deletions

View File

@ -15,6 +15,8 @@
#include "sys_timer.h"
#include <cinttypes>
#ifdef ENABLE_HITRACE_HELPER_METER
#include "helper/hitrace_helper.h"
#endif
@ -53,23 +55,26 @@ TimerCallbackInfo::~TimerCallbackInfo()
});
}
typedef napi_value (*SetTimerFunction)(napi_env env, napi_callback_info info, bool repeat);
typedef napi_value (*SetTimeFunction)(napi_env env, napi_callback_info info, bool repeat);
napi_value Timer::SetTimeroutFaker(napi_env env, napi_callback_info cbinfo, bool repeat)
napi_value Timer::SetTimeOutFaker(napi_env env, napi_callback_info cbinfo, bool repeat)
{
return nullptr;
std::lock_guard<std::mutex> lock(timeLock);
uint32_t tId = timeCallbackId++;
HILOG_WARN("Timer is deactivated on current JS Thread, timer id = %{public}" PRIu32, tId);
return Helper::NapiHelper::CreateUint32(env, tId);
}
struct Data {
struct TimeData {
napi_env env_;
SetTimerFunction func_;
SetTimeFunction func_;
};
void Timer::CleanUpHook(void* data)
{
auto that = reinterpret_cast<Data*>(data);
auto that = reinterpret_cast<TimeData*>(data);
Timer::ClearEnvironmentTimer(that->env_);
that->func_ = Timer::SetTimeroutFaker;
that->func_ = Timer::SetTimeOutFaker;
that->env_ = nullptr;
}
@ -78,14 +83,14 @@ bool Timer::RegisterTime(napi_env env)
if (env == nullptr) {
return false;
}
thread_local auto data = new Data();
thread_local auto data = new TimeData();
data->env_ = env;
data->func_ = SetTimeoutInner;
napi_add_env_cleanup_hook(env, CleanUpHook, data);
napi_property_descriptor properties[] = {
DECLARE_NAPI_FUNCTION_WITH_DATA("setTimeout", SetTimeout, data),
DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION("setInterval", SetInterval),
DECLARE_NAPI_FUNCTION_WITH_DATA("setInterval", SetInterval, data),
DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION("clearTimeout", ClearTimer),
DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION("clearInterval", ClearTimer)
};
@ -98,12 +103,14 @@ napi_value Timer::SetTimeout(napi_env env, napi_callback_info cbinfo)
{
void *data = nullptr;
napi_get_cb_info(env, cbinfo, 0, nullptr, nullptr, &data);
return reinterpret_cast<Data*>(data)->func_(env, cbinfo, false);
return reinterpret_cast<TimeData*>(data)->func_(env, cbinfo, false);
}
napi_value Timer::SetInterval(napi_env env, napi_callback_info cbinfo)
{
return Timer::SetTimeoutInner(env, cbinfo, true);
void *data = nullptr;
napi_get_cb_info(env, cbinfo, 0, nullptr, nullptr, &data);
return reinterpret_cast<TimeData*>(data)->func_(env, cbinfo, true);
}
napi_value Timer::ClearTimer(napi_env env, napi_callback_info cbinfo)

View File

@ -79,7 +79,7 @@ private:
static napi_value SetTimeoutInner(napi_env env, napi_callback_info cbinfo, bool repeat);
static void TimerCallback(uv_timer_t* handle);
static void DeleteTimer(uint32_t tId, TimerCallbackInfo* callbackInfo);
static napi_value SetTimeroutFaker(napi_env env, napi_callback_info cbinfo, bool repeat);
static napi_value SetTimeOutFaker(napi_env env, napi_callback_info cbinfo, bool repeat);
static void CleanUpHook(void* data);
static uint32_t timeCallbackId;