Skip to content

Commit 136856c

Browse files
committed
Use libui timer instead of own
1 parent f3dd4eb commit 136856c

6 files changed

Lines changed: 38 additions & 171 deletions

File tree

index.js

100755100644
Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,41 @@ function startLoop() {
5050
asyncHook.enable();
5151

5252
setTimeoutNode = global.setTimeout;
53-
54-
global.setTimeout = function(cb, t) {
53+
global.setTimeout = function(cb, timeout) {
54+
const timeoutHandle = {running: true};
5555
const args = Array.prototype.slice.call(arguments, 2);
56-
return binding.lib.setTimeout(function() {
57-
cb.apply(null, args);
58-
}, t);
59-
};
56+
binding.lib.Ui.startTimer(timeout, function() {
57+
if (timeoutHandle.running) {
58+
cb.apply(null, args);
59+
}
60+
return false;
61+
});
62+
return timeoutHandle;
63+
}
6064

6165
clearTimeoutNode = global.clearTimeout;
62-
global.clearTimeout = binding.lib.clearTimeout;
66+
global.clearTimeout = function(timeoutHandle) {
67+
timeoutHandle.running = false;
68+
}
6369

6470
setIntervalNode = global.setInterval;
65-
global.setInterval = function(cb, t) {
71+
global.setInterval = function(cb, timeout) {
72+
const timeoutHandle = {running: true};
6673
const args = Array.prototype.slice.call(arguments, 2);
67-
return binding.lib.setInterval(function() {
68-
cb.apply(null, args);
69-
}, t);
70-
};
74+
binding.lib.Ui.startTimer(timeout, function() {
75+
if (timeoutHandle.running) {
76+
cb.apply(null, args);
77+
return true;
78+
}
79+
return false;
80+
});
81+
return timeoutHandle;
82+
}
7183

7284
clearIntervalNode = global.clearInterval;
73-
global.clearInterval = binding.lib.clearInterval;
85+
global.clearInterval = function(timeoutHandle) {
86+
timeoutHandle.running = false;
87+
}
7488
}
7589

7690
// This is called when a new async handle

src/Ui.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ static int onShouldQuit_cb(void *data) {
1010
return 0;
1111
}
1212

13+
static int uiTimer_cb(void *data) {
14+
nbind::cbFunction *cb = (nbind::cbFunction *)data;
15+
return cb->call<int>();
16+
}
17+
1318
struct Ui {
1419
static void main() {
1520
uiMain();
@@ -40,6 +45,11 @@ struct Ui {
4045
uiFreeInitError(err);
4146
}
4247
}
48+
49+
static void startTimer(int ms, nbind::cbFunction &cb) {
50+
nbind::cbFunction *callbackJs = new nbind::cbFunction(cb);
51+
uiTimer(ms, uiTimer_cb, callbackJs);
52+
}
4353
};
4454

4555
NBIND_CLASS(Ui) {
@@ -49,4 +59,5 @@ NBIND_CLASS(Ui) {
4959
method(mainStep);
5060
method(mainSteps);
5161
method(onShouldQuit);
62+
method(startTimer);
5263
}

src/arch/darwin/timer.mm

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/arch/unix/timer.cc

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/arch/win32/timer.cc

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/timer-common.cc

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)