Merge pull request #1279 from realm/progress-api

Progress api
This commit is contained in:
blagoev 2017-09-12 11:33:59 +03:00 committed by GitHub
commit 7d25239fcc
16 changed files with 453 additions and 66 deletions

1
.vscode/launch.json vendored
View File

@ -54,6 +54,7 @@
"type": "node", "type": "node",
"request": "attach", "request": "attach",
"name": "Attach to Port", "name": "Attach to Port",
"protocol": "legacy",
"address": "localhost", "address": "localhost",
"port": 5858 "port": 5858
} }

View File

@ -5,6 +5,8 @@ X.Y.Z Release notes
### Enhancements ### Enhancements
* Improve performance of the RPC worker for chrome debugging. * Improve performance of the RPC worker for chrome debugging.
* Added Progress API `realm.syncSession.addProgressNotification` and `realm.syncSession.removeProgressNotification`
* Added additional parameter for `Realm.open` and `Realm.openAsync` for download progress notifications
* Added `Realm.deleteFile` for deleting a Realm (#363). * Added `Realm.deleteFile` for deleting a Realm (#363).
### Bug fixes ### Bug fixes

View File

@ -334,6 +334,28 @@ class Session {
* @type {string} * @type {string}
*/ */
get state() {} get state() {}
/**
* Register a progress notification callback on a session object
* @param {string} direction - The progress direction to register for.
* Can be either:
* - `download` - report download progress
* - `upload` - report upload progress
* @param {string} mode - The progress notification mode to use for the registration.
* Can be either:
* - `reportIndefinitely` - the registration will stay active until the callback is unregistered
* - `forCurrentlyOutstandingWork` - the registration will be active until only the currently transferable bytes are synced
* @param {callback(transferred, transferable)} callback - called with the following arguments:
* - `transferred` - the current number of bytes already transferred
* - `transferable` - the total number of transferable bytes (the number of bytes already transferred plus the number of bytes pending transfer)
*/
addProgressNotification(direction, mode, progressCallback) {}
/** Unregister a progress notification callback that was previously registered with addProgressNotification.
* Calling the function multiple times with the same callback is ignored.
* @param {callback(transferred, transferable)} callback - a previously registered progress callback
*/
removeProgressNotification(progressCallback) {}
} }

View File

@ -187,7 +187,8 @@ Object.defineProperties(Realm, {
}, },
}, },
_waitForDownload: { _waitForDownload: {
value: function(_config, callback) { value: function(_config, sessionCallback, callback) {
sessionCallback();
callback(); callback();
} }
}, },

View File

