55#include < android/looper.h>
66#include < unistd.h>
77#include < thread>
8+ #include " ModuleBinding.h"
9+ #include " IsolateDisposer.h"
10+ #include " Util.h"
811
912
1013/* *
1518 * ALL changes and scheduling of a TimerTask MUST be done when locked in an isolate to ensure consistency
1619 */
1720
18- using namespace tns ;
1921using namespace v8 ;
2022
2123// Takes a value and transform into a positive number
@@ -43,22 +45,18 @@ static double now_ms() {
4345 return 1000.0 * res.tv_sec + (double ) res.tv_nsec / 1e6 ;
4446}
4547
48+ namespace tns {
49+
50+
51+
4652
4753void Timers::Init (v8::Isolate *isolate, v8::Local<v8::ObjectTemplate> &globalObjectTemplate) {
4854 isolate_ = isolate;
4955 // TODO: remove the __ns__ prefix once this is validated
50- globalObjectTemplate->Set (ArgConverter::ConvertToV8String (isolate, " __ns__setTimeout" ),
51- FunctionTemplate::New (isolate, SetTimeoutCallback,
52- External::New (isolate, this )));
53- globalObjectTemplate->Set (ArgConverter::ConvertToV8String (isolate, " __ns__setInterval" ),
54- FunctionTemplate::New (isolate, SetIntervalCallback,
55- External::New (isolate, this )));
56- globalObjectTemplate->Set (ArgConverter::ConvertToV8String (isolate, " __ns__clearTimeout" ),
57- FunctionTemplate::New (isolate, ClearTimer,
58- External::New (isolate, this )));
59- globalObjectTemplate->Set (ArgConverter::ConvertToV8String (isolate, " __ns__clearInterval" ),
60- FunctionTemplate::New (isolate, ClearTimer,
61- External::New (isolate, this )));
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 ));
6260 auto res = pipe (fd_);
6361 assert (res != -1 );
6462 res = fcntl (fd_[1 ], F_SETFL, O_NONBLOCK);
@@ -324,4 +322,14 @@ int Timers::PumpTimerLoopCallback(int fd, int events, void *data) {
324322 }
325323 thiz->bufferFull .notify_one ();
326324 return 1 ;
327- }
325+ }
326+
327+ void Timers::InitStatic (v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalObjectTemplate) {
328+ auto timers = new Timers ();
329+ timers->Init (isolate, globalObjectTemplate);
330+ registerIsolateBoundObject (isolate, timers);
331+ }
332+
333+ };
334+
335+ NODE_BINDING_PER_ISOLATE_INIT_OBJ (timers, tns::Timers::InitStatic);
0 commit comments