!1625 revert timer

Merge pull request !1625 from 陈涵韬/local
This commit is contained in:
openharmony_ci 2024-11-11 09:26:14 +00:00 committed by Gitee
commit 92ecdffd5d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 4 additions and 40 deletions

View File

@ -15,8 +15,6 @@
#include "sys_timer.h"
#include <cinttypes>
#ifdef ENABLE_HITRACE_HELPER_METER
#include "helper/hitrace_helper.h"
#endif
@ -55,42 +53,14 @@ 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_FUNCTION_WITH_DATA("setTimeout", SetTimeout, data),
DECLARE_NAPI_FUNCTION_WITH_DATA("setInterval", SetInterval, data),
DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION("setTimeout", SetTimeout),
DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION("setInterval", SetInterval),
DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION("clearTimeout", ClearTimer),
DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION("clearInterval", ClearTimer)
};
@ -101,16 +71,12 @@ bool Timer::RegisterTime(napi_env env)
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<TimeData*>(data)->func_(env, cbinfo, false);
return Timer::SetTimeoutInner(env, cbinfo, false);
}
napi_value Timer::SetInterval(napi_env env, napi_callback_info cbinfo)
{
void *data = nullptr;
napi_get_cb_info(env, cbinfo, 0, nullptr, nullptr, &data);
return reinterpret_cast<TimeData*>(data)->func_(env, cbinfo, true);
return Timer::SetTimeoutInner(env, cbinfo, true);
}
napi_value Timer::ClearTimer(napi_env env, napi_callback_info cbinfo)

View File

@ -79,8 +79,6 @@ 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);
static uint32_t timeCallbackId;
static std::map<uint32_t, TimerCallbackInfo*> timerTable;