Support named partial sync subscriptions.

This commit is contained in:
Kenneth Geisshirt 2017-12-20 15:19:55 +01:00
parent fd5d4fb5c0
commit a7597eb577
1 changed files with 37 additions and 24 deletions

View File

@ -1067,12 +1067,20 @@ void RealmClass<T>::object_for_object_id(ContextType ctx, ObjectType this_object
#if REALM_ENABLE_SYNC
template<typename T>
void RealmClass<T>::subscribe_to_objects(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
args.validate_count(3);
args.validate_count(4);
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
std::string object_type = Value::validated_to_string(ctx, args[0]);
std::string query = Value::validated_to_string(ctx, args[1]);
auto callback = Value::validated_to_function(ctx, args[2]);
std::string key;
FunctionType callback;
if (Value::is_function(ctx, args[1])) {
callback = Value::validated_to_function(ctx, args[2]);
}
else {
key = Value::validated_to_string(ctx, args[2]);
}
auto &schema = realm->schema();
auto object_schema = schema.find(object_type);
@ -1081,6 +1089,10 @@ void RealmClass<T>::subscribe_to_objects(ContextType ctx, ObjectType this_object
throw std::runtime_error("Object type '" + object_type + "' not found in schema.");
}
if (key == nullptr) {
partial_sync::register_query(*realm, key, object_type, query);
}
else {
Protected<ObjectType> protected_this(ctx, this_object);
Protected<typename T::GlobalContext> protected_ctx(Context<T>::get_global_context(ctx));
Protected<FunctionType> protected_callback(ctx, callback);
@ -1108,6 +1120,7 @@ void RealmClass<T>::subscribe_to_objects(ContextType ctx, ObjectType this_object
partial_sync::register_query(realm, object_type, query, std::move(cb));
}
}
#endif
} // js