diff --git a/index.js b/index.js old mode 100755 new mode 100644 index 481a6f8..ec09e24 --- a/index.js +++ b/index.js @@ -50,27 +50,41 @@ function startLoop() { asyncHook.enable(); setTimeoutNode = global.setTimeout; - - global.setTimeout = function(cb, t) { + global.setTimeout = function(cb, timeout) { + const timeoutHandle = {running: true}; const args = Array.prototype.slice.call(arguments, 2); - return binding.lib.setTimeout(function() { - cb.apply(null, args); - }, t); - }; + binding.lib.Ui.startTimer(timeout, function() { + if (timeoutHandle.running) { + cb.apply(null, args); + } + return false; + }); + return timeoutHandle; + } clearTimeoutNode = global.clearTimeout; - global.clearTimeout = binding.lib.clearTimeout; + global.clearTimeout = function(timeoutHandle) { + timeoutHandle.running = false; + } setIntervalNode = global.setInterval; - global.setInterval = function(cb, t) { + global.setInterval = function(cb, timeout) { + const timeoutHandle = {running: true}; const args = Array.prototype.slice.call(arguments, 2); - return binding.lib.setInterval(function() { - cb.apply(null, args); - }, t); - }; + binding.lib.Ui.startTimer(timeout, function() { + if (timeoutHandle.running) { + cb.apply(null, args); + return true; + } + return false; + }); + return timeoutHandle; + } clearIntervalNode = global.clearInterval; - global.clearInterval = binding.lib.clearInterval; + global.clearInterval = function(timeoutHandle) { + timeoutHandle.running = false; + } } // This is called when a new async handle diff --git a/src/Ui.cc b/src/Ui.cc index 5342ead..9563d57 100644 --- a/src/Ui.cc +++ b/src/Ui.cc @@ -10,6 +10,11 @@ static int onShouldQuit_cb(void *data) { return 0; } +static int uiTimer_cb(void *data) { + nbind::cbFunction *cb = (nbind::cbFunction *)data; + return cb->call(); +} + struct Ui { static void main() { uiMain(); @@ -40,6 +45,11 @@ struct Ui { uiFreeInitError(err); } } + + static void startTimer(int ms, nbind::cbFunction &cb) { + nbind::cbFunction *callbackJs = new nbind::cbFunction(cb); + uiTimer(ms, uiTimer_cb, callbackJs); + } }; NBIND_CLASS(Ui) { @@ -49,4 +59,5 @@ NBIND_CLASS(Ui) { method(mainStep); method(mainSteps); method(onShouldQuit); + method(startTimer); } diff --git a/src/arch/darwin/timer.mm b/src/arch/darwin/timer.mm deleted file mode 100644 index 43537b6..0000000 --- a/src/arch/darwin/timer.mm +++ /dev/null @@ -1,40 +0,0 @@ -#include "timer.h" -#include "nbind/api.h" -#import - -TimeoutHandle *setTimeout(nbind::cbFunction &cb, unsigned int timeout) { - nbind::cbFunction *callbackJs = new nbind::cbFunction(cb); - TimeoutHandle *timeoutHandle = new TimeoutHandle(callbackJs); - timeoutHandle->handle = - [NSTimer scheduledTimerWithTimeInterval:timeout / 1000.0 - repeats:NO - block:^(NSTimer *timer) { - CALL_JSCB(timeoutHandle); - timeoutHandle->destroy(); - }]; - return timeoutHandle; -} - -void clearTimeout(TimeoutHandle *timeoutHandle) { - NSTimer *handle = (NSTimer *)timeoutHandle->handle; - [handle invalidate]; - timeoutHandle->destroy(); -} - -TimeoutHandle *setInterval(nbind::cbFunction &cb, unsigned int timeout) { - nbind::cbFunction *callbackJs = new nbind::cbFunction(cb); - TimeoutHandle *timeoutHandle = new TimeoutHandle(callbackJs); - timeoutHandle->handle = - [NSTimer scheduledTimerWithTimeInterval:timeout / 1000.0 - repeats:YES - block:^(NSTimer *timer) { - CALL_JSCB(timeoutHandle); - }]; - return timeoutHandle; -} - -void clearInterval(TimeoutHandle *timeoutHandle) { - NSTimer *handle = (NSTimer *)timeoutHandle->handle; - [handle invalidate]; - timeoutHandle->destroy(); -} diff --git a/src/arch/unix/timer.cc b/src/arch/unix/timer.cc deleted file mode 100644 index 5b0347d..0000000 --- a/src/arch/unix/timer.cc +++ /dev/null @@ -1,39 +0,0 @@ -#include "timer.h" -#include "nbind/api.h" - -gboolean glib_timeout_cb(TimeoutHandle *timeoutHandle) { - CALL_JSCB(timeoutHandle); - timeoutHandle->destroy(); - return FALSE; -} - -gboolean glib_interval_cb(TimeoutHandle *timeoutHandle) { - CALL_JSCB(timeoutHandle); - return TRUE; -} - -TimeoutHandle *setTimeout(nbind::cbFunction &cb, unsigned int timeout) { - nbind::cbFunction *callbackJs = new nbind::cbFunction(cb); - TimeoutHandle *timeoutHandle = new TimeoutHandle(callbackJs); - timeoutHandle->handle = - g_timeout_add(timeout, (GSourceFunc)glib_timeout_cb, timeoutHandle); - return timeoutHandle; -} - -void clearTimeout(TimeoutHandle *timeoutHandle) { - g_source_remove(timeoutHandle->handle); - timeoutHandle->destroy(); -} - -TimeoutHandle *setInterval(nbind::cbFunction &cb, unsigned int timeout) { - nbind::cbFunction *callbackJs = new nbind::cbFunction(cb); - TimeoutHandle *timeoutHandle = new TimeoutHandle(callbackJs); - timeoutHandle->handle = - g_timeout_add(timeout, (GSourceFunc)glib_interval_cb, timeoutHandle); - return timeoutHandle; -} - -void clearInterval(TimeoutHandle *timeoutHandle) { - g_source_remove(timeoutHandle->handle); - timeoutHandle->destroy(); -} diff --git a/src/arch/win32/timer.cc b/src/arch/win32/timer.cc deleted file mode 100644 index af8fd90..0000000 --- a/src/arch/win32/timer.cc +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include "nbind/api.h" -#include "timer.h" - -static std::map timersMap; - -void killTimer(TimeoutHandle *timeoutHandle) { - KillTimer(NULL, timeoutHandle->handle); - timersMap.erase(timeoutHandle->handle); - timeoutHandle->destroy(); -} - -void CALLBACK win_timeout_cb(HWND hwnd, UINT uMsg, UINT_PTR idEvent, - DWORD dwTime) { - TimeoutHandle *timeoutHandle = timersMap[idEvent]; - CALL_JSCB(timeoutHandle); - killTimer(timeoutHandle); -} - -void CALLBACK win_interval_cb(HWND hwnd, UINT uMsg, UINT_PTR idEvent, - DWORD dwTime) { - TimeoutHandle *timeoutHandle = timersMap[idEvent]; - CALL_JSCB(timeoutHandle); -} - -TimeoutHandle *setTimeout(nbind::cbFunction &cb, unsigned int timeout) { - nbind::cbFunction *callbackJs = new nbind::cbFunction(cb); - TimeoutHandle *timeoutHandle = new TimeoutHandle(callbackJs); - // SetTimer could work with a NULL window. - // https://stackoverflow.com/questions/7531650/can-i-use-a-settimer-api-in-a-console-c-application - timeoutHandle->handle = SetTimer(NULL, NULL, timeout, win_timeout_cb); - timersMap[timeoutHandle->handle] = timeoutHandle; - return timeoutHandle; -} - -void clearTimeout(TimeoutHandle *timeoutHandle) { - if (!timeoutHandle->destroyed) { - killTimer(timeoutHandle); - } -} - -TimeoutHandle *setInterval(nbind::cbFunction &cb, unsigned int timeout) { - nbind::cbFunction *callbackJs = new nbind::cbFunction(cb); - TimeoutHandle *timeoutHandle = new TimeoutHandle(callbackJs); - timeoutHandle->handle = SetTimer(NULL, NULL, timeout, win_interval_cb); - timersMap[timeoutHandle->handle] = timeoutHandle; - return timeoutHandle; -} - -void clearInterval(TimeoutHandle *timeoutHandle) { - killTimer(timeoutHandle); -} diff --git a/src/timer-common.cc b/src/timer-common.cc deleted file mode 100644 index 1bf5954..0000000 --- a/src/timer-common.cc +++ /dev/null @@ -1,27 +0,0 @@ -#include "nbind/api.h" -#include "timer.h" - -TimeoutHandle::TimeoutHandle(nbind::cbFunction *callbackJs) { - this->callbackJs = callbackJs; - this->destroyed = false; -} - -void TimeoutHandle::destroy() { - if (this->destroyed) { - return; - } - - delete this->callbackJs; - this->destroyed = true; -} - -#include "nbind/nbind.h" - -NBIND_GLOBAL() { - function(setTimeout); - function(clearTimeout); - function(setInterval); - function(clearInterval); -} - -NBIND_CLASS(TimeoutHandle) {}