Skip to content

Commit e89b573

Browse files
committed
setTimeout and setInterval for macos
1 parent f721e34 commit e89b573

File tree

4 files changed

+111
-40
lines changed

4 files changed

+111
-40
lines changed

binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"sources": [
4545
"<!@(ls -1 src/*.cc)",
4646
"<!@(ls -1 src/UiArea/*.cc)",
47-
"src/arch/darwin/libui_loop.mm"
47+
"<!@(ls -1 src/arch/darwin/*.mm)"
4848
],
4949
"xcode_settings": {
5050
"OTHER_LDFLAGS": [

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"build": "node-gyp configure build",
3535
"clean": "rm -rf build; rm -rf node_modules;",
3636
"precommit": "check-clang-format \"'npm run lint'\"",
37-
"lint": "clang-format -i --glob='{src/**/*.{h,cc},index.js,examples/**/*.js}'"
37+
"lint": "clang-format -i --glob='{src/**/*.{h,cc,mm},index.js,examples/**/*.js}'"
3838
},
3939
"keywords": [
4040
"libui",

src/arch/darwin/libui_loop.mm

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,48 @@
22
#include "../../includes/event-loop.h"
33

44
int uiLoopWakeup() {
5-
[NSApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined
6-
location:NSZeroPoint
7-
modifierFlags:0
8-
timestamp:0.0
9-
windowNumber:0
10-
context:nil
11-
subtype:0
12-
data1:0
13-
data2:0]
14-
atStart:NO];
15-
// give main thread some time to react
16-
usleep(50 * 1000);
17-
return 0;
5+
[NSApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined
6+
location:NSZeroPoint
7+
modifierFlags:0
8+
timestamp:0.0
9+
windowNumber:0
10+
context:nil
11+
subtype:0
12+
data1:0
13+
data2:0]
14+
atStart:NO];
15+
// give main thread some time to react
16+
usleep(50 * 1000);
17+
return 0;
1818
}
1919

2020
int uiEventsPending() {
21-
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
22-
untilDate:[NSDate distantPast]
23-
inMode:NSDefaultRunLoopMode
24-
dequeue:NO];
25-
return nil != event;
21+
NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask
22+
untilDate:[NSDate distantPast]
23+
inMode:NSDefaultRunLoopMode
24+
dequeue:NO];
25+
return nil != event;
2626
}
2727

28-
int waitForNodeEvents(uv_loop_t* loop, int timeout) {
29-
int nodeBackendFd = uv_backend_fd(loop);
30-
if (nodeBackendFd == -1) {
31-
fprintf(stderr, "Invalid node backend fd.\n");
32-
return 0;
33-
}
28+
int waitForNodeEvents(uv_loop_t *loop, int timeout) {
29+
int nodeBackendFd = uv_backend_fd(loop);
30+
if (nodeBackendFd == -1) {
31+
fprintf(stderr, "Invalid node backend fd.\n");
32+
return 0;
33+
}
3434

35-
struct kevent event;
36-
struct timespec ts;
37-
ts.tv_sec = timeout / 1000;
38-
ts.tv_nsec = (timeout % 1000) * 1000000;
39-
40-
int ret = kevent(nodeBackendFd, NULL, 0, &event, 1, &ts);
41-
42-
struct heap* timer_heap = (struct heap*)&loop->timer_heap;
43-
struct heap_node* timer_node = timer_heap->min;
44-
if (timer_node != NULL) {
45-
return 1;
46-
}
47-
48-
return ret;
35+
struct kevent event;
36+
struct timespec ts;
37+
ts.tv_sec = timeout / 1000;
38+
ts.tv_nsec = (timeout % 1000) * 1000000;
39+
40+
int ret = kevent(nodeBackendFd, NULL, 0, &event, 1, &ts);
41+
42+
struct heap *timer_heap = (struct heap *)&loop->timer_heap;
43+
struct heap_node *timer_node = timer_heap->min;
44+
if (timer_node != NULL) {
45+
return 1;
46+
}
47+
48+
return ret;
4949
}

src/arch/darwin/timer.mm

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "../../includes/event-loop-darwin.h"
2+
#include "nbind/api.h"
3+
4+
class TimeoutHandle {
5+
public:
6+
NSTimer *handle;
7+
nbind::cbFunction *callbackJs;
8+
bool destroyed;
9+
10+
TimeoutHandle(nbind::cbFunction *callbackJs) {
11+
this->callbackJs = callbackJs;
12+
this->destroyed = false;
13+
}
14+
15+
void destroy() {
16+
if (this->destroyed) {
17+
return;
18+
}
19+
20+
delete this->callbackJs;
21+
this->destroyed = true;
22+
}
23+
};
24+
25+
#define CALL_JSCB(timeoutHandle) (*(timeoutHandle->callbackJs))()
26+
27+
TimeoutHandle *setTimeout(nbind::cbFunction &cb, unsigned int timeout) {
28+
nbind::cbFunction *callbackJs = new nbind::cbFunction(cb);
29+
TimeoutHandle *timeoutHandle = new TimeoutHandle(callbackJs);
30+
timeoutHandle->handle =
31+
[NSTimer scheduledTimerWithTimeInterval:timeout / 1000.0
32+
repeats:NO
33+
block:^(NSTimer *timer) {
34+
CALL_JSCB(timeoutHandle);
35+
timeoutHandle->destroy();
36+
}];
37+
return timeoutHandle;
38+
}
39+
40+
void clearTimeout(TimeoutHandle *timeoutHandle) {
41+
[timeoutHandle->handle invalidate];
42+
timeoutHandle->destroy();
43+
}
44+
45+
TimeoutHandle *setInterval(nbind::cbFunction &cb, unsigned int timeout) {
46+
nbind::cbFunction *callbackJs = new nbind::cbFunction(cb);
47+
TimeoutHandle *timeoutHandle = new TimeoutHandle(callbackJs);
48+
timeoutHandle->handle =
49+
[NSTimer scheduledTimerWithTimeInterval:timeout / 1000.0
50+
repeats:YES
51+
block:^(NSTimer *timer) {
52+
CALL_JSCB(timeoutHandle);
53+
}];
54+
return timeoutHandle;
55+
}
56+
57+
void clearInterval(TimeoutHandle *timeoutHandle) {
58+
[timeoutHandle->handle invalidate];
59+
timeoutHandle->destroy();
60+
}
61+
62+
#include "nbind/nbind.h"
63+
64+
NBIND_GLOBAL() {
65+
function(setTimeout);
66+
function(clearTimeout);
67+
function(setInterval);
68+
function(clearInterval);
69+
}
70+
71+
NBIND_CLASS(TimeoutHandle) {}

0 commit comments

Comments
 (0)