Skip to content

Commit

Permalink
Fix PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed May 9, 2022
1 parent 6cb1735 commit 6bba536
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 68 deletions.
42 changes: 14 additions & 28 deletions lib/src/configuration.dart
Expand Up @@ -24,11 +24,8 @@ import 'native/realm_core.dart';
import 'realm_class.dart';

abstract class Configuration {
/// The default filename of a [Realm] database
static const String defaultRealmName = "default.realm";

static String _initDefaultPath() {
var path = defaultRealmName;
var path = "default.realm";
if (Platform.isAndroid || Platform.isIOS) {
path = _path.join(realmCore.getFilesPath(), path);
}
Expand All @@ -53,7 +50,6 @@ abstract class Configuration {
String? get fifoFilesFallbackPath;
String get path;
RealmSchema get schema;
int get schemaVersion;
List<int>? get encryptionKey;

@Deprecated('Use Configuration.local instead')
Expand Down Expand Up @@ -82,7 +78,6 @@ abstract class Configuration {
factory Configuration.inMemory(
List<SchemaObject> schemaObjects,
String identifier, {
Function(Realm realm)? initialDataCallback,
int schemaVersion,
String? fifoFilesFallbackPath,
String? path,
Expand All @@ -91,8 +86,6 @@ abstract class Configuration {
factory Configuration.flexibleSync(
User user,
List<SchemaObject> schemaObjects, {
Function(Realm realm)? initialDataCallback,
int schemaVersion,
String? fifoFilesFallbackPath,
String? path,
}) = FlexibleSyncConfiguration;
Expand All @@ -106,7 +99,6 @@ class _ConfigurationBase implements Configuration {
List<SchemaObject> schemaObjects, {
String? path,
this.fifoFilesFallbackPath,
this.schemaVersion = 0,
this.encryptionKey,
}) : schema = RealmSchema(schemaObjects),
path = path ?? Configuration.defaultPath;
Expand All @@ -115,23 +107,14 @@ class _ConfigurationBase implements Configuration {
@override
final RealmSchema schema;

/// The schema version used to open the [Realm]
///
/// If omitted the default value of `0` is used to open the [Realm]
/// It is required to specify a schema version when initializing an existing
/// Realm with a schema that contains objects that differ from their previous
/// specification. If the schema was updated and the schemaVersion was not,
/// an [RealmException] will be thrown.
@override
final int schemaVersion;

/// The path where the Realm should be stored.
///
/// If omitted the [defaultPath] for the platform will be used.
@override
final String path;

/// Specifies the FIFO special files fallback location.
///
/// Opening a [Realm] creates a number of FIFO special files in order to
/// coordinate access to the [Realm] across threads and processes. If the [Realm] file is stored in a location
/// that does not allow the creation of FIFO special files (e.g. FAT32 filesystems), then the [Realm] cannot be opened.
Expand All @@ -149,7 +132,7 @@ class LocalConfiguration extends _ConfigurationBase {
LocalConfiguration(
List<SchemaObject> schemaObjects, {
this.initialDataCallback,
int schemaVersion = 0,
this.schemaVersion = 0,
String? fifoFilesFallbackPath,
String? path,
this.disableFormatUpgrade = false,
Expand All @@ -160,9 +143,18 @@ class LocalConfiguration extends _ConfigurationBase {
schemaObjects,
path: path,
fifoFilesFallbackPath: fifoFilesFallbackPath,
schemaVersion: schemaVersion,
);

/// The schema version used to open the [Realm]
///
/// If omitted the default value of `0` is used to open the [Realm]
/// It is required to specify a schema version when initializing an existing
/// Realm with a schema that contains objects that differ from their previous
/// specification. If the schema was updated and the schemaVersion was not,
/// an [RealmException] will be thrown.
@override
final int schemaVersion;

/// Specifies whether a [Realm] should be opened as read-only.
/// This allows opening it from locked locations such as resources,
/// bundled with an application.
Expand Down Expand Up @@ -206,17 +198,16 @@ class _SyncConfigurationBase extends _ConfigurationBase {
_SyncConfigurationBase(
this.user,
List<SchemaObject> schemaObjects, {
int schemaVersion = 0,
String? fifoFilesFallbackPath,
String? path,
}) : super(
schemaObjects,
schemaVersion: schemaVersion,
fifoFilesFallbackPath: fifoFilesFallbackPath,
path: path,
);
}

/// @nodoc
enum SessionStopPolicy {
immediately, // Immediately stop the session as soon as all Realms/Sessions go out of scope.
liveIndefinitely, // Never stop the session.
Expand All @@ -229,14 +220,11 @@ class FlexibleSyncConfiguration extends _SyncConfigurationBase {
FlexibleSyncConfiguration(
User user,
List<SchemaObject> schemaObjects, {
Function(Realm realm)? initialDataCallback,
int schemaVersion = 0,
String? fifoFilesFallbackPath,
String? path,
}) : super(
user,
schemaObjects,
schemaVersion: schemaVersion,
fifoFilesFallbackPath: fifoFilesFallbackPath,
path: path,
);
Expand All @@ -251,13 +239,11 @@ class InMemoryConfiguration extends _ConfigurationBase {
InMemoryConfiguration(
List<SchemaObject> schemaObjects,
this.identifier, {
Function(Realm realm)? initialDataCallback,
int schemaVersion = 0,
String? fifoFilesFallbackPath,
String? path,
}) : super(
schemaObjects,
schemaVersion: schemaVersion,
fifoFilesFallbackPath: fifoFilesFallbackPath,
path: path,
);
Expand Down
23 changes: 11 additions & 12 deletions lib/src/native/realm_core.dart
Expand Up @@ -149,14 +149,17 @@ class _RealmCore {
final configHandle = ConfigHandle._(configPtr);

_realmLib.realm_config_set_schema(configHandle._pointer, schemaHandle._pointer);
_realmLib.realm_config_set_schema_version(configHandle._pointer, config.schemaVersion);
_realmLib.realm_config_set_path(configHandle._pointer, config.path.toUtf8Ptr(arena));
_realmLib.realm_config_set_scheduler(configHandle._pointer, schedulerHandle._pointer);

if (config.fifoFilesFallbackPath != null) {
_realmLib.realm_config_set_fifo_path(configHandle._pointer, config.fifoFilesFallbackPath!.toUtf8Ptr(arena));
}

// Setting schema version only makes sense for local realms, but core insists it is always set,
// hence we set it to 0 in those cases.
_realmLib.realm_config_set_schema_version(configHandle._pointer, config is LocalConfiguration ? config.schemaVersion : 0);

if (config is LocalConfiguration) {
if (config.initialDataCallback != null) {
_realmLib.realm_config_set_data_initialization_function(
Expand Down Expand Up @@ -232,18 +235,15 @@ class _RealmCore {
}

static void _stateChangeCallback(Pointer<Void> userdata, int state) {
final completer = userdata.toObject<Completer<int>>(isPersistent: true);
final completer = userdata.toObject<Completer<SubscriptionSetState>>(isPersistent: true);
if (completer == null) {
return;
}

// TODO: What about errors?!

completer.complete(state);
completer.complete(SubscriptionSetState.values[state]);
}

Future<int> waitForSubscriptionSetStateChange(SubscriptionSet subscriptions, SubscriptionSetState notifyWhen) {
final completer = Completer<int>();
Future<SubscriptionSetState> waitForSubscriptionSetStateChange(SubscriptionSet subscriptions, SubscriptionSetState notifyWhen) {
final completer = Completer<SubscriptionSetState>();
_realmLib.realm_dart_sync_on_subscription_set_state_change_async(
subscriptions.handle._pointer,
notifyWhen.index,
Expand All @@ -255,13 +255,12 @@ class _RealmCore {
return completer.future;
}

int subscriptionSetVersion(SubscriptionSet subscriptions) {
int subscriptionSetGetVersion(SubscriptionSet subscriptions) {
return _realmLib.realm_sync_subscription_set_version(subscriptions.handle._pointer);
}

int subscriptionSetState(SubscriptionSet subscriptions) {
refreshSubscriptionSet(subscriptions);
return _realmLib.realm_sync_subscription_set_state(subscriptions.handle._pointer);
SubscriptionSetState subscriptionSetGetState(SubscriptionSet subscriptions) {
return SubscriptionSetState.values[_realmLib.realm_sync_subscription_set_state(subscriptions.handle._pointer)];
}

MutableSubscriptionSetHandle makeSubscriptionSetMutable(SubscriptionSet subscriptions) {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/realm_class.dart
Expand Up @@ -270,8 +270,9 @@ class Realm {
SubscriptionSet? _subscriptions;
/// The active [subscriptions] for this [Realm]
SubscriptionSet get subscriptions {
if (config is! FlexibleSyncConfiguration) throw RealmError('Does not support flexible synchronization');
if (config is! FlexibleSyncConfiguration) throw RealmError('subscriptions is only valid on Realms opened with a FlexibleSyncConfiguration');
_subscriptions ??= SubscriptionSetInternal.create(this, realmCore.getSubscriptions(this));
realmCore.refreshSubscriptionSet(_subscriptions!);
return _subscriptions!;
}

Expand Down
13 changes: 9 additions & 4 deletions lib/src/subscription.dart
Expand Up @@ -16,6 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////////
import 'dart:async';
import 'dart:collection';

import 'native/realm_core.dart';
Expand Down Expand Up @@ -76,10 +77,14 @@ abstract class SubscriptionSet with IterableMixin<Subscription> {
return realmCore.findSubscriptionByName(this, name).convert(Subscription._);
}

Future<SubscriptionSetState> waitForStateChange(SubscriptionSetState state) async {
return SubscriptionSetState.values[await realmCore.waitForSubscriptionSetStateChange(this, state)];
Future<SubscriptionSetState> _waitForStateChange(SubscriptionSetState state) async {
final result = await realmCore.waitForSubscriptionSetStateChange(this, state);
realmCore.refreshSubscriptionSet(this);
return result;
}

Future<SubscriptionSetState> waitForSynchronization() => _waitForStateChange(SubscriptionSetState.complete);

@override
int get length => realmCore.getSubscriptionSetSize(this);

Expand All @@ -95,9 +100,9 @@ abstract class SubscriptionSet with IterableMixin<Subscription> {

void update(void Function(MutableSubscriptionSet mutableSubscriptions) action);

int get version => realmCore.subscriptionSetVersion(this);
int get version => realmCore.subscriptionSetGetVersion(this);

SubscriptionSetState get state => SubscriptionSetState.values[realmCore.subscriptionSetState(this)];
SubscriptionSetState get state => realmCore.subscriptionSetGetState(this);
}

extension SubscriptionSetInternal on SubscriptionSet {
Expand Down
4 changes: 2 additions & 2 deletions test/configuration_test.dart
Expand Up @@ -64,10 +64,10 @@ Future<void> main([List<String>? args]) async {
});

test('Configuration get/set schema version', () {
final config = Configuration([Car.schema]);
final config = LocalConfiguration([Car.schema]);
expect(config.schemaVersion, equals(0));

final explicitSchemaConfig = Configuration([Car.schema], schemaVersion: 3);
final explicitSchemaConfig = LocalConfiguration([Car.schema], schemaVersion: 3);
expect(explicitSchemaConfig.schemaVersion, equals(3));
});

Expand Down
24 changes: 3 additions & 21 deletions test/subscription_test.dart
Expand Up @@ -55,7 +55,7 @@ Future<void> main([List<String>? args]) async {

testSubscriptions('SubscriptionSet.state', (realm) async {
final subscriptions = realm.subscriptions;
await subscriptions.waitForStateChange(SubscriptionSetState.complete);
await subscriptions.waitForSynchronization();
expect(subscriptions.state, SubscriptionSetState.complete);
});

Expand Down Expand Up @@ -190,24 +190,6 @@ Future<void> main([List<String>? args]) async {
}
});

testSubscriptions('SubscriptionSet.waitForStateChange', (realm) async {
final subscriptions = realm.subscriptions;
await subscriptions.waitForStateChange(SubscriptionSetState.complete);

final stateMachineSteps = [
subscriptions.waitForStateChange(SubscriptionSetState.uncommitted),
subscriptions.waitForStateChange(SubscriptionSetState.pending),
subscriptions.waitForStateChange(SubscriptionSetState.bootstrapping),
subscriptions.waitForStateChange(SubscriptionSetState.complete),
];

subscriptions.update((mutableSubscriptions) {
mutableSubscriptions.addOrUpdate(realm.all<Task>());
});

await Future.wait(stateMachineSteps);
});

testSubscriptions('Get subscriptions', (realm) async {
final subscriptions = realm.subscriptions;

Expand Down Expand Up @@ -239,8 +221,8 @@ Future<void> main([List<String>? args]) async {
});

expect(subscriptions, isEmpty);
// expect(realm.subscriptions!.findByName(name), isNull); // TODO
expect(realm.subscriptions!.findByName(name), isNull);

await subscriptions.waitForStateChange(SubscriptionSetState.complete);
await subscriptions.waitForSynchronization();
});
}

0 comments on commit 6bba536

Please sign in to comment.