Skip to content

Commit b667786

Browse files
committed
feat: convert to promise semantics
1 parent 3e03bf6 commit b667786

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

permissions.mm

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
/***** HELPER FUNCTIONS *****/
1414

15+
// Dummy value to pass into function parameter for ThreadSafeFunction
16+
Napi::Value NoOp(const Napi::CallbackInfo& info) { return info.Env().Undefined(); }
17+
1518
NSString* GetUserHomeFolderPath() {
1619
NSString* path;
1720
BOOL isSandboxed = (nil != NSProcessInfo.processInfo.environment[@"APP_SANDBOX_CONTAINER_ID"]);
@@ -151,11 +154,12 @@
151154
}
152155

153156
// Request access to the Contacts store.
154-
void AskForContactsAccess(const Napi::CallbackInfo& info) {
157+
Napi::Promise AskForContactsAccess(const Napi::CallbackInfo& info) {
155158
Napi::Env env = info.Env();
159+
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
156160
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",
159163
0,
160164
1,
161165
[](Napi::Env){});
@@ -164,53 +168,60 @@ void AskForContactsAccess(const Napi::CallbackInfo& info) {
164168
CNContactStore *store = [CNContactStore new];
165169
[store requestAccessForEntityType:CNEntityTypeContacts
166170
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));
169173
};
170174
ts_fn.BlockingCall(granted ? "authorized" : "denied", callback);
171175
}];
172176
} 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"));
175178
}
179+
180+
return deferred.Promise();
176181
}
177182

178183
// Request access to Calendar.
179-
void AskForCalendarAccess(const Napi::CallbackInfo& info) {
184+
Napi::Promise AskForCalendarAccess(const Napi::CallbackInfo& info) {
180185
Napi::Env env = info.Env();
186+
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
181187
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",
184190
0,
185191
1,
186192
[](Napi::Env){});
187193

188194
[[EKEventStore new] requestAccessToEntityType:EKEntityTypeEvent
189195
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));
192198
};
193199
ts_fn.BlockingCall(granted ? "authorized" : "denied", callback);
194200
}];
201+
202+
return deferred.Promise();
195203
}
196204

197205
// Request access to Reminders.
198-
void AskForRemindersAccess(const Napi::CallbackInfo& info) {
206+
Napi::Promise AskForRemindersAccess(const Napi::CallbackInfo& info) {
199207
Napi::Env env = info.Env();
208+
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
200209
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",
203212
0,
204213
1,
205214
[](Napi::Env){});
206215

207216
[[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));
211220
};
212221
ts_fn.BlockingCall(granted ? "authorized" : "denied", callback);
213222
}];
223+
224+
return deferred.Promise();
214225
}
215226

216227
// Request Full Disk Access.
@@ -221,12 +232,13 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
221232
}
222233

223234
// Request access to either the Camera or the Microphone.
224-
void AskForMediaAccess(const Napi::CallbackInfo& info) {
235+
Napi::Promise AskForMediaAccess(const Napi::CallbackInfo& info) {
225236
Napi::Env env = info.Env();
226237
const std::string type = info[0].As<Napi::String>().Utf8Value();
238+
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
227239
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",
230242
0,
231243
1,
232244
[](Napi::Env){});
@@ -235,15 +247,16 @@ void AskForMediaAccess(const Napi::CallbackInfo& info) {
235247
AVMediaType media_type = (type == "microphone") ? AVMediaTypeAudio : AVMediaTypeVideo;
236248
[AVCaptureDevice requestAccessForMediaType:media_type
237249
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));
240252
};
241253
ts_fn.BlockingCall(granted ? "authorized" : "denied", callback);
242254
}];
243255
} 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"));
246257
}
258+
259+
return deferred.Promise();
247260
}
248261

249262
// Initializes all functions exposed to JS

0 commit comments

Comments
 (0)