take notification name

This commit is contained in:
Ari Lazier 2015-10-26 19:18:24 -07:00
parent bb68e45250
commit 6f41c3cf68
3 changed files with 34 additions and 11 deletions

View File

@ -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] = [];
}

View File

@ -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<SharedRealm *>(thisObject);
static_cast<RJSRealmDelegate *>(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<SharedRealm *>(thisObject);
static_cast<RJSRealmDelegate *>(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<SharedRealm *>(thisObject);
static_cast<RJSRealmDelegate *>(realm->m_delegate.get())->remove_all_notifications();
return NULL;

View File

@ -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);