forked from Tencent/Hippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer_module.cc
More file actions
303 lines (262 loc) · 10.9 KB
/
timer_module.cc
File metadata and controls
303 lines (262 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "driver/modules/timer_module.h"
#include "driver/base/common.h"
#include "driver/modules/module_register.h"
#include "footstone/logging.h"
#include "footstone/task.h"
#include "footstone/check.h"
#include "footstone/time_delta.h"
#include "footstone/one_shot_timer.h"
#include "footstone/repeating_timer.h"
#include "footstone/string_view_utils.h"
#include "footstone/idle_task.h"
using string_view = footstone::stringview::string_view;
using Ctx = hippy::napi::Ctx;
using CtxValue = hippy::napi::CtxValue;
using CallbackInfo = hippy::CallbackInfo;
using RegisterFunction = hippy::base::RegisterFunction;
using RegisterMap = hippy::base::RegisterMap;
using BaseTimer = footstone::BaseTimer;
using Task = footstone::runner::Task;
using TaskRunner = footstone::runner::TaskRunner;
using RepeatingTimer = footstone::timer::RepeatingTimer;
using OneShotTimer = footstone::timer::OneShotTimer;
using TimeDelta = footstone::time::TimeDelta;
using IdleTask = footstone::IdleTask;
using IdleCbParam = footstone::IdleTask::IdleCbParam;
constexpr char kTimeoutKey[] = "timeout";
constexpr char kDidTimeoutKey[] = "didTimeout";
constexpr char kTimeRemainingKey[] = "timeRemaining";
namespace hippy {
inline namespace driver {
inline namespace module {
GEN_INVOKE_CB(TimerModule, SetTimeout) // NOLINT(cert-err58-cpp)
GEN_INVOKE_CB(TimerModule, ClearTimeout) // NOLINT(cert-err58-cpp)
GEN_INVOKE_CB(TimerModule, SetInterval) // NOLINT(cert-err58-cpp)
GEN_INVOKE_CB(TimerModule, ClearInterval) // NOLINT(cert-err58-cpp)
GEN_INVOKE_CB(TimerModule, RequestIdleCallback) // NOLINT(cert-err58-cpp)
GEN_INVOKE_CB(TimerModule, CancelIdleCallback) // NOLINT(cert-err58-cpp)
TimerModule::TimerModule() :
timer_map_(std::make_shared<std::unordered_map<uint32_t,
std::shared_ptr<footstone::BaseTimer>>>()),
idle_function_holder_map_(std::make_shared<std::unordered_map<uint32_t,
std::shared_ptr<CtxValue>>>()) {}
void TimerModule::SetTimeout(CallbackInfo& info, void* data) {
info.GetReturnValue()->Set(Start(info, false));
}
void TimerModule::ClearTimeout(CallbackInfo& info, void* data) {
ClearInterval(info, data);
}
void TimerModule::SetInterval(CallbackInfo& info, void* data) {
info.GetReturnValue()->Set(Start(info, true));
}
void TimerModule::ClearInterval(CallbackInfo& info, void* data) {
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto context = scope->GetContext();
FOOTSTONE_CHECK(context);
int32_t argument = 0;
if (!context->GetValueNumber(info[0], &argument)) {
info.GetExceptionValue()->Set(context, "The first argument must be int32.");
return;
}
uint32_t task_id = footstone::checked_numeric_cast<int32_t, uint32_t>(argument);
Cancel(task_id);
info.GetReturnValue()->Set(context->CreateNumber(task_id));
}
void TimerModule::RequestIdleCallback(CallbackInfo& info, void* data) {
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto context = scope->GetContext();
FOOTSTONE_CHECK(context);
auto function = info[0];
if (!context->IsFunction(function)) {
info.GetExceptionValue()->Set(context,"The first argument must be function.");
return;
}
auto runner = scope->GetTaskRunner();
FOOTSTONE_DCHECK(runner);
auto timeout = TimeDelta::Max();
if (info[1]) {
std::unordered_map<std::shared_ptr<CtxValue>, std::shared_ptr<CtxValue>> option;
auto timeout_key = context->CreateString(kTimeoutKey);
auto timeout_value = context->GetProperty(info[1], timeout_key);
double time;
auto flag = context->GetValueNumber(timeout_value, &time);
if (flag) {
timeout = TimeDelta::FromMilliseconds(static_cast<int64_t>(time));
}
}
std::weak_ptr<Scope> weak_scope = scope;
auto task = std::make_unique<IdleTask>();
task->SetTimeout(timeout);
auto task_id = task->GetId();
(*idle_function_holder_map_)[task_id] = function;
std::weak_ptr<CtxValue> weak_function = function;
std::weak_ptr<std::unordered_map<uint32_t, std::shared_ptr<CtxValue>>> weak_map = idle_function_holder_map_;
task->SetUnit([weak_scope, weak_function, weak_map, task_id](const IdleCbParam& idle_cb_param) {
auto scope = weak_scope.lock();
if (!scope) {
return;
}
auto function = weak_function.lock();
FOOTSTONE_DCHECK(function);
if (!function) {
return;
}
std::shared_ptr<hippy::napi::Ctx> context = scope->GetContext();
auto param = context->CreateObject();
auto did_timeout_key = context->CreateString(kDidTimeoutKey);
auto did_timeout_value = context->CreateBoolean(idle_cb_param.did_time_out);
context->SetProperty(param, did_timeout_key, did_timeout_value);
auto res_time = idle_cb_param.res_time.ToMillisecondsF();
auto time_remaining_key = context->CreateString(kTimeRemainingKey);
auto time_remaining_value = context->CreateNumber(res_time);
context->SetProperty(param, time_remaining_key, time_remaining_value);
std::shared_ptr<CtxValue> argv[] = { param };
context->CallFunction(function, context->GetGlobalObject(), 1, argv);
auto idle_function_holder_map = weak_map.lock();
if (!idle_function_holder_map) {
return;
}
auto it = idle_function_holder_map->find(task_id);
FOOTSTONE_DCHECK(it != idle_function_holder_map->end());
if (it != idle_function_holder_map->end()) {
idle_function_holder_map->erase(it);
}
});
runner->PostIdleTask(std::move(task));
}
void TimerModule::CancelIdleCallback(CallbackInfo& info, void* data) {
}
std::shared_ptr<hippy::napi::CtxValue> TimerModule::Start(CallbackInfo& info, bool repeat) {
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_DCHECK(scope);
if (!scope || !scope->GetEngine().lock()) {
return nullptr;
}
auto context = scope->GetContext();
FOOTSTONE_CHECK(context);
auto function = info[0];
if (!context->IsFunction(function)) {
info.GetExceptionValue()->Set(context,"The first argument must be function.");
return nullptr;
}
std::shared_ptr<TaskRunner> runner = scope->GetTaskRunner();
FOOTSTONE_DCHECK(runner);
double number = 0;
context->GetValueNumber(info[1], &number);
TimeDelta delay = TimeDelta::FromMilliseconds(static_cast<int64_t>(std::max(.0, number)));
std::weak_ptr<Scope> weak_scope = scope;
auto task = std::make_unique<Task>();
auto task_id = task->GetId();
std::weak_ptr<std::unordered_map<uint32_t , std::shared_ptr<BaseTimer>>> weak_timer_map = timer_map_;
// holding chain: scope -> timer_module -> timer_map -> BaseTimer -> task -> CtxValue(function)
task->SetExecUnit([weak_scope, function, task_id, repeat, weak_timer_map] {
auto scope = weak_scope.lock();
if (!scope) {
return;
}
auto timer_map = weak_timer_map.lock();
if (!timer_map) {
return;
}
FOOTSTONE_DCHECK(function);
if (!function) {
return;
}
std::shared_ptr<hippy::napi::Ctx> context = scope->GetContext();
context->CallFunction(function, context->GetGlobalObject(), 0, nullptr);
#if defined(JS_JSC)
// exception check for jsc
RegisterFunction func;
if (scope->GetExtraCallback(kAsyncTaskEndKey, func)) {
func(nullptr);
}
#endif /* defined(JS_JSC) */
if (!repeat) {
timer_map->erase(task_id);
}
});
if (repeat) {
auto timer = std::make_shared<RepeatingTimer>(runner);
timer->Start(std::move(task), delay);
timer_map_->insert({task_id, std::move(timer)});
} else {
auto timer = std::make_shared<OneShotTimer>(runner);
timer->Start(std::move(task), delay);
timer_map_->insert({task_id, std::move(timer)});
}
return context->CreateNumber(task_id);
}
void TimerModule::Cancel(uint32_t task_id) {
timer_map_->erase(task_id);
}
std::shared_ptr<CtxValue> TimerModule::BindFunction(std::shared_ptr<Scope> scope,
std::shared_ptr<CtxValue>* rest_args) {
auto context = scope->GetContext();
auto object = context->CreateObject();
auto key = context->CreateString("SetTimeout");
auto wrapper = std::make_unique<hippy::napi::FunctionWrapper>(InvokeTimerModuleSetTimeout, nullptr);
auto value = context->CreateFunction(wrapper);
scope->SaveFunctionWrapper(std::move(wrapper));
context->SetProperty(object, key, value);
key = context->CreateString("ClearTimeout");
wrapper = std::make_unique<hippy::napi::FunctionWrapper>(InvokeTimerModuleClearTimeout, nullptr);
value = context->CreateFunction(wrapper);
scope->SaveFunctionWrapper(std::move(wrapper));
context->SetProperty(object, key, value);
key = context->CreateString("SetInterval");
wrapper = std::make_unique<hippy::napi::FunctionWrapper>(InvokeTimerModuleSetInterval, nullptr);
value = context->CreateFunction(wrapper);
scope->SaveFunctionWrapper(std::move(wrapper));
context->SetProperty(object, key, value);
key = context->CreateString("ClearInterval");
wrapper = std::make_unique<hippy::napi::FunctionWrapper>(InvokeTimerModuleClearInterval, nullptr);
value = context->CreateFunction(wrapper);
scope->SaveFunctionWrapper(std::move(wrapper));
context->SetProperty(object, key, value);
key = context->CreateString("RequestIdleCallback");
wrapper = std::make_unique<hippy::napi::FunctionWrapper>(InvokeTimerModuleRequestIdleCallback, nullptr);
value = context->CreateFunction(wrapper);
scope->SaveFunctionWrapper(std::move(wrapper));
context->SetProperty(object, key, value);
key = context->CreateString("CancelIdleCallback");
wrapper = std::make_unique<hippy::napi::FunctionWrapper>(InvokeTimerModuleCancelIdleCallback, nullptr);
value = context->CreateFunction(wrapper);
scope->SaveFunctionWrapper(std::move(wrapper));
context->SetProperty(object, key, value);
return object;
}
}
}
}