@ -22,7 +22,9 @@ import { keys, objectTypes } from './constants';
import { getterForProperty, createMethods } from './util'; import { getterForProperty, createMethods } from './util';
import { deserialize } from './rpc'; import { deserialize } from './rpc';
export default class Session { } export default class Session {
}
Object.defineProperties(Session.prototype, { Object.defineProperties(Session.prototype, {
url: { get: getterForProperty('url') }, url: { get: getterForProperty('url') },
@ -31,7 +33,9 @@ Object.defineProperties(Session.prototype, {
createMethods(Session.prototype, objectTypes.SESSION, [ createMethods(Session.prototype, objectTypes.SESSION, [
'_refreshAccessToken', '_refreshAccessToken',
'_simulateError' '_simulateError',
'addProgressNotification',
'removeProgressNotification'
]); ]);
export function createSession(realmId, info) { export function createSession(realmId, info) {

View File

@ -40,7 +40,7 @@ export function createMethod(type, name, mutates) {
let id = this[keys.id]; let id = this[keys.id];
if (!realmId || !id) { if (!realmId || !id) {
throw new TypeError(name + ' method was not called a Realm object!'); throw new TypeError(name + ' method was not called on a Realm object!');
} }
if (this[keys.type] !== type) { if (this[keys.type] !== type) {
throw new TypeError(name + ' method was called on an object of the wrong type!'); throw new TypeError(name + ' method was called on an object of the wrong type!');

View File

@ -43,28 +43,55 @@ module.exports = function(realmConstructor) {
//Add async open API //Add async open API
Object.defineProperties(realmConstructor, getOwnPropertyDescriptors({ Object.defineProperties(realmConstructor, getOwnPropertyDescriptors({
open(config) { open(config) {
return new Promise((resolve, reject) => { let syncSession;
realmConstructor._waitForDownload(config, (error) => { let promise = new Promise((resolve, reject) => {
if (error) { realmConstructor._waitForDownload(config,
reject(error); (session) => {
} syncSession = session;
else { },
try { (error) => {
let syncedRealm = new this(config); if (error) {
//FIXME: RN hangs here. Remove when node's makeCallback alternative is implemented setTimeout(() => { reject(error); }, 1);
setTimeout(() => { resolve(syncedRealm); }, 1);
} catch (e) {
reject(e);
} }
} else {
}); try {
let syncedRealm = new this(config);
//FIXME: RN hangs here. Remove when node's makeCallback alternative is implemented
setTimeout(() => { resolve(syncedRealm); }, 1);
} catch (e) {
reject(e);
}
}
});
}); });
promise.progress = (callback) => {
if (syncSession) {
syncSession.addProgressNotification('download', 'forCurrentlyOutstandingWork', callback);
}
return promise;
};
return promise;
}, },
openAsync(config, callback) { openAsync(config, progressCallback, callback) {
realmConstructor._waitForDownload(config, (error) => {
if (!callback) {
callback = progressCallback;
progressCallback = null;
}
realmConstructor._waitForDownload(config,
(syncSession) => {
if (progressCallback) {
syncSession.addProgressNotification('download', 'forCurrentlyOutstandingWork', progressCallback);;
}
},
(error) => {
if (error) { if (error) {
callback(error); setTimeout(() => { callback(error); }, 1);
} }
else { else {
try { try {

19
lib/index.d.ts vendored
View File

@ -332,6 +332,10 @@ declare namespace Realm.Sync {
ssl_trust_certificate_path?: string; ssl_trust_certificate_path?: string;
} }
type ProgressNotificationCallback = (transferred: number, transferable: number) => void;
type ProgressDirection = 'download' | 'upload';
type ProgressMode = 'reportIndefinitely' | 'forCurrentlyOutstandingWork';
/** /**
* Session * Session
* @see { @link https://realm.io/docs/javascript/latest/api/Realm.Sync.Session.html } * @see { @link https://realm.io/docs/javascript/latest/api/Realm.Sync.Session.html }
@ -341,6 +345,9 @@ declare namespace Realm.Sync {
readonly state: 'invalid' | 'active' | 'inactive'; readonly state: 'invalid' | 'active' | 'inactive';
readonly url: string; readonly url: string;
readonly user: User; readonly user: User;
addProgressNotification(direction: ProgressDirection, mode: ProgressMode, progressCallback: ProgressNotificationCallback): void;
removeProgressNotification(progressCallback: ProgressNotificationCallback): void;
} }
/** /**
@ -402,6 +409,11 @@ declare namespace Realm.Sync {
} }
} }
interface ProgressPromise extends Promise<Realm> {
progress(callback: Realm.Sync.ProgressNotificationCallback) : Promise<Realm>
}
declare class Realm { declare class Realm {
static defaultPath: string; static defaultPath: string;
@ -422,17 +434,20 @@ declare class Realm {
*/ */
static schemaVersion(path: string, encryptionKey?: ArrayBuffer | ArrayBufferView): number; static schemaVersion(path: string, encryptionKey?: ArrayBuffer | ArrayBufferView): number;
/** /**
* Open a realm asynchronously with a promise. If the realm is synced, it will be fully synchronized before it is available. * Open a realm asynchronously with a promise. If the realm is synced, it will be fully synchronized before it is available.
* @param {Configuration} config * @param {Configuration} config
*/ */
static open(config: Realm.Configuration): Promise<Realm> static open(config: Realm.Configuration): ProgressPromise;
/** /**
* Open a realm asynchronously with a callback. If the realm is synced, it will be fully synchronized before it is available. * Open a realm asynchronously with a callback. If the realm is synced, it will be fully synchronized before it is available.
* @param {Configuration} config * @param {Configuration} config
* @param {ProgressNotificationCallback} progressCallback? a progress notification callback for 'download' direction and 'forCurrentlyOutstandingWork' mode
* @param {Function} callback will be called when the realm is ready. * @param {Function} callback will be called when the realm is ready.
*/ */
static openAsync(config: Realm.Configuration, callback: (error: any, realm: Realm) => void): void static openAsync(config: Realm.Configuration, progressCallback?: Realm.Sync.ProgressNotificationCallback, callback: (error: any, realm: Realm) => void): void
/** /**
* Delete the Realm file for the given configuration. * Delete the Realm file for the given configuration.

View File

@ -86,7 +86,7 @@ function getSpecialPurposeRealm(user, realmName, schema) {
setTimeout(() => {}, 1); setTimeout(() => {}, 1);
if (error) { if (error) {
reject(error); setTimeout(() => reject(error), 1);
} }
else { else {
try { try {
@ -95,7 +95,7 @@ function getSpecialPurposeRealm(user, realmName, schema) {
//FIXME: RN hangs here. Remove when node's makeCallback alternative is implemented (#1255) //FIXME: RN hangs here. Remove when node's makeCallback alternative is implemented (#1255)
setTimeout(() => resolve(syncedRealm), 1); setTimeout(() => resolve(syncedRealm), 1);
} catch (e) { } catch (e) {
reject(e); setTimeout(() => reject(e), 1);
} }
} }
}); });

View File

@ -48,7 +48,11 @@ download_server() {
} }
start_server() { start_server() {
sh ./object-server-for-testing/start-object-server.command & #disabled ROS logging
sh ./object-server-for-testing/start-object-server.command &> /dev/null &
#enabled ROS logging
#sh ./object-server-for-testing/start-object-server.command &
SERVER_PID=$! SERVER_PID=$!
} }

View File

@ -619,9 +619,14 @@ void RealmClass<T>::get_sync_session(ContextType ctx, ObjectType object, ReturnV
template<typename T> template<typename T>
void RealmClass<T>::wait_for_download_completion(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { void RealmClass<T>::wait_for_download_completion(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
validate_argument_count(argc, 2); validate_argument_count(argc, 2, 3);
auto config_object = Value::validated_to_object(ctx, arguments[0]); auto config_object = Value::validated_to_object(ctx, arguments[0]);
auto callback_function = Value::validated_to_function(ctx, arguments[1]); auto callback_function = Value::validated_to_function(ctx, arguments[argc - 1]);
ValueType session_callback = Value::from_null(ctx);
if (argc == 3) {
session_callback = Value::validated_to_function(ctx, arguments[1]);
}
#if REALM_ENABLE_SYNC #if REALM_ENABLE_SYNC
ValueType sync_config_value = Object::get_property(ctx, config_object, "sync"); ValueType sync_config_value = Object::get_property(ctx, config_object, "sync");
@ -694,6 +699,14 @@ void RealmClass<T>::wait_for_download_completion(ContextType ctx, FunctionType,
std::shared_ptr<SyncUser> user = sync_config->user; std::shared_ptr<SyncUser> user = sync_config->user;
if (user && user->state() != SyncUser::State::Error) { if (user && user->state() != SyncUser::State::Error) {
if (auto session = user->session_for_on_disk_path(config.path)) { if (auto session = user->session_for_on_disk_path(config.path)) {
if (!Value::is_null(ctx, session_callback)) {
FunctionType session_callback_func = Value::to_function(ctx, session_callback);
auto syncSession = create_object<T, SessionClass<T>>(ctx, new WeakSession(session));
ValueType callback_arguments[1];
callback_arguments[0] = syncSession;
Function<T>::callback(protected_ctx, session_callback_func, protected_this, 1, callback_arguments);
}
if (progressFuncDefined) { if (progressFuncDefined) {
session->register_progress_notifier(std::move(progressFunc), SyncSession::NotifierType::download, false); session->register_progress_notifier(std::move(progressFunc), SyncSession::NotifierType::download, false);
} }

View File

@ -160,6 +160,7 @@ class SessionClass : public ClassDefinition<T, WeakSession> {
public: public:
std::string const name = "Session"; std::string const name = "Session";
using ProgressHandler = void(uint64_t transferred_bytes, uint64_t transferrable_bytes);
static FunctionType create_constructor(ContextType); static FunctionType create_constructor(ContextType);
@ -170,6 +171,8 @@ public:
static void simulate_error(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &); static void simulate_error(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void refresh_access_token(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &); static void refresh_access_token(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void add_progress_notification(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &);
static void remove_progress_notification(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &);
PropertyMap<T> const properties = { PropertyMap<T> const properties = {
{"config", {wrap<get_config>, nullptr}}, {"config", {wrap<get_config>, nullptr}},
@ -180,7 +183,9 @@ public:
MethodMap<T> const methods = { MethodMap<T> const methods = {
{"_simulateError", wrap<simulate_error>}, {"_simulateError", wrap<simulate_error>},
{"_refreshAccessToken", wrap<refresh_access_token>} {"_refreshAccessToken", wrap<refresh_access_token>},
{"addProgressNotification", wrap<add_progress_notification>},
{"removeProgressNotification", wrap<remove_progress_notification>},
}; };
}; };
@ -308,7 +313,83 @@ void SessionClass<T>::refresh_access_token(ContextType ctx, FunctionType, Object
} }
template<typename T> template<typename T>
class SyncClass : public ClassDefinition<T, void *> { void SessionClass<T>::add_progress_notification(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
validate_argument_count(argc, 3);
if (auto session = get_internal<T, SessionClass<T>>(this_object)->lock()) {
std::string direction = Value::validated_to_string(ctx, arguments[0], "direction");
std::string mode = Value::validated_to_string(ctx, arguments[1], "mode");
SyncSession::NotifierType notifierType;
if (direction == "download") {
notifierType = SyncSession::NotifierType::download;
}
else if (direction == "upload") {
notifierType = SyncSession::NotifierType::upload;
}
else {
throw std::invalid_argument("Invalid argument 'direction'. Only 'download' and 'upload' progress notification directions are supported");
}
bool is_streaming = false;
if (mode == "reportIndefinitely") {
is_streaming = true;
}
else if (mode == "forCurrentlyOutstandingWork") {
is_streaming = false;
}
else {
throw std::invalid_argument("Invalid argument 'mode'. Only 'reportIndefinitely' and 'forCurrentlyOutstandingWork' progress notification modes are supported");
}
auto callback_function = Value::validated_to_function(ctx, arguments[2], "callback");
Protected<FunctionType> protected_callback(ctx, callback_function);
Protected<ObjectType> protected_this(ctx, this_object);
Protected<typename T::GlobalContext> protected_ctx(Context<T>::get_global_context(ctx));
std::function<ProgressHandler> progressFunc;
EventLoopDispatcher<ProgressHandler> progress_handler([=](uint64_t transferred_bytes, uint64_t transferrable_bytes) {
HANDLESCOPE
ValueType callback_arguments[2];
callback_arguments[0] = Value::from_number(protected_ctx, transferred_bytes);
callback_arguments[1] = Value::from_number(protected_ctx, transferrable_bytes);
Function<T>::callback(protected_ctx, protected_callback, typename T::Object(), 2, callback_arguments);
});
progressFunc = std::move(progress_handler);
auto registrationToken = session->register_progress_notifier(std::move(progressFunc), notifierType, false);
auto syncSession = create_object<T, SessionClass<T>>(ctx, new WeakSession(session));
PropertyAttributes attributes = ReadOnly | DontEnum | DontDelete;
Object::set_property(ctx, callback_function, "_syncSession", syncSession, attributes);
Object::set_property(ctx, callback_function, "_registrationToken", Value::from_number(protected_ctx, registrationToken), attributes);
}
}
template<typename T>
void SessionClass<T>::remove_progress_notification(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
validate_argument_count(argc, 1);
auto callback_function = Value::validated_to_function(ctx, arguments[0], "callback");
auto syncSessionProp = Object::get_property(ctx, callback_function, "_syncSession");
if (Value::is_undefined(ctx, syncSessionProp) || Value::is_null(ctx, syncSessionProp)) {
return;
}
auto syncSession = Value::validated_to_object(ctx, syncSessionProp);
auto registrationToken = Object::get_property(ctx, callback_function, "_registrationToken");
if (auto session = get_internal<T, SessionClass<T>>(syncSession)->lock()) {
auto reg = Value::validated_to_number(ctx, registrationToken);
session->unregister_progress_notifier(reg);
}
}
template<typename T>
class SyncClass : public ClassDefinition<T, void*> {
using GlobalContextType = typename T::GlobalContext; using GlobalContextType = typename T::GlobalContext;
using ContextType = typename T::Context; using ContextType = typename T::Context;
using FunctionType = typename T::Function; using FunctionType = typename T::Function;
@ -446,6 +527,5 @@ void SyncClass<T>::populate_sync_config(ContextType ctx, ObjectType realm_constr
} }
} }
} }
} // js } // js
} // realm } // realm

View File

@ -2,33 +2,49 @@
This script creates 3 new objects into a new realm. These are objects are validated to exists by the download api tests. This script creates 3 new objects into a new realm. These are objects are validated to exists by the download api tests.
*/ */
'use strict'; 'use strict';
console.log("download-api-helper started");
const username = process.argv[2]; const username = process.argv[2];
const realmName = process.argv[3]; const realmName = process.argv[3];
const realmModule = process.argv[4]; const realmModule = process.argv[4];
var Realm = require(realmModule); var Realm = require(realmModule);
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
function createObjects(user) {
const config = {
sync: { user,
url: `realm://localhost:9080/~/${realmName}`,
error: err => console.log(err)
},
schema: [{ name: 'Dog', properties: { name: 'string' } }]
};
var realm = new Realm(config);
realm.write(() => {
for (let i = 1; i <= 3; i++) {
realm.create('Dog', { name: `Lassy ${i}` });
}
});
console.log("Dogs count " + realm.objects('Dog').length);
setTimeout(() => process.exit(0), 3000);
}
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, registeredUser) => {
if (error) { if (error) {
console.log(error); const registrationError = JSON.stringify(error);
process.exit(-2); Realm.Sync.User.login('http://localhost:9080', username, 'password', (err, loggedUser) => {
} else { if (err) {
const config = { const loginError = JSON.stringify(err);
sync: { user, url: `realm://localhost:9080/~/${realmName}`, error: err => console.log(err) }, console.error("download-api-helper failed:\n User.register() error:\n" + registrationError + "\n User.login() error:\n" + loginError);
schema: [{ name: 'Dog', properties: { name: 'string' } }] process.exit(-2);
}; }
else {
var realm = new Realm(config); createObjects(loggedUser);
realm.write(() => {
for (let i = 1; i <= 3; i++) {
realm.create('Dog', { name: `Lassy ${i}` });
} }
}); });
}
console.log("Dogs count " + realm.objects('Dog').length); else {
setTimeout(() => process.exit(0), 3000); createObjects(registeredUser);
} }
}); });

View File

@ -59,6 +59,7 @@ module.exports = {
return createUsersWithTestRealms(1) return createUsersWithTestRealms(1)
.then(([user]) => { .then(([user]) => {
return user.applyPermissions({ userId: '*' }, `/${user.identity}/test`, 'read') return user.applyPermissions({ userId: '*' }, `/${user.identity}/test`, 'read')
.then(wait(100))
.then(() => user.getGrantedPermissions('any')) .then(() => user.getGrantedPermissions('any'))
.then(permissions => { .then(permissions => {
TestCase.assertEqual(permissions[1].path, `/${user.identity}/test`); TestCase.assertEqual(permissions[1].path, `/${user.identity}/test`);

View File

@ -26,7 +26,7 @@ const Realm = require('realm');
const TestCase = require('./asserts'); const TestCase = require('./asserts');
const isNodeProccess = (typeof process === 'object' && process + '' === '[object process]'); const isNodeProccess = (typeof process === 'object' && process + '' === '[object process]');
console.log("isnode " + isNodeProccess + " typeof " + (typeof(process) === 'object'));
function node_require(module) { function node_require(module) {
return require(module); return require(module);
} }
@ -84,13 +84,23 @@ function runOutOfProcess(nodeJsFilePath) {
fs.appendFileSync(tmpFile.fd, content, { encoding: 'utf8' }); fs.appendFileSync(tmpFile.fd, content, { encoding: 'utf8' });
nodeArgs[0] = tmpFile.name; nodeArgs[0] = tmpFile.name;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const child = execFile('node', nodeArgs, { cwd: tmpDir.name }, (error, stdout, stderr) => { try {
if (error) { console.log('runOutOfProcess command\n node ' + nodeArgs.join(" "));
reject(new Error(`Error executing ${nodeJsFilePath} Error: ${error}`)); const child = execFile('node', nodeArgs, { cwd: tmpDir.name }, (error, stdout, stderr) => {
if (error) {
console.error("runOutOfProcess failed\n" + error);
reject(new Error(`Running ${nodeJsFilePath} failed. error: ${error}`));
return;
}
console.log('runOutOfProcess success\n' + stdout);
resolve();
});
} }
resolve(); catch (e) {
}); reject(e);
}) };
});
} }
module.exports = { module.exports = {
@ -150,7 +160,7 @@ module.exports = {
const realmName = uuid(); const realmName = uuid();
const expectedObjectsCount = 3; const expectedObjectsCount = 3;
runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH) return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
.then(() => { .then(() => {
return promisifiedLogin('http://localhost:9080', username, 'password').then(user => { return promisifiedLogin('http://localhost:9080', username, 'password').then(user => {
const accessTokenRefreshed = this; const accessTokenRefreshed = this;
@ -186,7 +196,7 @@ module.exports = {
const realmName = uuid(); const realmName = uuid();
const expectedObjectsCount = 3; const expectedObjectsCount = 3;
runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH) return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
.then(() => { .then(() => {
return promisifiedLogin('http://localhost:9080', username, 'password').then(user => { return promisifiedLogin('http://localhost:9080', username, 'password').then(user => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -239,7 +249,7 @@ module.exports = {
const realmName = uuid(); const realmName = uuid();
const expectedObjectsCount = 3; const expectedObjectsCount = 3;
runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH) return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
.then(() => { .then(() => {
return promisifiedLogin('http://localhost:9080', username, 'password').then(user => { return promisifiedLogin('http://localhost:9080', username, 'password').then(user => {
const accessTokenRefreshed = this; const accessTokenRefreshed = this;
@ -275,7 +285,7 @@ module.exports = {
const realmName = uuid(); const realmName = uuid();
const expectedObjectsCount = 3; const expectedObjectsCount = 3;
runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH) return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
.then(() => { .then(() => {
return promisifiedLogin('http://localhost:9080', username, 'password').then(user => { return promisifiedLogin('http://localhost:9080', username, 'password').then(user => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -322,7 +332,7 @@ module.exports = {
const realmName = uuid(); const realmName = uuid();
const expectedObjectsCount = 3; const expectedObjectsCount = 3;
runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH) return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
.then(() => { .then(() => {
return promisifiedLogin('http://localhost:9080', username, 'password').then(user => { return promisifiedLogin('http://localhost:9080', username, 'password').then(user => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -449,5 +459,191 @@ module.exports = {
session._simulateError(123, 'simulated error'); session._simulateError(123, 'simulated error');
}); });
}); });
} },
testProgressNotificationsForRealmConstructor() {
if (!isNodeProccess) {
return Promise.resolve();
}
const username = uuid();
const realmName = uuid();
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
.then(() => {
return promisifiedLogin('http://localhost:9080', username, 'password').then(user => {
return new Promise((resolve, reject) => {
let config = {
sync: {
user,
url: `realm://localhost:9080/~/${realmName}`
},
schema: [{ name: 'Dog', properties: { name: 'string' } }],
};
let realm = new Realm(config);
const progressCallback = (transferred, total) => {
resolve();
};
realm.syncSession.addProgressNotification('download', 'reportIndefinitely', progressCallback);
setTimeout(function() {
reject("Progress Notifications API failed to call progress callback for Realm constructor");
}, 5000);
});
});
});
},
testProgressNotificationsUnregisterForRealmConstructor() {
if (!isNodeProccess) {
return Promise.resolve();
}
const username = uuid();
const realmName = uuid();
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
.then(() => {
return promisifiedLogin('http://localhost:9080', username, 'password').then(user => {
return new Promise((resolve, reject) => {
let config = {
sync: {
user,
url: `realm://localhost:9080/~/${realmName}`
},
schema: [{ name: 'Dog', properties: { name: 'string' } }],
};
let realm = new Realm(config);
let unregisterFunc;
let writeDataFunc = () => {
realm.write(() => {
for (let i = 1; i <= 3; i++) {
realm.create('Dog', { name: `Lassy ${i}` });
}
});
}
let syncFinished = false;
let failOnCall = false;
const progressCallback = (transferred, total) => {
if (failOnCall) {
reject(new Error("Progress callback should not be called after removeProgressNotification"));
}
syncFinished = transferred === total;
//unregister and write some new data.
if (syncFinished) {
failOnCall = true;
unregisterFunc();
//use second callback to wait for sync finished
realm.syncSession.addProgressNotification('upload', 'reportIndefinitely', (x, y) => {
if (x === y) {
resolve();
}
});
writeDataFunc();
}
};
realm.syncSession.addProgressNotification('upload', 'reportIndefinitely', progressCallback);
unregisterFunc = () => {
realm.syncSession.removeProgressNotification(progressCallback);
};
writeDataFunc();
});
});
});
},
testProgressNotificationsForRealmOpen() {
if (!isNodeProccess) {
return Promise.resolve();
}
const username = uuid();
const realmName = uuid();
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
.then(() => {
return promisifiedLogin('http://localhost:9080', username, 'password').then(user => {
return new Promise((resolve, reject) => {
let config = {
sync: {
user,
url: `realm://localhost:9080/~/${realmName}`
},
schema: [{ name: 'Dog', properties: { name: 'string' } }],
};
let progressCalled = false;
Realm.open(config)
.progress((transferred, total) => {
progressCalled = true;
})
.then(() => {
TestCase.assertTrue(progressCalled);
resolve();
})
.catch((e) => reject(e));
setTimeout(function() {
reject("Progress Notifications API failed to call progress callback for Realm constructor");
}, 5000);
});
});
});
},
testProgressNotificationsForRealmOpenAsync() {
if (!isNodeProccess) {
return Promise.resolve();
}
const username = uuid();
const realmName = uuid();
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
.then(() => {
return promisifiedLogin('http://localhost:9080', username, 'password').then(user => {
return new Promise((resolve, reject) => {
let config = {
sync: {
user,
url: `realm://localhost:9080/~/${realmName}`
},
schema: [{ name: 'Dog', properties: { name: 'string' } }],
};
let progressCalled = false;
Realm.openAsync(config,
(transferred, total) => {
progressCalled = true;
},
(error, realm) => {
if (error) {
reject(error);
return;
}
TestCase.assertTrue(progressCalled);
resolve();
});
setTimeout(function() {
reject("Progress Notifications API failed to call progress callback for Realm constructor");
}, 5000);
});
});
});
},
} }

View File

@ -20,6 +20,7 @@
/* eslint-disable no-console */ /* eslint-disable no-console */
'use strict'; 'use strict';
const isNodeProccess = (typeof process === 'object' && process + '' === '[object process]');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
@ -28,7 +29,11 @@ const Realm = require('realm');
const RealmTests = require('../js'); const RealmTests = require('../js');
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000; jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
const isDebuggerAttached = typeof v8debug === 'object'; let isDebuggerAttached = typeof v8debug === 'object';
if (!isDebuggerAttached && isNodeProccess) {
isDebuggerAttached = /--debug|--inspect/.test(process.execArgv.join(' '));
}
if (isDebuggerAttached) { if (isDebuggerAttached) {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 3000000; jasmine.DEFAULT_TIMEOUT_INTERVAL = 3000000;
} }