-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathV8Timer.h
54 lines (46 loc) · 1.45 KB
/
V8Timer.h
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
#pragma once
#include "V8Helpers.h"
#include "V8ResourceImpl.h"
class V8Timer
{
public:
V8Timer(v8::Isolate* _isolate, v8::Local<v8::Context> _context, int64_t curTime, v8::Local<v8::Function> _callback, uint32_t _interval, bool _once, V8Helpers::SourceLocation&& _location)
: isolate(_isolate), context(_isolate, _context), lastRun(curTime), callback(_isolate, _callback), interval(_interval), once(_once), location(std::move(_location))
{
// Log::Debug << "Create timer: " << curTime << " " << interval << Log::Endl;
}
bool Update(int64_t curTime)
{
if(curTime - lastRun >= interval)
{
V8Helpers::TryCatch([&] {
std::vector<v8::Local<v8::Value>> args;
v8::MaybeLocal<v8::Value> result = V8Helpers::CallFunctionWithTimeout(callback.Get(isolate), context.Get(isolate), args);
return !result.IsEmpty();
});
lastRun = curTime;
return !once;
}
return true;
}
const V8Helpers::SourceLocation& GetLocation() const
{
return location;
}
int64_t GetInterval()
{
return interval;
}
bool IsOnce()
{
return once;
}
private:
v8::Isolate* isolate;
V8Helpers::CPersistent<v8::Context> context;
V8Helpers::CPersistent<v8::Function> callback;
int64_t interval;
int64_t lastRun = 0;
bool once;
V8Helpers::SourceLocation location;
};