From f4248f5d30233ecd850b8f04f24b077f17dc1fa6 Mon Sep 17 00:00:00 2001 From: Kenneth Geisshirt Date: Wed, 27 Sep 2017 15:59:21 +0200 Subject: [PATCH] wip --- src/js_sync.hpp | 17 ++++++++++++---- tests/js/download-api-helper.js | 13 ++++++------ tests/js/session-tests.js | 36 ++++++++++++++++----------------- tests/package.json | 3 ++- 4 files changed, 40 insertions(+), 29 deletions(-) diff --git a/src/js_sync.hpp b/src/js_sync.hpp index 5d7c1d72..345a9aab 100644 --- a/src/js_sync.hpp +++ b/src/js_sync.hpp @@ -42,6 +42,8 @@ namespace js { using SharedUser = std::shared_ptr; using WeakSession = std::weak_ptr; +static bool config_fs_done; + template class UserClass : public ClassDefinition { using GlobalContextType = typename T::GlobalContext; @@ -516,6 +518,7 @@ public: static FunctionType create_constructor(ContextType); static void set_sync_log_level(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &); + static void initialize(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &); // private static std::function session_bind_callback(ContextType ctx, ObjectType sync_constructor); @@ -526,6 +529,7 @@ public: MethodMap const static_methods = { {"setLogLevel", wrap}, + {"initialize", wrap}, }; }; @@ -537,10 +541,7 @@ inline typename T::Function SyncClass::create_constructor(ContextType ctx) { Object::set_property(ctx, sync_constructor, "User", ObjectWrap>::create_constructor(ctx), attributes); Object::set_property(ctx, sync_constructor, "Session", ObjectWrap>::create_constructor(ctx), attributes); - // setup synced realmFile paths - ensure_directory_exists_for_file(default_realm_file_directory()); - SyncManager::shared().configure_file_system(default_realm_file_directory(), SyncManager::MetadataMode::NoEncryption); - + config_fs_done = false; return sync_constructor; } @@ -647,5 +648,13 @@ void SyncClass::populate_sync_config(ContextType ctx, ObjectType realm_constr } } } + +template +void SyncClass::initialize(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { + // setup synced realmFile paths + ensure_directory_exists_for_file(default_realm_file_directory()); + SyncManager::shared().configure_file_system(default_realm_file_directory(), SyncManager::MetadataMode::NoEncryption); + return_value.set(Nan::Undefined()); +} } // js } // realm diff --git a/tests/js/download-api-helper.js b/tests/js/download-api-helper.js index 5b9e6c4b..ae892753 100644 --- a/tests/js/download-api-helper.js +++ b/tests/js/download-api-helper.js @@ -11,9 +11,9 @@ var Realm = require(realmModule); function createObjects(user) { const config = { - sync: { user, - url: `realm://localhost:9080/~/${realmName}`, - error: err => console.log(err) + sync: { user, + url: `realm://localhost:9080/~/${realmName}`, + error: err => console.log(err) }, schema: [{ name: 'Dog', properties: { name: 'string' } }] }; @@ -30,6 +30,7 @@ function createObjects(user) { setTimeout(() => process.exit(0), 3000); } +Realm.Sync.initialize(); Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, registeredUser) => { if (error) { const registrationError = JSON.stringify(error); @@ -43,8 +44,8 @@ Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, createObjects(loggedUser); } }); - } - else { + } + else { createObjects(registeredUser); } -}); \ No newline at end of file +}); diff --git a/tests/js/session-tests.js b/tests/js/session-tests.js index 7087d027..91f669fe 100644 --- a/tests/js/session-tests.js +++ b/tests/js/session-tests.js @@ -29,7 +29,7 @@ const isNodeProccess = (typeof process === 'object' && process + '' === '[object function node_require(module) { return require(module); -} +} let tmp; let fs; @@ -37,7 +37,7 @@ let execFile; if (isNodeProccess) { tmp = node_require('tmp'); - fs = node_require('fs'); + fs = node_require('fs-extra'); execFile = node_require('child_process').execFile; tmp.setGracefulCleanup(); } @@ -275,7 +275,7 @@ module.exports = { }); }); }, - + testProgressNotificationsForRealmOpenAsync() { if (!isNodeProccess) { return Promise.resolve(); @@ -292,7 +292,7 @@ module.exports = { let progressNotificationCalled = false; let config = { sync: { user, url: `realm://localhost:9080/~/${realmName}`, - _onDownloadProgress: (transferred, total) => { + _onDownloadProgress: (transferred, total) => { progressNotificationCalled = true }, }, @@ -486,7 +486,7 @@ module.exports = { const progressCallback = (transferred, total) => { resolve(); }; - + realm.syncSession.addProgressNotification('download', 'reportIndefinitely', progressCallback); setTimeout(function() { @@ -495,7 +495,7 @@ module.exports = { }); }); }); - }, + }, testProgressNotificationsUnregisterForRealmConstructor() { if (!isNodeProccess) { @@ -519,7 +519,7 @@ module.exports = { let realm = new Realm(config); let unregisterFunc; - + let writeDataFunc = () => { realm.write(() => { for (let i = 1; i <= 3; i++) { @@ -534,14 +534,14 @@ module.exports = { if (failOnCall) { reject(new Error("Progress callback should not be called after removeProgressNotification")); } - + syncFinished = transferred === total; - + //unregister and write some new data. - if (syncFinished) { + if (syncFinished) { failOnCall = true; unregisterFunc(); - + //use second callback to wait for sync finished realm.syncSession.addProgressNotification('upload', 'reportIndefinitely', (x, y) => { if (x === y) { @@ -551,9 +551,9 @@ module.exports = { writeDataFunc(); } }; - + realm.syncSession.addProgressNotification('upload', 'reportIndefinitely', progressCallback); - + unregisterFunc = () => { realm.syncSession.removeProgressNotification(progressCallback); }; @@ -601,7 +601,7 @@ module.exports = { }); }); }); - }, + }, testProgressNotificationsForRealmOpenAsync2() { if (!isNodeProccess) { @@ -624,14 +624,14 @@ module.exports = { }; let progressCalled = false; - - Realm.openAsync(config, + + Realm.openAsync(config, (error, realm) => { if (error) { reject(error); return; } - + TestCase.assertTrue(progressCalled); resolve(); }, @@ -645,5 +645,5 @@ module.exports = { }); }); }); - }, + }, } diff --git a/tests/package.json b/tests/package.json index d537accb..081e5922 100644 --- a/tests/package.json +++ b/tests/package.json @@ -11,7 +11,8 @@ "terminate": "^1.0.8", "tmp": "^0.0.30", "url-parse": "^1.1.7", - "typescript": "^2.5.2" + "typescript": "^2.5.2", + "fs-extra": "^4.0.2" }, "scripts": { "check-typescript" : "tsc --noEmit --alwaysStrict ./../lib/index.d.ts",