Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
461 additions
and
500 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
packages/realm_dart/lib/src/native/mutable_subscription_set_handle.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} |
Oops, something went wrong.