Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/event-loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ function makeToolbar() {
let timeoutHandle = null;
btnCustom.onClicked(() => {
if (timeoutHandle) {
console.log('before clearTimeout', timeoutHandle);
clearTimeout(timeoutHandle);
console.log('after clearTimeout', timeoutHandle);

timeoutHandle = null;
return;
}
Expand All @@ -125,7 +128,9 @@ function makeToolbar() {
timeoutHandle = setTimeout((a, b, c) => {
const elapsed = Date.now() - now;
logAppend(`Custom setTimeout: ${now} - elapsed ${elapsed} ms. Args: ${a} ${b} ${c}`);
timeoutHandle = null;
}, 1000, 'custom', 'args', 2);
console.log('after timeoutHandle', timeoutHandle);
});
toolbar.append(btnCustom, false);

Expand Down
51 changes: 33 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,44 +50,59 @@ 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() {
// console.log('timeoutHandle.running ', timeoutHandle.running);
if (timeoutHandle.running) {
cb.apply(null, args);
}
return false;
});
return timeoutHandle;
}

clearTimeoutNode = global.clearTimeout;
global.clearTimeout = function(obj) {
if (obj && obj.constructor === binding.lib.TimeoutHandle) {

global.clearTimeout = function(timeoutHandle) {
if (timeoutHandle && typeof timeoutHandle.running === 'boolean') {
timeoutHandle.running = false;
// console.log('patched clearTimeout called');
binding.lib.clearTimeout(obj);
} else {
// console.log('node clearTimeout called');
// not created by us, use original
// clearTimeoutNode
clearTimeoutNode(obj);
clearTimeoutNode(timeoutHandle);
}
};

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 = function(obj) {
if (obj && obj.constructor === binding.lib.TimeoutHandle) {

global.clearInterval = function(timeoutHandle) {
if (timeoutHandle && typeof timeoutHandle.running === 'boolean') {
timeoutHandle.running = false;
// console.log('patched clearInterval called');
binding.lib.clearInterval(obj);
} else {
// console.log('node clearInterval called');
// not created by us, use original
// clearTimeoutNode
clearIntervalNode(obj);
clearIntervalNode(timeoutHandle);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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>();
}

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.