!1629 fix timer can be written

Merge pull request !1629 from 陈涵韬/local
This commit is contained in:
openharmony_ci 2024-11-11 14:34:41 +00:00 committed by Gitee
commit 1beb2cf90b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 45 additions and 4 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,14 +55,42 @@ TimerCallbackInfo::~TimerCallbackInfo()
});
}
typedef napi_value (*SetTimeFunction)(napi_env env, napi_callback_info info, bool repeat);
napi_value Timer::SetTimeOutFaker(napi_env env, napi_callback_info cbinfo, bool repeat)
{
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 TimeData {
napi_env env_;
SetTimeFunction func_;
};
void Timer::CleanUpHook(void* data)
{
auto that = reinterpret_cast<TimeData*>(data);
Timer::ClearEnvironmentTimer(that->env_);
that->func_ = Timer::SetTimeOutFaker;
that->env_ = nullptr;
}
bool Timer::RegisterTime(napi_env env)
{
if (env == nullptr) {
return false;
}
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_DEFAULT_PROPERTY_FUNCTION("setTimeout", SetTimeout),
DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION("setInterval", SetInterval),
DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION_WITH_DATA("setTimeout", SetTimeout, data),
DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION_WITH_DATA("setInterval", SetInterval, data),
DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION("clearTimeout", ClearTimer),
DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION("clearInterval", ClearTimer)
};
@ -71,12 +101,16 @@ bool Timer::RegisterTime(napi_env env)
napi_value Timer::SetTimeout(napi_env env, napi_callback_info cbinfo)
{
return Timer::SetTimeoutInner(env, cbinfo, false);
void *data = nullptr;
napi_get_cb_info(env, cbinfo, 0, nullptr, nullptr, &data);
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,6 +79,13 @@ 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 SetTimeOutFaker(napi_env env, napi_callback_info cbinfo, bool repeat);
static void CleanUpHook(void* data);
#define DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION_WITH_DATA(name, func, data) \
{ \
(name), nullptr, (func), nullptr, nullptr, nullptr, napi_default_jsproperty, data \
}
static uint32_t timeCallbackId;
static std::map<uint32_t, TimerCallbackInfo*> timerTable;