From d5ae329d8a5e6fd1e3fa990fc3ed7c544954156c Mon Sep 17 00:00:00 2001 From: Scott Kyle Date: Mon, 12 Oct 2015 23:21:13 -0700 Subject: [PATCH] Add Notification constructor and destructor These handle the life cycle of the member variables. --- src/RJSRealm.mm | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/RJSRealm.mm b/src/RJSRealm.mm index 0efcd38b..dff5b647 100644 --- a/src/RJSRealm.mm +++ b/src/RJSRealm.mm @@ -373,6 +373,18 @@ namespace realm { JSObjectRef realmObject; JSObjectRef callbackObject; RJSRealmDelegate::NotificationFunction func; + + Notification(JSGlobalContextRef c, JSObjectRef r, JSObjectRef cb, RJSRealmDelegate::NotificationFunction f) : ctx(c), realmObject(r), callbackObject(cb), func(f) { + JSGlobalContextRetain(ctx); + JSValueProtect(ctx, realmObject); + JSValueProtect(ctx, callbackObject); + } + + ~Notification() { + JSValueUnprotect(ctx, callbackObject); + JSValueUnprotect(ctx, realmObject); + JSGlobalContextRelease(ctx); + } }; } @@ -382,10 +394,7 @@ JSValueRef RealmAddNotification(JSContextRef ctx, JSObjectRef function, JSObject JSObjectRef callback = RJSValidatedValueToFunction(ctx, arguments[0]); SharedRealm realm = *RJSGetInternal(thisObject); - JSGlobalContextRef gCtx = JSGlobalContextRetain(JSContextGetGlobalContext(ctx)); - - JSValueProtect(gCtx, thisObject); - JSValueProtect(gCtx, callback); + JSGlobalContextRef gCtx = JSContextGetGlobalContext(ctx); RJSRealmDelegate::NotificationFunction func = std::make_shared>([=](std::string notification_name) { JSValueRef arguments[2]; @@ -424,16 +433,6 @@ JSValueRef RealmClose(JSContextRef ctx, JSObjectRef function, JSObjectRef thisOb return NULL; } -void RJSNotificationFinalize(JSObjectRef object) { - Notification *notification = RJSGetInternal(object); - JSGlobalContextRef ctx = notification->ctx; - - JSValueUnprotect(ctx, notification->callbackObject); - JSValueUnprotect(ctx, notification->realmObject); - JSGlobalContextRelease(ctx); - RJSFinalize(object); -} - const JSStaticFunction RJSRealmFuncs[] = { {"objects", RealmObjects, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"create", RealmCreateObject, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, @@ -451,7 +450,7 @@ JSClassRef RJSRealmClass() { } JSClassRef RJSNotificationClass() { - static JSClassRef s_notificationClass = RJSCreateWrapperClass("Notification", NULL, NULL, NULL, RJSNotificationFinalize); + static JSClassRef s_notificationClass = RJSCreateWrapperClass("Notification", NULL, NULL, NULL); return s_notificationClass; }