From 739d837e373e5d698e19604ec188cc9f8706f6c8 Mon Sep 17 00:00:00 2001 From: blagoev Date: Thu, 9 Mar 2023 11:07:50 +0200 Subject: [PATCH 1/2] support shouldCompact callback with flexible sync config --- lib/src/configuration.dart | 6 ++++++ lib/src/native/realm_core.dart | 20 ++++++++++++++++---- test/app_test.dart | 2 +- test/configuration_test.dart | 14 ++++++++++++++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/lib/src/configuration.dart b/lib/src/configuration.dart index 83ff15f61..e763746f4 100644 --- a/lib/src/configuration.dart +++ b/lib/src/configuration.dart @@ -203,6 +203,7 @@ abstract class Configuration implements Finalizable { SyncErrorHandler syncErrorHandler = defaultSyncErrorHandler, ClientResetHandler clientResetHandler = const RecoverOrDiscardUnsyncedChangesHandler(onManualResetFallback: _defaultClientResetHandler), int? maxNumberOfActiveVersions, + ShouldCompactCallback? shouldCompactCallback, }) => FlexibleSyncConfiguration._( user, @@ -213,6 +214,7 @@ abstract class Configuration implements Finalizable { syncErrorHandler: syncErrorHandler, clientResetHandler: clientResetHandler, maxNumberOfActiveVersions: maxNumberOfActiveVersions, + shouldCompactCallback: shouldCompactCallback, ); /// Constructs a [DisconnectedSyncConfiguration] @@ -350,6 +352,9 @@ class FlexibleSyncConfiguration extends Configuration { /// The default [ClientResetHandler] logs a message using the current [Realm.logger]. final ClientResetHandler clientResetHandler; + /// Called when opening a `Realm` for the first time, after process start. + final ShouldCompactCallback? shouldCompactCallback; + FlexibleSyncConfiguration._( this.user, super.schemaObjects, { @@ -359,6 +364,7 @@ class FlexibleSyncConfiguration extends Configuration { this.syncErrorHandler = defaultSyncErrorHandler, this.clientResetHandler = const RecoverOrDiscardUnsyncedChangesHandler(onManualResetFallback: _defaultClientResetHandler), super.maxNumberOfActiveVersions, + this.shouldCompactCallback, }) : super._(); @override diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index 528cce4d8..c2de61e63 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -261,6 +261,15 @@ class _RealmCore { afterResetUserdata.cast(), _realmLib.addresses.realm_dart_userdata_async_free); } + if (config.shouldCompactCallback != null) { + _realmLib.realm_config_set_should_compact_on_launch_function( + configHandle._pointer, + Pointer.fromFunction(should_compact_callback, false), + config.toWeakHandle(), + nullptr, + ); + } + _realmLib.realm_config_set_sync_config(configPtr, syncConfigPtr); } finally { _realmLib.realm_release(syncConfigPtr.cast()); @@ -476,12 +485,15 @@ class _RealmCore { } static bool should_compact_callback(Pointer userdata, int totalSize, int usedSize) { - final LocalConfiguration? config = userdata.toObject(); - if (config == null) { - return false; + Object? config = userdata.toObject(); + + if (config is LocalConfiguration) { + return config.shouldCompactCallback!(totalSize, usedSize); + } else if (config is FlexibleSyncConfiguration) { + return config.shouldCompactCallback!(totalSize, usedSize); } - return config.shouldCompactCallback!(totalSize, usedSize); + return false; } static bool migration_callback( diff --git a/test/app_test.dart b/test/app_test.dart index 793e527a1..ac30d3488 100644 --- a/test/app_test.dart +++ b/test/app_test.dart @@ -192,7 +192,7 @@ Future main([List? args]) async { configuration.appId, baseFilePath: configuration.baseFilePath, baseUrl: configuration.baseUrl, - ); // uses App.defaultLogger + ); await testLogger( configuration, diff --git a/test/configuration_test.dart b/test/configuration_test.dart index aa0b9e2d3..570240aaf 100644 --- a/test/configuration_test.dart +++ b/test/configuration_test.dart @@ -492,6 +492,20 @@ Future main([List? args]) async { }); } + baasTest('Configuration.flexibleSync shouldCompactCallback is invoked', (appConfig) async { + final app = App(appConfig); + final user = await app.logIn(Credentials.emailPassword(testUsername, testPassword)); + + var invoked = false; + var config = Configuration.flexibleSync(user, [Event.schema], shouldCompactCallback: (totalSize, usedSize) { + invoked = true; + return false; + }); + + final realm = getRealm(config); + expect(invoked, isTrue); + }); + baasTest('Configuration.flexibleSync suggests correct path', (appConfig) async { final app = App(appConfig); final user = await app.logIn(Credentials.emailPassword(testUsername, testPassword)); From 9094640011726772af9501f2fb54f27a71bfd8e0 Mon Sep 17 00:00:00 2001 From: blagoev Date: Thu, 9 Mar 2023 11:48:52 +0200 Subject: [PATCH 2/2] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d075943b7..b8dabefa6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Enhancements * Deprecated `SyncResolveError` and `SyncResolveErrorCode` ([#1182](https://github.com/realm/realm-dart/pull/1182)). * Added `SyncWebSocketError` and `SyncWebSocketErrorCode` for web socket connection sync errors ([#1182](https://github.com/realm/realm-dart/pull/1182)). +* Added `FlexibleSyncConfiguration.shouldCompactCallback` support ([#1204](https://github.com/realm/realm-dart/pull/1204)). ### Fixed * Fixed error message when trying to `switchUser` of the `App` to a user that has been logged out ([#1182](https://github.com/realm/realm-dart/pull/1182)).