Kneth/fix partial sync (#1383)
* fix typo in subscribeToObjects * Fix partial sync test * Swap error and result order in callback * Use correct url
This commit is contained in:
parent
19d05c2021
commit
aefd81ccf7
|
@ -7,7 +7,7 @@ X.Y.Z-rc? Release notes (DATE)
|
|||
* None
|
||||
|
||||
### Bug fixes
|
||||
* None.
|
||||
* Fixed bug in `Realm.subscribeToObjects()`.
|
||||
|
||||
### Internal
|
||||
* None
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
'use strict';
|
||||
|
||||
|
||||
let getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors || function(obj) {
|
||||
return Object.getOwnPropertyNames(obj).reduce(function (descriptors, name) {
|
||||
descriptors[name] = Object.getOwnPropertyDescriptor(obj, name);
|
||||
|
@ -139,23 +139,19 @@ module.exports = function(realmConstructor) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
// instance methods
|
||||
Object.defineProperties(realmConstructor.prototype, {
|
||||
subscribeToObjects: function(objectType, query) {
|
||||
const realm = this;
|
||||
let promise = new Promise((resolve, reject) => {
|
||||
callback = function(results, err) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(results);
|
||||
}
|
||||
};
|
||||
realm._subscriibeToObjects(objectType, query, callback);
|
||||
realmConstructor.prototype.subscribeToObjects = function(objectType, query) {
|
||||
const realm = this;
|
||||
let promise = new Promise((resolve, reject) => {
|
||||
realm._subscribeToObjects(objectType, query, function(err, results) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(results);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
return promise;
|
||||
};
|
||||
}
|
||||
|
||||
// TODO: Remove this now useless object.
|
||||
|
|
|
@ -1045,33 +1045,43 @@ void RealmClass<T>::subscribe_to_objects(ContextType ctx, ObjectType this_object
|
|||
args.validate_count(3);
|
||||
|
||||
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
|
||||
auto class_name = Value::validated_to_string(ctx, args[0]);
|
||||
auto query = Value::validated_to_string(ctx, args[1]);
|
||||
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]);
|
||||
|
||||
auto &schema = realm->schema();
|
||||
auto object_schema = schema.find(object_type);
|
||||
|
||||
if (object_schema == schema.end()) {
|
||||
throw std::runtime_error("Object type '" + object_type + "' not found in schema.");
|
||||
}
|
||||
|
||||
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);
|
||||
auto cb = [=](realm::Results results, std::exception_ptr err) {
|
||||
HANDLESCOPE
|
||||
|
||||
if (err) {
|
||||
try {
|
||||
std::rethrow_exception(err);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
typename T::Value arguments[2];
|
||||
arguments[0] = Value::from_null(protected_ctx);
|
||||
arguments[1] = Value::from_string(protected_ctx, e.what());
|
||||
Function<T>::callback(ctx, callback, protected_this, 2, arguments);
|
||||
ValueType callback_arguments[2];
|
||||
callback_arguments[0] = Value::from_string(protected_ctx, e.what());
|
||||
callback_arguments[1] = Value::from_null(protected_ctx);
|
||||
Function<T>::callback(ctx, protected_callback, protected_this, 2, callback_arguments);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
typename T::Value arguments[2];
|
||||
arguments[0] = ResultsClass<T>::create_instance(protected_ctx, results);
|
||||
arguments[1] = Value::from_null(protected_ctx);
|
||||
Function<T>::callback(protected_ctx, callback, protected_this, 2, arguments);
|
||||
ValueType callback_arguments[2];
|
||||
callback_arguments[0] = Value::from_null(protected_ctx);
|
||||
callback_arguments[1] = ResultsClass<T>::create_instance(protected_ctx, results);
|
||||
Function<T>::callback(protected_ctx, protected_callback, protected_this, 2, callback_arguments);
|
||||
};
|
||||
|
||||
partial_sync::register_query(realm, class_name, query, std::move(cb));
|
||||
partial_sync::register_query(realm, object_type, query, std::move(cb));
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
|
@ -670,7 +670,7 @@ void SyncClass<T>::populate_sync_config(ContextType ctx, ObjectType realm_constr
|
|||
|
||||
|
||||
config.schema_mode = SchemaMode::Additive;
|
||||
config.path = syncManagerShared().path_for_realm(*shared_user, raw_realm_url);
|
||||
config.path = syncManagerShared().path_for_realm(*shared_user, config.sync_config->realm_url());
|
||||
|
||||
if (!config.encryption_key.empty()) {
|
||||
config.sync_config->realm_encryption_key = std::array<char, 64>();
|
||||
|
|
|
@ -114,7 +114,6 @@ function runOutOfProcess(nodeJsFilePath) {
|
|||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
testLocalRealmHasNoSession() {
|
||||
let realm = new Realm();
|
||||
TestCase.assertNull(realm.syncSession);
|
||||
|
@ -792,66 +791,26 @@ module.exports = {
|
|||
|
||||
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
||||
.then(() => {
|
||||
Realm.Sync.User.login('http://localhost:9080', username, 'password').then(user1 => {
|
||||
TestCase.assertDefined(user1, 'user1');
|
||||
let config1 = {
|
||||
return Realm.Sync.User.login('http://localhost:9080', username, 'password').then(user => {
|
||||
let config = {
|
||||
sync: {
|
||||
user: user1,
|
||||
user: user,
|
||||
url: `realm://localhost:9080/~/${realmName}`,
|
||||
partial: true,
|
||||
error: (session, error) => console.log(error)
|
||||
},
|
||||
schema: [{ name: 'Integer', properties: { value: 'int' } }],
|
||||
schema: [{ name: 'Dog', properties: { name: 'string' } }]
|
||||
};
|
||||
return new Promise((resolve, reject) => {
|
||||
return Realm.open(config1)
|
||||
.then(realm1 => {
|
||||
for(let i = 0; i < 10; i++) {
|
||||
realm1.write(() => {
|
||||
realm1.create('Integer', {value: i});
|
||||
});
|
||||
}
|
||||
|
||||
const progressCallback = (transferred, total) => {
|
||||
if (transferred === total) {
|
||||
resolve();
|
||||
}
|
||||
}
|
||||
realm.syncSession.addProgressNotification('upload', 'reportIndefinitely', progressCallback);
|
||||
})
|
||||
.then(() => {
|
||||
realm.close();
|
||||
realm.deleteFile(config1);
|
||||
user1.logout();
|
||||
});
|
||||
Realm.deleteFile(config);
|
||||
const realm = new Realm(config);
|
||||
TestCase.assertEqual(realm.objects('Dog').length, 0);
|
||||
return realm.subscribeToObjects("Dog", "name == 'Lassy 1'").then(results => {
|
||||
TestCase.assertEqual(results.length, 1);
|
||||
TestCase.assertTrue(results[0].name === 'Lassy 1', "The object is not synced correctly");
|
||||
});
|
||||
})
|
||||
})
|
||||
.then(() => {
|
||||
Realm.Sync.User.login('http://localhost:9080', username, 'password').then(user2 => {
|
||||
TestCase.assertDefined(user2, 'user2');
|
||||
return new Promise((resolve, reject) => {
|
||||
let config2 = {
|
||||
sync: {
|
||||
user: user2,
|
||||
url: `realm://localhost:9080/~/${realmName}`,
|
||||
partial: true,
|
||||
},
|
||||
schema: [{ name: 'Integer', properties: { value: 'int' } }],
|
||||
};
|
||||
|
||||
const realm2 = new Realm(config2);
|
||||
realm2.subscribeToObjects('Integer', 'value > 5').then((results, error) => {
|
||||
return results;
|
||||
}).then((results) => {
|
||||
TestCase.assertEqual(results.length, 4);
|
||||
for(obj in results) {
|
||||
TestCase.assertTrue(obj.value > 5, '<= 5');
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
reject();
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
testClientReset() {
|
||||
|
|
Loading…
Reference in New Issue