1 /** 2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_TIMER_MODULE_H 17 #define PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_TIMER_MODULE_H 18 19 #include <uv.h> 20 #include <node_api.h> 21 #include <unordered_map> 22 #include <sys/types.h> 23 #include "ani/ani.h" 24 #include "libpandabase/macros.h" 25 26 class TimerModule { 27 public: 28 static bool Init(ani_env *env); 29 static ani_int StartTimer(ani_env *env, ani_object funcObject, ani_int delayMs, ani_boolean repeat); 30 static void StopTimer(ani_env *env, ani_int timerId); 31 32 private: 33 class TimerInfo { 34 public: TimerInfo(ani_object info)35 explicit TimerInfo(ani_object info) : info_(info) {} 36 NO_COPY_SEMANTIC(TimerInfo); 37 NO_MOVE_SEMANTIC(TimerInfo); 38 ~TimerInfo() = default; 39 40 bool IsOneShotTimer(ani_env *env); 41 42 uv_timer_t *GetLinkedTimer(ani_env *env); 43 44 void InvokeCallback(ani_env *env); 45 46 private: 47 ani_object info_; 48 }; 49 50 static void TimerCallback(uv_timer_t *timer); 51 static void DisarmTimer(uv_timer_t *timer); 52 static void RepeatTimer(uv_timer_t *timer, uint64_t timerId); 53 static bool CheckMainThread(ani_env *env); 54 55 static ani_object GetTimerTable(ani_env *env); 56 static std::pair<ani_object, bool> FindTimerInfo(ani_env *env, ani_int timerId); 57 static void ClearTimerInfo(ani_env *env, ani_int timerId); 58 59 static constexpr ani_int MIN_INTERVAL_MS = 1; 60 }; 61 62 #endif // PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_TIMER_MODULE_H 63