From 246ccc7bde82dd2929da42c64d675c284be89be2 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Mon, 26 Oct 2015 16:15:46 -0700 Subject: [PATCH] add other notification methods --- src/RJSRealm.mm | 40 ++++++++++++++++++++++++++++++++++++++-- tests/RealmTests.js | 22 +++++++++++++++++++++- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/RJSRealm.mm b/src/RJSRealm.mm index 5b6560c1..0ac77cd8 100644 --- a/src/RJSRealm.mm +++ b/src/RJSRealm.mm @@ -423,7 +423,7 @@ JSValueRef RealmWrite(JSContextRef ctx, JSObjectRef function, JSObjectRef thisOb return NULL; } -JSValueRef RealmAddNotification(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { +JSValueRef RealmAddListener(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { try { RJSValidateArgumentCount(argumentCount, 1); @@ -440,6 +440,40 @@ JSValueRef RealmAddNotification(JSContextRef ctx, JSObjectRef function, JSObject } } +JSValueRef RealmRemoveListener(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { + try { + RJSValidateArgumentCount(argumentCount, 1); + + JSObjectRef callback = RJSValidatedValueToFunction(ctx, arguments[0]); + SharedRealm realm = *RJSGetInternal(thisObject); + static_cast(realm->m_delegate.get())->remove_notification(callback); + return NULL; + } + catch (std::exception &exp) { + if (jsException) { + *jsException = RJSMakeError(ctx, exp); + } + return NULL; + } +} + +JSValueRef RealmRemoveAllListeners(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { + try { + RJSValidateArgumentCount(argumentCount, 0); + SharedRealm realm = *RJSGetInternal(thisObject); + static_cast(realm->m_delegate.get())->remove_all_notifications(); + return NULL; + } + catch (std::exception &exp) { + if (jsException) { + *jsException = RJSMakeError(ctx, exp); + } + return NULL; + } +} + + + JSValueRef RealmClose(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { try { RJSValidateArgumentCount(argumentCount, 0); @@ -462,7 +496,9 @@ static const JSStaticFunction RJSRealmFuncs[] = { {"delete", RealmDelete, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"deleteAll", RealmDeleteAll, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"write", RealmWrite, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, - {"addNotification", RealmAddNotification, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"addListener", RealmAddListener, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"removeListener", RealmRemoveListener, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"removeAllListeners", RealmRemoveAllListeners, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"close", RealmClose, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL}, }; diff --git a/tests/RealmTests.js b/tests/RealmTests.js index b75360f2..258366c8 100644 --- a/tests/RealmTests.js +++ b/tests/RealmTests.js @@ -282,7 +282,7 @@ module.exports = BaseTest.extend({ var notificationCount = 0; var notificationName; - var notification = realm.addNotification(function(realm, name) { + realm.addListener(function(realm, name) { notificationCount++; notificationName = name; }); @@ -291,5 +291,25 @@ module.exports = BaseTest.extend({ realm.write(function() {}); TestCase.assertEqual(notificationCount, 1); TestCase.assertEqual(notificationName, 'DidChangeNotification'); + + var secondNotificationCount = 0; + function secondNotification(realm, name) { + secondNotificationCount++; + }; + realm.addListener(secondNotification) + + realm.write(function() {}); + TestCase.assertEqual(notificationCount, 2); + TestCase.assertEqual(secondNotificationCount, 1); + + realm.removeListener(secondNotification); + realm.write(function() {}); + TestCase.assertEqual(notificationCount, 3); + TestCase.assertEqual(secondNotificationCount, 1); + + realm.removeAllListeners(); + realm.write(function() {}); + TestCase.assertEqual(notificationCount, 3); + TestCase.assertEqual(secondNotificationCount, 1); }, });