| 51 | |
| 52 | |
| 53 | void Timers::Init(v8::Isolate *isolate, v8::Local<v8::ObjectTemplate> &globalObjectTemplate) { |
| 54 | isolate_ = isolate; |
| 55 | // TODO: remove the __ns__ prefix once this is validated |
| 56 | SetMethod(isolate, globalObjectTemplate, "__ns__setTimeout", SetTimeoutCallback, External::New(isolate, this)); |
| 57 | SetMethod(isolate, globalObjectTemplate, "__ns__setInterval", SetIntervalCallback, External::New(isolate, this)); |
| 58 | SetMethod(isolate, globalObjectTemplate, "__ns__clearTimeout", ClearTimer, External::New(isolate, this)); |
| 59 | SetMethod(isolate, globalObjectTemplate, "__ns__clearInterval", ClearTimer, External::New(isolate, this)); |
| 60 | auto res = pipe(fd_); |
| 61 | assert(res != -1); |
| 62 | res = fcntl(fd_[1], F_SETFL, O_NONBLOCK); |
| 63 | assert(res != -1); |
| 64 | // TODO: check success of fd |
| 65 | looper_ = ALooper_prepare(0); |
| 66 | ALooper_acquire(looper_); |
| 67 | ALooper_addFd(looper_, fd_[0], ALOOPER_POLL_CALLBACK, ALOOPER_EVENT_INPUT, |
| 68 | PumpTimerLoopCallback, this); |
| 69 | ALooper_wake(looper_); |
| 70 | watcher_ = std::thread(&Timers::threadLoop, this); |
| 71 | stopped = false; |
| 72 | } |
| 73 | |
| 74 | void Timers::addTask(std::shared_ptr<TimerTask> task) { |
| 75 | if (task->queued_) { |
no test coverage detected