Skip to content

Commit

Permalink
feat: expose external parameter to contact event
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Aug 23, 2023
1 parent f18f6eb commit 0eaaae7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ console.log(`Contact was ${updated ? 'updated' : 'not updated'}.`)

### `contacts.listener`

This module exposes an `EventEmitter`, which can be used to listen to potential changes to the `CNContactStore`. When a contact is changed either with methods contained in this module, or manually by a user, the `contact-changed` event will be emitted.
This module exposes an `EventEmitter`, which can be used to listen to potential changes to the `CNContactStore`. When a contact is changed either with methods contained in this module, or manually by a user, the `contact-changed` event will be emitted with one parameter `external`. This signifies whether or not the change to contact data originated outside the current app.

Owing to the underlying architecture of this module, the listener must be manually managed; before use you must initialize it with `listener.setup()` and when you are finished listening for events you must remove it with `listener.remove()`. To check if a listener is currently active, use `listener.isListening()`.

Expand All @@ -274,8 +274,8 @@ addNewContact({
emailAddresses: ['[email protected]'],
})

listener.once('contact-changed', () => {
console.log('A contact was changed!')
listener.once('contact-changed', (external) => {
console.log(`A contact was changed ${external ? 'outside of' : 'within'} this app!`)
listener.remove()
})
```
12 changes: 9 additions & 3 deletions contacts.mm
Original file line number Diff line number Diff line change
Expand Up @@ -665,10 +665,16 @@ CNAuthorizationStatus AuthStatus() {
return;

contacts_ref.Reset();
auto callback = [](Napi::Env env, Napi::Function js_cb,
const char *value) {
js_cb.Call({Napi::String::New(env, value)});
bool external =
[[info objectForKey:@"CNNotificationOriginationExternally"]
boolValue];

auto callback = [external](Napi::Env env, Napi::Function js_cb,
const char *value) {
js_cb.Call({Napi::String::New(env, value),
Napi::Boolean::New(env, external)});
};

ts_fn.BlockingCall("contact-changed", callback);
}];

Expand Down

0 comments on commit 0eaaae7

Please sign in to comment.