Skip to content

Commit

Permalink
Refactor handles subscription.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Mar 11, 2024
1 parent 98c4a7e commit 2e43af9
Show file tree
Hide file tree
Showing 8 changed files with 461 additions and 500 deletions.
17 changes: 17 additions & 0 deletions packages/realm_dart/lib/src/native/convert.dart
@@ -0,0 +1,17 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

part of 'realm_core.dart';

extension HandleBaseEx<T extends HandleBase> on T {
T? get nullPtrAsNull => _pointer == nullptr ? null : this;
U? convert<U>(U Function(T) convertor) => nullPtrAsNull.convert(convertor);
}

extension NullableObjectEx<T> on T? {
U? convert<U>(U Function(T) convertor) {
final self = this;
if (self == null) return null;
return convertor(self);
}
}
60 changes: 60 additions & 0 deletions packages/realm_dart/lib/src/native/error_handling.dart
@@ -0,0 +1,60 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

part of 'realm_core.dart';

void invokeGetBool(bool Function() callback, [String? errorMessage]) {
bool success = callback();
if (!success) {
throwLastError(errorMessage);
}
}

Pointer<T> invokeGetPointer<T extends NativeType>(Pointer<T> Function() callback, [String? errorMessage]) {
final result = callback();
if (result == nullptr) {
throwLastError(errorMessage);
}
return result;
}

class LastError {
final int code;
final String? message;
final Object? userError;

LastError(this.code, [this.message, this.userError]);

@override
String toString() => "${message ?? 'No message'}. Error code: $code.";
}

LastError? getLastError(Allocator allocator) {
final error = allocator<realm_error_t>();
final success = _realmLib.realm_get_last_error(error);
return success ? error.ref.toDart() : null;
}

Never throwLastError([String? errorMessage]) {
using((Arena arena) {
final lastError = getLastError(arena);
if (lastError?.userError != null) {
throw UserCallbackException(lastError!.userError!);
}

final message = '${errorMessage != null ? "$errorMessage. " : ""}${lastError ?? ""}';
switch (lastError?.code) {
case realm_errno.RLM_ERR_SCHEMA_MISMATCH:
throw MigrationRequiredException(message);
default:
throw RealmException(message);
}
});
}

extension on realm_error {
LastError toDart() {
final message = this.message.cast<Utf8>().toRealmDartString();
return LastError(error, message, user_code_error.toUserCodeError());
}
}
@@ -0,0 +1,70 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

part of 'realm_core.dart';

class MutableSubscriptionSetHandle extends SubscriptionSetHandle {
MutableSubscriptionSetHandle._(Pointer<realm_flx_sync_mutable_subscription_set> pointer, RealmHandle root) : super._(pointer.cast(), root);

Pointer<realm_flx_sync_mutable_subscription_set> get _mutablePointer => super._pointer.cast();

SubscriptionSetHandle commit() => SubscriptionSetHandle._(invokeGetPointer(() => _realmLib.realm_sync_subscription_set_commit(_mutablePointer)), _root);

SubscriptionHandle insertOrAssignSubscription(RealmResults results, String? name, bool update) {
if (!update) {
if (name != null && findByName(name) != null) {
throw RealmException('Duplicate subscription with name: $name');
}
}
return using((arena) {
final outIndex = arena<Size>();
final outInserted = arena<Bool>();
invokeGetBool(() => _realmLib.realm_sync_subscription_set_insert_or_assign_results(
_mutablePointer,
results.handle._pointer,
name?.toCharPtr(arena) ?? nullptr,
outIndex,
outInserted,
));
return this[outIndex.value];
});
}

bool erase(Subscription subscription) {
return using((arena) {
final outErased = arena<Bool>();
invokeGetBool(() => _realmLib.realm_sync_subscription_set_erase_by_id(
_mutablePointer,
subscription.id.toNative(arena),
outErased,
));
return outErased.value;
});
}

bool eraseByName(String name) {
return using((arena) {
final outErased = arena<Bool>();
invokeGetBool(() => _realmLib.realm_sync_subscription_set_erase_by_name(
_mutablePointer,
name.toCharPtr(arena),
outErased,
));
return outErased.value;
});
}

bool eraseByResults(RealmResults results) {
return using((arena) {
final outErased = arena<Bool>();
invokeGetBool(() => _realmLib.realm_sync_subscription_set_erase_by_results(
_mutablePointer,
results.handle._pointer,
outErased,
));
return outErased.value;
});
}

void clear() => invokeGetBool(() => _realmLib.realm_sync_subscription_set_clear(_mutablePointer));
}

0 comments on commit 2e43af9

Please sign in to comment.