12
12
13
13
/* **** HELPER FUNCTIONS *****/
14
14
15
+ // Dummy value to pass into function parameter for ThreadSafeFunction
16
+ Napi::Value NoOp (const Napi::CallbackInfo& info) { return info.Env ().Undefined (); }
17
+
15
18
NSString * GetUserHomeFolderPath () {
16
19
NSString * path;
17
20
BOOL isSandboxed = (nil != NSProcessInfo .processInfo .environment [@" APP_SANDBOX_CONTAINER_ID" ]);
151
154
}
152
155
153
156
// Request access to the Contacts store.
154
- void AskForContactsAccess (const Napi::CallbackInfo& info) {
157
+ Napi::Promise AskForContactsAccess (const Napi::CallbackInfo& info) {
155
158
Napi::Env env = info.Env ();
159
+ Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New (env);
156
160
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New (env,
157
- info[ 0 ]. As < Napi::Function>( ),
158
- " Resource Name " ,
161
+ Napi::Function::New (env, NoOp ),
162
+ " contactsCallback " ,
159
163
0 ,
160
164
1 ,
161
165
[](Napi::Env){});
@@ -164,53 +168,60 @@ void AskForContactsAccess(const Napi::CallbackInfo& info) {
164
168
CNContactStore *store = [CNContactStore new ];
165
169
[store requestAccessForEntityType: CNEntityTypeContacts
166
170
completionHandler: ^(BOOL granted, NSError * error) {
167
- auto callback = [](Napi::Env env, Napi::Function js_cb, const char * granted) {
168
- js_cb. Call ({ Napi::String::New (env, granted)} );
171
+ auto callback = [= ](Napi::Env env, Napi::Function js_cb, const char * granted) {
172
+ deferred. Resolve ( Napi::String::New (env, granted));
169
173
};
170
174
ts_fn.BlockingCall (granted ? " authorized" : " denied" , callback);
171
175
}];
172
176
} else {
173
- Napi::FunctionReference fn = Napi::Persistent (info[0 ].As <Napi::Function>());
174
- fn.Call ({Napi::String::New (env, " authorized" )});
177
+ deferred.Resolve (Napi::String::New (env, " authorized" ));
175
178
}
179
+
180
+ return deferred.Promise ();
176
181
}
177
182
178
183
// Request access to Calendar.
179
- void AskForCalendarAccess (const Napi::CallbackInfo& info) {
184
+ Napi::Promise AskForCalendarAccess (const Napi::CallbackInfo& info) {
180
185
Napi::Env env = info.Env ();
186
+ Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New (env);
181
187
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New (env,
182
- info[ 0 ]. As < Napi::Function>( ),
183
- " Resource Name " ,
188
+ Napi::Function::New (env, NoOp ),
189
+ " calendarCallback " ,
184
190
0 ,
185
191
1 ,
186
192
[](Napi::Env){});
187
193
188
194
[[EKEventStore new ] requestAccessToEntityType: EKEntityTypeEvent
189
195
completion: ^(BOOL granted, NSError * error) {
190
- auto callback = [](Napi::Env env, Napi::Function js_cb, const char * granted) {
191
- js_cb. Call ({ Napi::String::New (env, granted)} );
196
+ auto callback = [= ](Napi::Env env, Napi::Function js_cb, const char * granted) {
197
+ deferred. Resolve ( Napi::String::New (env, granted));
192
198
};
193
199
ts_fn.BlockingCall (granted ? " authorized" : " denied" , callback);
194
200
}];
201
+
202
+ return deferred.Promise ();
195
203
}
196
204
197
205
// Request access to Reminders.
198
- void AskForRemindersAccess (const Napi::CallbackInfo& info) {
206
+ Napi::Promise AskForRemindersAccess (const Napi::CallbackInfo& info) {
199
207
Napi::Env env = info.Env ();
208
+ Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New (env);
200
209
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New (env,
201
- info[ 0 ]. As < Napi::Function>( ),
202
- " Resource Name " ,
210
+ Napi::Function::New (env, NoOp ),
211
+ " remindersCallback " ,
203
212
0 ,
204
213
1 ,
205
214
[](Napi::Env){});
206
215
207
216
[[EKEventStore new ] requestAccessToEntityType: EKEntityTypeReminder
208
- completion: ^(BOOL granted, NSError * error) {
209
- auto callback = [](Napi::Env env, Napi::Function js_cb , const char * granted) {
210
- js_cb. Call ({ Napi::String::New (env, granted)} );
217
+ completion: ^(BOOL granted, NSError * error) {
218
+ auto callback = [= ](Napi::Env env, Napi::Function prom_cb , const char * granted) {
219
+ deferred. Resolve ( Napi::String::New (env, granted));
211
220
};
212
221
ts_fn.BlockingCall (granted ? " authorized" : " denied" , callback);
213
222
}];
223
+
224
+ return deferred.Promise ();
214
225
}
215
226
216
227
// Request Full Disk Access.
@@ -221,12 +232,13 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
221
232
}
222
233
223
234
// Request access to either the Camera or the Microphone.
224
- void AskForMediaAccess (const Napi::CallbackInfo& info) {
235
+ Napi::Promise AskForMediaAccess (const Napi::CallbackInfo& info) {
225
236
Napi::Env env = info.Env ();
226
237
const std::string type = info[0 ].As <Napi::String>().Utf8Value ();
238
+ Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New (env);
227
239
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New (env,
228
- info[ 1 ]. As < Napi::Function>( ),
229
- " Resource Name " ,
240
+ Napi::Function::New (env, NoOp ),
241
+ " mediaAccessCallback " ,
230
242
0 ,
231
243
1 ,
232
244
[](Napi::Env){});
@@ -235,15 +247,16 @@ void AskForMediaAccess(const Napi::CallbackInfo& info) {
235
247
AVMediaType media_type = (type == " microphone" ) ? AVMediaTypeAudio : AVMediaTypeVideo;
236
248
[AVCaptureDevice requestAccessForMediaType: media_type
237
249
completionHandler: ^(BOOL granted) {
238
- auto callback = [](Napi::Env env, Napi::Function js_cb, const char * granted) {
239
- js_cb. Call ({ Napi::String::New (env, granted)} );
250
+ auto callback = [= ](Napi::Env env, Napi::Function js_cb, const char * granted) {
251
+ deferred. Resolve ( Napi::String::New (env, granted));
240
252
};
241
253
ts_fn.BlockingCall (granted ? " authorized" : " denied" , callback);
242
254
}];
243
255
} else {
244
- Napi::FunctionReference fn = Napi::Persistent (info[0 ].As <Napi::Function>());
245
- fn.Call ({Napi::String::New (env, " authorized" )});
256
+ deferred.Resolve (Napi::String::New (env, " authorized" ));
246
257
}
258
+
259
+ return deferred.Promise ();
247
260
}
248
261
249
262
// Initializes all functions exposed to JS
0 commit comments