Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support flx sync compact callback #1204

Merged
merged 3 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
6 changes: 6 additions & 0 deletions lib/src/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ abstract class Configuration implements Finalizable {
SyncErrorHandler syncErrorHandler = defaultSyncErrorHandler,
ClientResetHandler clientResetHandler = const RecoverOrDiscardUnsyncedChangesHandler(onManualResetFallback: _defaultClientResetHandler),
int? maxNumberOfActiveVersions,
ShouldCompactCallback? shouldCompactCallback,
}) =>
FlexibleSyncConfiguration._(
user,
Expand All @@ -213,6 +214,7 @@ abstract class Configuration implements Finalizable {
syncErrorHandler: syncErrorHandler,
clientResetHandler: clientResetHandler,
maxNumberOfActiveVersions: maxNumberOfActiveVersions,
shouldCompactCallback: shouldCompactCallback,
);

/// Constructs a [DisconnectedSyncConfiguration]
Expand Down Expand Up @@ -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, {
Expand All @@ -359,6 +364,7 @@ class FlexibleSyncConfiguration extends Configuration {
this.syncErrorHandler = defaultSyncErrorHandler,
this.clientResetHandler = const RecoverOrDiscardUnsyncedChangesHandler(onManualResetFallback: _defaultClientResetHandler),
super.maxNumberOfActiveVersions,
this.shouldCompactCallback,
}) : super._();

@override
Expand Down
20 changes: 16 additions & 4 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -476,12 +485,15 @@ class _RealmCore {
}

static bool should_compact_callback(Pointer<Void> 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(
Expand Down
2 changes: 1 addition & 1 deletion test/app_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Future<void> main([List<String>? args]) async {
configuration.appId,
baseFilePath: configuration.baseFilePath,
baseUrl: configuration.baseUrl,
); // uses App.defaultLogger
);

await testLogger(
configuration,
Expand Down
14 changes: 14 additions & 0 deletions test/configuration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,20 @@ Future<void> main([List<String>? 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));
Expand Down