Skip to content

Commit

Permalink
Use libui timer instead of own
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Apr 28, 2018
1 parent f3dd4eb commit 136856c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 171 deletions.
40 changes: 27 additions & 13 deletions index.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/Ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>();

This comment has been minimized.

Copy link
@parro-it

parro-it Apr 29, 2018

Owner

We should remember to release the reference to nbind::cbFunction once we solves the issue I found on control events callbacks (that apply here too I guess)

}

struct Ui {
static void main() {
uiMain();
Expand Down Expand Up @@ -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) {
Expand All @@ -49,4 +59,5 @@ NBIND_CLASS(Ui) {
method(mainStep);
method(mainSteps);
method(onShouldQuit);
method(startTimer);
}
40 changes: 0 additions & 40 deletions src/arch/darwin/timer.mm

This file was deleted.

39 changes: 0 additions & 39 deletions src/arch/unix/timer.cc

This file was deleted.

52 changes: 0 additions & 52 deletions src/arch/win32/timer.cc

This file was deleted.

27 changes: 0 additions & 27 deletions src/timer-common.cc

This file was deleted.

0 comments on commit 136856c

Please sign in to comment.