|
| 1 | +#include <stdio.h> |
1 | 2 | #include <node_api.h>
|
2 | 3 | #include "../common.h"
|
3 | 4 |
|
@@ -173,10 +174,52 @@ napi_value TestCancel(napi_env env, napi_callback_info info) {
|
173 | 174 | return nullptr;
|
174 | 175 | }
|
175 | 176 |
|
| 177 | +struct { |
| 178 | + napi_ref ref; |
| 179 | + napi_async_work work; |
| 180 | +} repeated_work_info = { nullptr, nullptr }; |
| 181 | + |
| 182 | +static void RepeatedWorkerThread(napi_env env, void* data) {} |
| 183 | + |
| 184 | +static void RepeatedWorkComplete(napi_env env, napi_status status, void* data) { |
| 185 | + napi_value cb, js_status; |
| 186 | + NAPI_CALL_RETURN_VOID(env, |
| 187 | + napi_get_reference_value(env, repeated_work_info.ref, &cb)); |
| 188 | + NAPI_CALL_RETURN_VOID(env, |
| 189 | + napi_delete_async_work(env, repeated_work_info.work)); |
| 190 | + NAPI_CALL_RETURN_VOID(env, |
| 191 | + napi_delete_reference(env, repeated_work_info.ref)); |
| 192 | + repeated_work_info.work = nullptr; |
| 193 | + repeated_work_info.ref = nullptr; |
| 194 | + NAPI_CALL_RETURN_VOID(env, |
| 195 | + napi_create_uint32(env, (uint32_t)status, &js_status)); |
| 196 | + NAPI_CALL_RETURN_VOID(env, |
| 197 | + napi_call_function(env, cb, cb, 1, &js_status, nullptr)); |
| 198 | +} |
| 199 | + |
| 200 | +static napi_value DoRepeatedWork(napi_env env, napi_callback_info info) { |
| 201 | + size_t argc = 1; |
| 202 | + napi_value cb, name; |
| 203 | + NAPI_ASSERT(env, repeated_work_info.ref == nullptr, |
| 204 | + "Reference left over from previous work"); |
| 205 | + NAPI_ASSERT(env, repeated_work_info.work == nullptr, |
| 206 | + "Work pointer left over from previous work"); |
| 207 | + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &cb, nullptr, nullptr)); |
| 208 | + NAPI_CALL(env, napi_create_reference(env, cb, 1, &repeated_work_info.ref)); |
| 209 | + NAPI_CALL(env, |
| 210 | + napi_create_string_utf8(env, "Repeated Work", NAPI_AUTO_LENGTH, &name)); |
| 211 | + NAPI_CALL(env, |
| 212 | + napi_create_async_work(env, nullptr, name, RepeatedWorkerThread, |
| 213 | + RepeatedWorkComplete, &repeated_work_info, &repeated_work_info.work)); |
| 214 | + NAPI_CALL(env, napi_queue_async_work(env, repeated_work_info.work)); |
| 215 | + return nullptr; |
| 216 | +} |
| 217 | + |
176 | 218 | napi_value Init(napi_env env, napi_value exports) {
|
177 | 219 | napi_property_descriptor properties[] = {
|
178 | 220 | DECLARE_NAPI_PROPERTY("Test", Test),
|
179 | 221 | DECLARE_NAPI_PROPERTY("TestCancel", TestCancel),
|
| 222 | + DECLARE_NAPI_PROPERTY("DoRepeatedWork", DoRepeatedWork), |
180 | 223 | };
|
181 | 224 |
|
182 | 225 | NAPI_CALL(env, napi_define_properties(
|
|
0 commit comments