|
3 | 3 |
|
4 | 4 | #include "Timers.h" |
5 | 5 | #import <Foundation/Foundation.h> |
| 6 | +#include <objc/runtime.h> |
6 | 7 |
|
7 | 8 | namespace charon { |
8 | 9 |
|
|
22 | 23 | .attributes = napi_default, |
23 | 24 | .data = nullptr, |
24 | 25 | }, |
| 26 | + { |
| 27 | + .utf8name = "setInterval", |
| 28 | + .name = nullptr, |
| 29 | + .method = setInterval, |
| 30 | + .getter = nullptr, |
| 31 | + .setter = nullptr, |
| 32 | + .value = nullptr, |
| 33 | + .attributes = napi_default, |
| 34 | + .data = nullptr, |
| 35 | + }, |
| 36 | + { |
| 37 | + .utf8name = "clearTimeout", |
| 38 | + .name = nullptr, |
| 39 | + .method = clearTimer, |
| 40 | + .getter = nullptr, |
| 41 | + .setter = nullptr, |
| 42 | + .value = nullptr, |
| 43 | + .attributes = napi_default, |
| 44 | + .data = nullptr, |
| 45 | + }, |
| 46 | + { |
| 47 | + .utf8name = "clearInterval", |
| 48 | + .name = nullptr, |
| 49 | + .method = clearTimer, |
| 50 | + .getter = nullptr, |
| 51 | + .setter = nullptr, |
| 52 | + .value = nullptr, |
| 53 | + .attributes = napi_default, |
| 54 | + .data = nullptr, |
| 55 | + }, |
25 | 56 | }; |
26 | 57 |
|
27 | | - napi_define_properties(env, global, 1, properties); |
| 58 | + napi_define_properties(env, global, 4, properties); |
28 | 59 | } |
29 | 60 |
|
30 | 61 | napi_value Timers::setTimeout(napi_env env, napi_callback_info cbinfo) { |
|
42 | 73 |
|
43 | 74 | NSTimer *timer = [NSTimer |
44 | 75 | timerWithTimeInterval:interval |
45 | | - repeats:false |
| 76 | + repeats:NO |
46 | 77 | block:^(NSTimer *timer) { |
47 | 78 | napi_value global, callbackValue; |
48 | 79 | napi_get_global(env, &global); |
49 | 80 | napi_get_reference_value(env, callback, &callbackValue); |
50 | 81 | napi_call_function(env, global, callbackValue, 0, |
51 | 82 | nullptr, nullptr); |
52 | 83 | napi_delete_reference(env, callback); |
| 84 | + objc_setAssociatedObject(timer, "callback", nil, |
| 85 | + OBJC_ASSOCIATION_ASSIGN); |
53 | 86 | }]; |
54 | 87 |
|
| 88 | + objc_setAssociatedObject(timer, "callback", (id)callback, |
| 89 | + OBJC_ASSOCIATION_ASSIGN); |
| 90 | + |
55 | 91 | napi_value result; |
56 | 92 | napi_create_int64(env, (int64_t)timer, &result); |
57 | 93 |
|
|
60 | 96 | return result; |
61 | 97 | } |
62 | 98 |
|
| 99 | +napi_value Timers::setInterval(napi_env env, napi_callback_info cbinfo) { |
| 100 | + size_t argc = 2; |
| 101 | + napi_value argv[2]; |
| 102 | + napi_get_cb_info(env, cbinfo, &argc, argv, nullptr, nullptr); |
| 103 | + |
| 104 | + double ms; |
| 105 | + napi_get_value_double(env, argv[1], &ms); |
| 106 | + |
| 107 | + NSTimeInterval interval = ms / 1000; |
| 108 | + |
| 109 | + napi_ref callback; |
| 110 | + napi_create_reference(env, argv[0], 1, &callback); |
| 111 | + |
| 112 | + NSTimer *timer = [NSTimer |
| 113 | + timerWithTimeInterval:interval |
| 114 | + repeats:YES |
| 115 | + block:^(NSTimer *timer) { |
| 116 | + napi_value global, callbackValue; |
| 117 | + napi_get_global(env, &global); |
| 118 | + napi_get_reference_value(env, callback, &callbackValue); |
| 119 | + napi_call_function(env, global, callbackValue, 0, |
| 120 | + nullptr, nullptr); |
| 121 | + }]; |
| 122 | + |
| 123 | + objc_setAssociatedObject(timer, "callback", (id)callback, |
| 124 | + OBJC_ASSOCIATION_ASSIGN); |
| 125 | + |
| 126 | + napi_value result; |
| 127 | + napi_create_int64(env, (int64_t)timer, &result); |
| 128 | + |
| 129 | + [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; |
| 130 | + |
| 131 | + return result; |
| 132 | +} |
| 133 | + |
| 134 | +napi_value Timers::clearTimer(napi_env env, napi_callback_info cbinfo) { |
| 135 | + size_t argc = 1; |
| 136 | + napi_value argv[1]; |
| 137 | + napi_get_cb_info(env, cbinfo, &argc, argv, nullptr, nullptr); |
| 138 | + |
| 139 | + int64_t timer; |
| 140 | + napi_get_value_int64(env, argv[0], &timer); |
| 141 | + |
| 142 | + NSTimer *t = (NSTimer *)timer; |
| 143 | + [t invalidate]; |
| 144 | + |
| 145 | + napi_ref callback = (napi_ref)objc_getAssociatedObject(t, "callback"); |
| 146 | + if (callback != nil) { |
| 147 | + napi_delete_reference(env, callback); |
| 148 | + } |
| 149 | + |
| 150 | + return nullptr; |
| 151 | +} |
| 152 | + |
63 | 153 | } // namespace charon |
64 | 154 |
|
65 | 155 | #endif // __APPLE__ |
0 commit comments