222
222
Napi::Promise AskForContactsAccess (const Napi::CallbackInfo &info) {
223
223
Napi::Env env = info.Env ();
224
224
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New (env);
225
- Napi::ThreadSafeFunction ts_fn =
226
- Napi::ThreadSafeFunction::New (env, Napi::Function::New (env, NoOp),
227
- " contactsCallback" , 0 , 1 );
225
+ Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New (
226
+ env, Napi::Function::New (env, NoOp), " contactsCallback" , 0 , 1 );
228
227
228
+ __block Napi::ThreadSafeFunction tsfn = ts_fn;
229
229
if (@available (macOS 10.11 , *)) {
230
230
CNContactStore *store = [CNContactStore new ];
231
231
[store requestAccessForEntityType: CNEntityTypeContacts
234
234
const char *granted) {
235
235
deferred.Resolve (Napi::String::New (env, granted));
236
236
};
237
- ts_fn.BlockingCall (granted ? " authorized" : " denied" ,
238
- callback);
237
+ tsfn.BlockingCall (granted ? " authorized" : " denied" ,
238
+ callback);
239
+ tsfn.Release ();
239
240
}];
240
241
} else {
241
242
deferred.Resolve (Napi::String::New (env, " authorized" ));
248
249
Napi::Promise AskForCalendarAccess (const Napi::CallbackInfo &info) {
249
250
Napi::Env env = info.Env ();
250
251
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New (env);
251
- Napi::ThreadSafeFunction ts_fn =
252
- Napi::ThreadSafeFunction::New (env, Napi::Function::New (env, NoOp),
253
- " calendarCallback" , 0 , 1 );
252
+ Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New (
253
+ env, Napi::Function::New (env, NoOp), " calendarCallback" , 0 , 1 );
254
254
255
+ __block Napi::ThreadSafeFunction tsfn = ts_fn;
255
256
[[EKEventStore new ]
256
257
requestAccessToEntityType: EKEntityTypeEvent
257
258
completion: ^(BOOL granted, NSError *error) {
258
259
auto callback = [=](Napi::Env env, Napi::Function js_cb,
259
260
const char *granted) {
260
261
deferred.Resolve (Napi::String::New (env, granted));
261
262
};
262
- ts_fn.BlockingCall (granted ? " authorized" : " denied" ,
263
- callback);
263
+ tsfn.BlockingCall (granted ? " authorized" : " denied" ,
264
+ callback);
265
+ tsfn.Release ();
264
266
}];
265
267
266
268
return deferred.Promise ();
273
275
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New (
274
276
env, Napi::Function::New (env, NoOp), " remindersCallback" , 0 , 1 );
275
277
278
+ __block Napi::ThreadSafeFunction tsfn = ts_fn;
276
279
[[EKEventStore new ]
277
280
requestAccessToEntityType: EKEntityTypeReminder
278
281
completion: ^(BOOL granted, NSError *error) {
281
284
const char *granted) {
282
285
deferred.Resolve (Napi::String::New (env, granted));
283
286
};
284
- ts_fn.BlockingCall (granted ? " authorized" : " denied" ,
285
- callback);
287
+ tsfn.BlockingCall (granted ? " authorized" : " denied" ,
288
+ callback);
289
+ tsfn.Release ();
286
290
}];
287
291
288
292
return deferred.Promise ();
@@ -307,6 +311,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
307
311
std::string auth_status = MediaAuthStatus (" camera" );
308
312
309
313
if (auth_status == " not determined" ) {
314
+ __block Napi::ThreadSafeFunction tsfn = ts_fn;
310
315
[AVCaptureDevice
311
316
requestAccessForMediaType: AVMediaTypeVideo
312
317
completionHandler: ^(BOOL granted) {
@@ -315,8 +320,9 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
315
320
deferred.Resolve (Napi::String::New (env, granted));
316
321
};
317
322
318
- ts_fn.BlockingCall (granted ? " authorized" : " denied" ,
319
- callback);
323
+ tsfn.BlockingCall (granted ? " authorized" : " denied" ,
324
+ callback);
325
+ tsfn.Release ();
320
326
}];
321
327
} else if (auth_status == " denied" ) {
322
328
NSWorkspace *workspace = [[NSWorkspace alloc ] init ];
@@ -347,6 +353,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
347
353
std::string auth_status = MediaAuthStatus (" microphone" );
348
354
349
355
if (auth_status == " not determined" ) {
356
+ __block Napi::ThreadSafeFunction tsfn = ts_fn;
350
357
[AVCaptureDevice
351
358
requestAccessForMediaType: AVMediaTypeAudio
352
359
completionHandler: ^(BOOL granted) {
@@ -355,8 +362,9 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
355
362
deferred.Resolve (Napi::String::New (env, granted));
356
363
};
357
364
358
- ts_fn.BlockingCall (granted ? " authorized" : " denied" ,
359
- callback);
365
+ tsfn.BlockingCall (granted ? " authorized" : " denied" ,
366
+ callback);
367
+ tsfn.Release ();
360
368
}];
361
369
} else if (auth_status == " denied" ) {
362
370
NSWorkspace *workspace = [[NSWorkspace alloc ] init ];
@@ -379,7 +387,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
379
387
// Request Screen Capture Access.
380
388
void AskForScreenCaptureAccess (const Napi::CallbackInfo &info) {
381
389
if (@available (macOS 10.15 , *)) {
382
- // Tries to create a capture stream. This is necessary to add the app back
390
+ // Tries to create a capture stream. This is necessary to add the app back
383
391
// to the list in sysprefs if the user previously denied.
384
392
// https://stackoverflow.com/questions/56597221/detecting-screen-recording-settings-on-macos-catalina
385
393
CGDisplayStreamRef stream = CGDisplayStreamCreate (
0 commit comments