Skip to content

Commit

Permalink
Take 2
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed May 31, 2024
1 parent 8de2a83 commit 04dbf20
Show file tree
Hide file tree
Showing 22 changed files with 347 additions and 63 deletions.
37 changes: 35 additions & 2 deletions packages/realm_dart/lib/src/handles/app_handle.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

export 'native/app_handle.dart'
if (dart.library.js_interop) 'web/app_handle.dart';
import '../../realm.dart';

import 'credentials_handle.dart';
import 'native/app_handle.dart' if (dart.library.js_interop) 'web/app_handle.dart' as impl;
import 'user_handle.dart';

abstract interface class AppHandle {
factory AppHandle.from(AppConfiguration configuration) = impl.AppHandle.from;
factory AppHandle.get(String id, String? baseUrl) = impl.AppHandle.get;

String get id;

UserHandle? get currentUser;
List<UserHandle> get users;
Future<UserHandle> logIn(CredentialsHandle credentials);
Future<void> removeUser(UserHandle user);
void switchUser(UserHandle user);
Future<void> refreshCustomData(UserHandle user);

void reconnect();
String get baseUrl;
Future<void> updateBaseUrl(Uri? baseUrl);

Future<void> registerUser(String email, String password);
Future<void> confirmUser(String token, String tokenId);
Future<void> resendConfirmation(String email);

Future<void> completeResetPassword(String password, String token, String tokenId);
Future<void> requestResetPassword(String email);
Future<void> callResetPasswordFunction(String email, String password, String? argsAsJSON);
Future<void> retryCustomConfirmationFunction(String email);
Future<void> deleteUser(UserHandle user);
bool resetRealm(String realmPath);
Future<String> callAppFunction(UserHandle user, String functionName, String? argsAsJSON);
}
19 changes: 17 additions & 2 deletions packages/realm_dart/lib/src/handles/async_open_task_handle.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

export 'native/async_open_task_handle.dart'
if (dart.library.js_interop) 'web/async_open_task_handle.dart';
import '../realm_class.dart';
import 'handle_base.dart';
import 'native/async_open_task_handle.dart' if (dart.library.js_interop) 'web/async_open_task_handle.dart' as impl;
import 'realm_handle.dart';

abstract class AsyncOpenTaskHandle extends HandleBase {
factory AsyncOpenTaskHandle.from(FlexibleSyncConfiguration config) = impl.AsyncOpenTaskHandle.from;

Future<RealmHandle> openAsync(CancellationToken? cancellationToken);
void cancel();

AsyncOpenTaskProgressNotificationTokenHandle registerProgressNotifier(
RealmAsyncOpenProgressNotificationsController controller,
);
}

