Skip to content

Commit

Permalink
Merge pull request #32 from MarshallOfSound/context-aware
Browse files Browse the repository at this point in the history
chore: make context aware
  • Loading branch information
CharlieHess committed May 16, 2019
2 parents 525c6ac + ea6f319 commit 4da30ff
Show file tree
Hide file tree
Showing 5 changed files with 1,026 additions and 375 deletions.
4 changes: 4 additions & 0 deletions Notification.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
// Notification.cc represents the top level of the module.
// C++ constructs that are exposed to javascript are exported here

#if NODE_MAJOR_VERSION >= 10
NAN_MODULE_WORKER_ENABLED(Notification, MacNotification::Init)
#else
NODE_MODULE(Notification, MacNotification::Init)
#endif
14 changes: 14 additions & 0 deletions mac_notification.mm
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@
NotificationCenterDelegate *delegate = [[NotificationCenterDelegate alloc] initWithActivationCallback:activated];
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
center.delegate = delegate;

#if NODE_MAJOR_VERSION >= 10
node::AddEnvironmentCleanupHook(
v8::Isolate::GetCurrent(),
[](void *arg) {
auto our_delegate = static_cast<NotificationCenterDelegate*>(arg);
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
if (center.delegate == our_delegate) {
center.delegate = nil;
}
},
const_cast<void*>(static_cast<const void*>(delegate))
);
#endif
}

Nan::Utf8String* MacNotification::StringFromObjectOrNull(Local<Object> object, const char *key) {
Expand Down
3 changes: 2 additions & 1 deletion notification_center_delegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ static void AsyncSendHandler(uv_async_t *handle) {
Nan::New(info->id).ToLocalChecked()
};

info->callback->Call(3, argv);
Nan::AsyncResource async_resource("NodeMacNotifier:AsyncSendHandler");
info->callback->Call(3, argv, &async_resource);

delete info;
info = nullptr;
Expand Down
Loading

0 comments on commit 4da30ff

Please sign in to comment.