Skip to content

Commit 0ee3179

Browse files
Commit
1 parent c185b38 commit 0ee3179

File tree

3 files changed

+44
-62
lines changed

3 files changed

+44
-62
lines changed

dist/addon.node

-37 KB
Binary file not shown.

native-src/sync_root_interface/callbacks/CancelFetchData/CancelFetchDataCallback.cpp

Lines changed: 41 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@
99

1010
napi_threadsafe_function g_cancel_fetch_data_threadsafe_callback = nullptr;
1111

12-
struct CallbackContext {
12+
struct CallbackContext
13+
{
1314
std::mutex mtx;
1415
std::condition_variable cv;
1516
bool ready = false;
1617
};
1718

18-
struct CancelFetchDataArgs {
19+
struct CancelFetchDataArgs
20+
{
1921
std::wstring fileIdentityArg;
20-
CallbackContext* context;
21-
22-
CancelFetchDataArgs(const std::wstring& fileId, CallbackContext* ctx)
22+
CallbackContext *context;
23+
24+
CancelFetchDataArgs(const std::wstring &fileId, CallbackContext *ctx)
2325
: fileIdentityArg(fileId), context(ctx) {}
2426
};
2527

@@ -31,19 +33,11 @@ void notify_cancel_fetch_data_call(napi_env env, napi_value js_callback, void *c
3133
napi_value js_string;
3234
napi_create_string_utf16(env, u16_fileIdentity.c_str(), u16_fileIdentity.size(), &js_string);
3335

34-
napi_value args_to_js_callback_cancel_fetch[1] = {js_string};
36+
napi_value args_to_js_callback[1] = {js_string};
3537

3638
napi_value undefined;
3739
napi_get_undefined(env, &undefined);
38-
napi_value result;
39-
40-
Logger::getInstance().log("Executed to call JS function in cancelFetchCallback.", LogLevel::ERROR);
41-
napi_status status = napi_call_function(env, undefined, js_callback, 1, args_to_js_callback_cancel_fetch, &result);
42-
if (status != napi_ok)
43-
{
44-
fprintf(stderr, "Failed to call JS function.\n");
45-
Logger::getInstance().log("Failed to call JS function in cancelFetchCallback.", LogLevel::ERROR);
46-
}
40+
napi_call_function(env, undefined, js_callback, 1, args_to_js_callback, nullptr);
4741

4842
{
4943
std::lock_guard<std::mutex> lock(args->context->mtx);
@@ -54,18 +48,47 @@ void notify_cancel_fetch_data_call(napi_env env, napi_value js_callback, void *c
5448
delete args;
5549
}
5650

51+
void CALLBACK cancel_fetch_data_callback_wrapper(_In_ CONST CF_CALLBACK_INFO *callbackInfo, _In_ CONST CF_CALLBACK_PARAMETERS *callbackParameters)
52+
{
53+
LPCVOID fileIdentity = callbackInfo->FileIdentity;
54+
DWORD fileIdentityLength = callbackInfo->FileIdentityLength;
55+
56+
const wchar_t *wchar_ptr = static_cast<const wchar_t *>(fileIdentity);
57+
std::wstring fileIdentityStr(wchar_ptr, fileIdentityLength / sizeof(wchar_t));
58+
59+
CallbackContext context;
60+
CancelFetchDataArgs *args = new CancelFetchDataArgs(fileIdentityStr, &context);
61+
62+
napi_call_threadsafe_function(g_cancel_fetch_data_threadsafe_callback, args, napi_tsfn_blocking);
63+
64+
{
65+
std::unique_lock<std::mutex> lock(context.mtx);
66+
auto timeout = std::chrono::seconds(30);
67+
68+
if (context.cv.wait_for(lock, timeout, [&context]
69+
{ return context.ready; }))
70+
{
71+
wprintf(L"Cancel fetch completed\n");
72+
}
73+
else
74+
{
75+
wprintf(L"Cancel fetch timed out\n");
76+
}
77+
}
78+
}
79+
5780
void register_threadsafe_cancel_fetch_data_callback(const std::string &resource_name, napi_env env, InputSyncCallbacks input)
5881
{
5982
std::u16string converted_resource_name(resource_name.begin(), resource_name.end());
6083

6184
napi_value resource_name_value;
6285
napi_create_string_utf16(env, converted_resource_name.c_str(), NAPI_AUTO_LENGTH, &resource_name_value);
6386

64-
napi_threadsafe_function tsfn_cancel_fetch_data;
6587
napi_value cancel_fetch_data_value;
6688
napi_get_reference_value(env, input.cancel_fetch_data_callback_ref, &cancel_fetch_data_value);
6789

68-
napi_status status = napi_create_threadsafe_function(
90+
napi_threadsafe_function tsfn_cancel_fetch_data;
91+
napi_create_threadsafe_function(
6992
env,
7093
cancel_fetch_data_value,
7194
NULL,
@@ -78,46 +101,5 @@ void register_threadsafe_cancel_fetch_data_callback(const std::string &resource_
78101
notify_cancel_fetch_data_call,
79102
&tsfn_cancel_fetch_data);
80103

81-
if (status != napi_ok)
82-
{
83-
napi_throw_error(env, nullptr, "Failed to create cancel fetch data threadsafe function");
84-
return;
85-
}
86-
87104
g_cancel_fetch_data_threadsafe_callback = tsfn_cancel_fetch_data;
88-
}
89-
90-
void CALLBACK cancel_fetch_data_callback_wrapper(
91-
_In_ CONST CF_CALLBACK_INFO *callbackInfo,
92-
_In_ CONST CF_CALLBACK_PARAMETERS *callbackParameters)
93-
{
94-
printf("cancel_fetch_data_callback_wrapper called\n");
95-
96-
LPCVOID fileIdentity = callbackInfo->FileIdentity;
97-
DWORD fileIdentityLength = callbackInfo->FileIdentityLength;
98-
99-
const wchar_t *wchar_ptr = static_cast<const wchar_t *>(fileIdentity);
100-
std::wstring fileIdentityStr(wchar_ptr, fileIdentityLength / sizeof(wchar_t));
101-
102-
if (g_cancel_fetch_data_threadsafe_callback == nullptr)
103-
{
104-
wprintf(L"Callback fetch_data_callback_wrapper called but g_fetch_data_threadsafe_callback is null\n");
105-
return;
106-
}
107-
108-
CallbackContext context;
109-
CancelFetchDataArgs *args = new CancelFetchDataArgs(fileIdentityStr, &context);
110-
111-
napi_call_threadsafe_function(g_cancel_fetch_data_threadsafe_callback, args, napi_tsfn_blocking);
112-
113-
{
114-
std::unique_lock<std::mutex> lock(context.mtx);
115-
auto timeout = std::chrono::seconds(30);
116-
117-
if (context.cv.wait_for(lock, timeout, [&context] { return context.ready; })) {
118-
wprintf(L"Cancel fetch completed\n");
119-
} else {
120-
wprintf(L"Cancel fetch timed out\n");
121-
}
122-
}
123-
}
105+
}

native-src/sync_root_interface/callbacks/FetchData/FetchData.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ void notify_fetch_data_call(napi_env env, napi_value js_callback, void *context,
111111
napi_value js_path;
112112
napi_create_string_utf16(env, (char16_t *)ctx->path.c_str(), ctx->path.length(), &js_path);
113113

114-
napi_value js_callback;
115-
napi_create_function(env, "callback", NAPI_AUTO_LENGTH, response_callback_fn_fetch_data_wrapper, ctx, &js_callback);
114+
napi_value js_callback_fn;
115+
napi_create_function(env, "callback", NAPI_AUTO_LENGTH, response_callback_fn_fetch_data_wrapper, ctx, &js_callback_fn);
116116

117-
napi_value args_to_js_callback[2] = {js_path, js_callback};
117+
napi_value args_to_js_callback[2] = {js_path, js_callback_fn};
118118

119119
{
120120
std::unique_lock<std::mutex> lock(ctx->mtx);

0 commit comments

Comments
 (0)