abstract class AsyncOpenTaskProgressNotificationTokenHandle extends HandleBase {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

export 'native/collection_changes_handle.dart'
if (dart.library.js_interop) 'web/collection_changes_handle.dart';
import '../collections.dart';
import 'handle_base.dart';

abstract interface class CollectionChangesHandle extends HandleBase {
CollectionChanges get changes;
}
8 changes: 6 additions & 2 deletions packages/realm_dart/lib/src/handles/handle_base.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

export 'native/handle_base.dart'
if (dart.library.js_interop) 'web/handle_base.dart';
abstract class HandleBase {
bool get released;
bool get isUnowned;
void releaseCore();
void release();
}
42 changes: 40 additions & 2 deletions packages/realm_dart/lib/src/handles/list_handle.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

export 'native/list_handle.dart'
if (dart.library.js_interop) 'web/list_handle.dart';
import '../realm_dart.dart';
import 'handle_base.dart';
import 'notification_token_handle.dart';
import 'object_handle.dart';
import 'realm_handle.dart';
import 'results_handle.dart';

abstract interface class ListHandle extends HandleBase {
bool get isValid;

ResultsHandle asResults();

int get size;

void removeAt(int index);

void move(int from, int to);
void deleteAll();
int indexOf(Object? value);
void clear();

// TODO: avoid taking the [realm] parameter
Object? elementAt(Realm realm, int index);

ListHandle? resolveIn(RealmHandle frozenRealm);

// TODO: Consider splitting into two methods
void addOrUpdateAt(int index, Object? value, bool insert);

// TODO: avoid taking the [realm] parameter
void addOrUpdateCollectionAt(Realm realm, int index, RealmValue value, bool insert);

ObjectHandle setEmbeddedAt(int index);

ObjectHandle insertEmbeddedAt(int index);

ResultsHandle query(String query, List<Object?> args);

NotificationTokenHandle subscribeForNotifications(NotificationsController controller);
}
8 changes: 6 additions & 2 deletions packages/realm_dart/lib/src/handles/map_changes_handle.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

export 'native/map_changes_handle.dart'
if (dart.library.js_interop) 'web/map_changes_handle.dart';
import '../collections.dart';
import 'handle_base.dart';

abstract class MapChangesHandle extends HandleBase {
MapChanges get changes;
}
30 changes: 28 additions & 2 deletions packages/realm_dart/lib/src/handles/map_handle.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

export 'native/map_handle.dart'
if (dart.library.js_interop) 'web/map_handle.dart';
import '../realm_class.dart';
import 'handle_base.dart';
import 'notification_token_handle.dart';
import 'object_handle.dart';
import 'realm_handle.dart';
import 'results_handle.dart';

abstract class MapHandle extends HandleBase {
bool get isValid;
ResultsHandle get keys;
int get size;
ResultsHandle get values;

void clear();
bool containsKey(String key);
bool containsValue(Object? value);
// TODO: avoid taking a [Realm] as parameter (wrong layer)
Object? find(Realm realm, String key);
int indexOf(Object? value);
void insert(String key, Object? value);
// TODO: avoid taking a [Realm] as parameter (wrong layer)
void insertCollection(Realm realm, String key, RealmValue value);
ObjectHandle insertEmbedded(String key);
ResultsHandle query(String query, List<Object?> args);
bool remove(String key);
MapHandle? resolveIn(RealmHandle frozenRealm);
NotificationTokenHandle subscribeForNotifications(NotificationsController controller);
}
38 changes: 30 additions & 8 deletions packages/realm_dart/lib/src/handles/native/app_handle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import 'realm_core.dart';
import 'realm_library.dart';
import 'user_handle.dart';

class AppHandle extends HandleBase<realm_app> {
import '../app_handle.dart' as intf;

class AppHandle extends HandleBase<realm_app> implements intf.AppHandle {
AppHandle(Pointer<realm_app> pointer) : super(pointer, 16);

static bool _firstTime = true;
Expand All @@ -37,7 +39,7 @@ class AppHandle extends HandleBase<realm_app> {
return AppHandle(realmLib.realm_app_create_cached(appConfigHandle.pointer));
}

static AppHandle? get(String id, String? baseUrl) {
factory AppHandle.get(String id, String? baseUrl) {
return using((arena) {
final outApp = arena<Pointer<realm_app>>();
realmLib
Expand All @@ -47,14 +49,16 @@ class AppHandle extends HandleBase<realm_app> {
outApp,
)
.raiseLastErrorIfFalse();
return outApp.value.convert(AppHandle.new);
return AppHandle(outApp.value);
});
}

@override
UserHandle? get currentUser {
return realmLib.realm_app_get_current_user(pointer).convert(UserHandle.new);
}

@override
List<UserHandle> get users => using((arena) => _getUsers(arena));

List<UserHandle> _getUsers(Arena arena, {int expectedSize = 2}) {
Expand All @@ -76,7 +80,8 @@ class AppHandle extends HandleBase<realm_app> {
return result;
}

Future<void> removeUser(UserHandle user) {
@override
Future<void> removeUser(covariant UserHandle user) {
final completer = Completer<void>();
realmLib
.realm_app_remove_user(
Expand All @@ -90,7 +95,8 @@ class AppHandle extends HandleBase<realm_app> {
return completer.future;
}

void switchUser(UserHandle user) {
@override
void switchUser(covariant UserHandle user) {
using((arena) {
realmLib
.realm_app_switch_user(
Expand All @@ -101,13 +107,16 @@ class AppHandle extends HandleBase<realm_app> {
});
}

@override
void reconnect() => realmLib.realm_app_sync_client_reconnect(pointer);

@override
String get baseUrl {
final customDataPtr = realmLib.realm_app_get_base_url(pointer);
return customDataPtr.cast<Utf8>().toRealmDartString(freeRealmMemory: true)!;
}

@override
Future<void> updateBaseUrl(Uri? baseUrl) {
final completer = Completer<void>();
using((arena) {
Expand All @@ -124,7 +133,8 @@ class AppHandle extends HandleBase<realm_app> {
return completer.future;
}

Future<void> refreshCustomData(UserHandle user) {
@override
Future<void> refreshCustomData(covariant UserHandle user) {
final completer = Completer<void>();
realmLib
.realm_app_refresh_custom_data(
Expand All @@ -138,10 +148,12 @@ class AppHandle extends HandleBase<realm_app> {
return completer.future;
}

@override
String get id {
return realmLib.realm_app_get_app_id(pointer).cast<Utf8>().toRealmDartString()!;
}

@override
Future<UserHandle> logIn(CredentialsHandle credentials) {
final completer = Completer<UserHandle>();
realmLib
Expand All @@ -156,6 +168,7 @@ class AppHandle extends HandleBase<realm_app> {
return completer.future;
}

@override
Future<void> registerUser(String email, String password) {
final completer = Completer<void>();
using((arena) {
Expand All @@ -173,6 +186,7 @@ class AppHandle extends HandleBase<realm_app> {
return completer.future;
}

@override
Future<void> confirmUser(String token, String tokenId) {
final completer = Completer<void>();
using((arena) {
Expand All @@ -190,6 +204,7 @@ class AppHandle extends HandleBase<realm_app> {
return completer.future;
}

@override
Future<void> resendConfirmation(String email) {
final completer = Completer<void>();
using((arena) {
Expand All @@ -206,6 +221,7 @@ class AppHandle extends HandleBase<realm_app> {
return completer.future;
}

@override
Future<void> completeResetPassword(String password, String token, String tokenId) {
final completer = Completer<void>();
using((arena) {
Expand All @@ -224,6 +240,7 @@ class AppHandle extends HandleBase<realm_app> {
return completer.future;
}

@override
Future<void> requestResetPassword(String email) {
final completer = Completer<void>();
using((arena) {
Expand All @@ -240,6 +257,7 @@ class AppHandle extends HandleBase<realm_app> {
return completer.future;
}

@override
Future<void> callResetPasswordFunction(String email, String password, String? argsAsJSON) {
final completer = Completer<void>();
using((arena) {
Expand All @@ -258,6 +276,7 @@ class AppHandle extends HandleBase<realm_app> {
return completer.future;
}

@override
Future<void> retryCustomConfirmationFunction(String email) {
final completer = Completer<void>();
using((arena) {
Expand All @@ -274,7 +293,8 @@ class AppHandle extends HandleBase<realm_app> {
return completer.future;
}

Future<void> deleteUser(UserHandle user) {
@override
Future<void> deleteUser(covariant UserHandle user) {
final completer = Completer<void>();
realmLib
.realm_app_delete_user(
Expand All @@ -288,6 +308,7 @@ class AppHandle extends HandleBase<realm_app> {
return completer.future;
}

@override
bool resetRealm(String realmPath) {
return using((arena) {
final didRun = arena<Bool>();
Expand All @@ -302,7 +323,8 @@ class AppHandle extends HandleBase<realm_app> {
});
}

Future<String> callAppFunction(UserHandle user, String functionName, String? argsAsJSON) {
@override
Future<String> callAppFunction(covariant UserHandle user, String functionName, String? argsAsJSON) {
return using((arena) {
final completer = Completer<String>();
realmLib
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import 'realm_handle.dart';
import 'realm_library.dart';
import 'session_handle.dart';

class AsyncOpenTaskHandle extends HandleBase<realm_async_open_task_t> {
import '../async_open_task_handle.dart' as intf;

class AsyncOpenTaskHandle extends HandleBase<realm_async_open_task_t> implements intf.AsyncOpenTaskHandle {
AsyncOpenTaskHandle(Pointer<realm_async_open_task_t> pointer) : super(pointer, 32);

factory AsyncOpenTaskHandle.from(FlexibleSyncConfiguration config) {
Expand Down Expand Up @@ -58,7 +60,8 @@ class AsyncOpenTaskHandle extends HandleBase<realm_async_open_task_t> {
}
}

class AsyncOpenTaskProgressNotificationTokenHandle extends HandleBase<realm_async_open_task_progress_notification_token_t> {
class AsyncOpenTaskProgressNotificationTokenHandle extends HandleBase<realm_async_open_task_progress_notification_token_t>
implements intf.AsyncOpenTaskProgressNotificationTokenHandle {
AsyncOpenTaskProgressNotificationTokenHandle(Pointer<realm_async_open_task_progress_notification_token_t> pointer) : super(pointer, 40);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import 'handle_base.dart';
import 'realm_bindings.dart';
import 'realm_library.dart';

class CollectionChangesHandle extends HandleBase<realm_collection_changes> {
import '../collection_changes_handle.dart' as intf;

class CollectionChangesHandle extends HandleBase<realm_collection_changes> implements intf.CollectionChangesHandle {
CollectionChangesHandle(Pointer<realm_collection_changes> pointer) : super(pointer, 256);

@override
CollectionChanges get changes {
return using((arena) {
final outNumDeletions = arena<Size>();
Expand Down
Loading

0 comments on commit 04dbf20

Please sign in to comment.