Skip to content

Commit 0412ea6

Browse files
committed
perf: cache contacts until changed
1 parent c8ca168 commit 0412ea6

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

contacts.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
Napi::ThreadSafeFunction ts_fn;
66
id observer = nil;
77

8+
Napi::Reference<Napi::Array> contacts_ref;
9+
810
/***** HELPERS *****/
911

1012
// Dummy value to pass into function parameter for ThreadSafeFunction.
@@ -493,6 +495,9 @@ CNAuthorizationStatus AuthStatus() {
493495
if (AuthStatus() != CNAuthorizationStatusAuthorized)
494496
return Napi::Array::New(env);
495497

498+
if (!contacts_ref.IsEmpty())
499+
return contacts_ref.Value();
500+
496501
CNContactStore *addressBook = [[CNContactStore alloc] init];
497502
Napi::Array extra_keys = info[0].As<Napi::Array>();
498503

@@ -536,6 +541,8 @@ CNAuthorizationStatus AuthStatus() {
536541
contacts[i] = CreateContact(env, cncontact);
537542
}
538543

544+
contacts_ref = Napi::Persistent(contacts);
545+
539546
return contacts;
540547
}
541548

@@ -647,6 +654,7 @@ CNAuthorizationStatus AuthStatus() {
647654
object:nil
648655
queue:[NSOperationQueue mainQueue]
649656
usingBlock:^(NSNotification *note) {
657+
contacts_ref.Reset();
650658
auto callback = [](Napi::Env env, Napi::Function js_cb,
651659
const char *value) {
652660
js_cb.Call({Napi::String::New(env, value)});

0 commit comments

Comments
 (0)