diff --git a/lib/realm.js b/lib/realm.js index 6a22be88..7b90e35d 100644 --- a/lib/realm.js +++ b/lib/realm.js @@ -50,25 +50,34 @@ class Realm { }); } - addListener(callback) { + addListener(name, callback) { if (typeof callback != 'function') { throw new Error('Realm.addListener must be passed a function!'); } + if (name != 'change') { + throw new Error("Only 'change' notification is supported."); + } this[notificationsKey].push(callback); } - removeListener(callback) { + removeListener(name, callback) { if (typeof callback != 'function') { throw new Error('Realm.addListener must be passed a function!'); } + if (name != 'change') { + throw new Error("Only 'change' notification is supported."); + } var index = 0; while((index = this[notificationsKey].indexOf(callback, index)) != -1) { this[notificationsKey].splice(index, 1); }; } - removeAllListeners() { + removeAllListeners(name) { + if (name != undefined && name != 'change') { + throw new Error("Only 'change' notification is supported."); + } this[notificationsKey] = []; } diff --git a/src/RJSRealm.mm b/src/RJSRealm.mm index b999961b..f609abec 100644 --- a/src/RJSRealm.mm +++ b/src/RJSRealm.mm @@ -423,11 +423,20 @@ JSValueRef RealmWrite(JSContextRef ctx, JSObjectRef function, JSObjectRef thisOb return NULL; } +std::string RJSValidatedNotificationName(JSContextRef ctx, JSValueRef value) { + std::string name = RJSValidatedStringForValue(ctx, value); + if (name != "change") { + throw std::runtime_error("Only the 'change' notification name is supported."); + } + return name; +} + JSValueRef RealmAddListener(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { try { - RJSValidateArgumentCount(argumentCount, 1); + RJSValidateArgumentCount(argumentCount, 2); + __unused std::string name = RJSValidatedNotificationName(ctx, arguments[0]); + JSObjectRef callback = RJSValidatedValueToFunction(ctx, arguments[1]); - JSObjectRef callback = RJSValidatedValueToFunction(ctx, arguments[0]); SharedRealm realm = *RJSGetInternal(thisObject); static_cast(realm->m_delegate.get())->add_notification(callback); return NULL; @@ -442,9 +451,10 @@ JSValueRef RealmAddListener(JSContextRef ctx, JSObjectRef function, JSObjectRef JSValueRef RealmRemoveListener(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { try { - RJSValidateArgumentCount(argumentCount, 1); + RJSValidateArgumentCount(argumentCount, 2); + __unused std::string name = RJSValidatedNotificationName(ctx, arguments[0]); + JSObjectRef callback = RJSValidatedValueToFunction(ctx, arguments[1]); - JSObjectRef callback = RJSValidatedValueToFunction(ctx, arguments[0]); SharedRealm realm = *RJSGetInternal(thisObject); static_cast(realm->m_delegate.get())->remove_notification(callback); return NULL; @@ -459,7 +469,11 @@ JSValueRef RealmRemoveListener(JSContextRef ctx, JSObjectRef function, JSObjectR JSValueRef RealmRemoveAllListeners(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { try { - RJSValidateArgumentCount(argumentCount, 0); + RJSValidateArgumentRange(argumentCount, 0, 1); + if (argumentCount) { + RJSValidatedNotificationName(ctx, arguments[0]); + } + SharedRealm realm = *RJSGetInternal(thisObject); static_cast(realm->m_delegate.get())->remove_all_notifications(); return NULL; diff --git a/tests/RealmTests.js b/tests/RealmTests.js index 74f04135..84f21971 100644 --- a/tests/RealmTests.js +++ b/tests/RealmTests.js @@ -282,7 +282,7 @@ module.exports = BaseTest.extend({ var notificationCount = 0; var notificationName; - realm.addListener(function(realm, name) { + realm.addListener('change', function(realm, name) { notificationCount++; notificationName = name; }); @@ -296,13 +296,13 @@ module.exports = BaseTest.extend({ function secondNotification(realm, name) { secondNotificationCount++; }; - realm.addListener(secondNotification) + realm.addListener('change', secondNotification) realm.write(function() {}); TestCase.assertEqual(notificationCount, 2); TestCase.assertEqual(secondNotificationCount, 1); - realm.removeListener(secondNotification); + realm.removeListener('change', secondNotification); realm.write(function() {}); TestCase.assertEqual(notificationCount, 3); TestCase.assertEqual(secondNotificationCount, 1);