Skip to content

Commit

Permalink
Merge 2e43af9 into 17827e8
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Mar 11, 2024
2 parents 17827e8 + 2e43af9 commit e1a92c9
Show file tree
Hide file tree
Showing 10 changed files with 468 additions and 502 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-native.yml
Expand Up @@ -55,7 +55,7 @@ jobs:
if: matrix.build == 'macos'
run: |
xcodes installed
xcodes select 15.2
sudo xcodes select 15.2
- name: Build
if: steps.check-cache.outputs.cache-hit != 'true'
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Expand Up @@ -279,7 +279,7 @@ jobs:
differentiator: fi${{ github.run_id }}${{ github.run_attempt }}

flutter-tests-ios:
runs-on: macos-latest
runs-on: macos-14
name: Flutter Tests iOS
timeout-minutes: 45
needs:
Expand All @@ -293,6 +293,11 @@ jobs:
working-directory: packages/realm/tests

steps:
- name: Select XCode for MacOS
run: |
xcodes installed
sudo xcodes select 15.2
- name: Checkout
uses: actions/checkout@v4
with:
Expand Down
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 e1a92c9

Please sign in to comment.