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 461fecd
Show file tree
Hide file tree
Showing 63 changed files with 980 additions and 173 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;
static AppHandle? get(String id, String? baseUrl) => impl.AppHandle.get(id, baseUrl);

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 interface 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();
}
31 changes: 29 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,32 @@
// 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;
int get size;

// 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);
ResultsHandle asResults();
void clear();
void deleteAll();
// TODO: avoid taking the [realm] parameter
Object? elementAt(Realm realm, int index);
int indexOf(Object? value);
ObjectHandle insertEmbeddedAt(int index);
void move(int from, int to);
ResultsHandle query(String query, List<Object?> args);
void removeAt(int index);
ListHandle? resolveIn(RealmHandle frozenRealm);
ObjectHandle setEmbeddedAt(int index);
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 interface 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 interface 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);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

export 'native/mutable_subscription_set_handle.dart'
if (dart.library.js_interop) 'web/mutable_subscription_set_handle.dart';
import 'results_handle.dart';
import 'subscription_handle.dart';
import 'subscription_set_handle.dart';

abstract interface class MutableSubscriptionSetHandle extends SubscriptionSetHandle {
SubscriptionSetHandle commit();

SubscriptionHandle insertOrAssignSubscription(ResultsHandle results, String? name, bool update);

bool erase(SubscriptionHandle subscription);
bool eraseByName(String name);
bool eraseByResults(ResultsHandle results);

void clear();
}
40 changes: 31 additions & 9 deletions packages/realm_dart/lib/src/handles/native/app_handle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'dart:isolate';

import '../../init.dart';
import '../../realm_class.dart';
import '../../scheduler.dart';
import 'convert.dart';
import 'convert_native.dart';
import 'credentials_handle.dart';
Expand All @@ -19,9 +18,12 @@ import 'http_transport_handle.dart';
import 'realm_bindings.dart';
import 'realm_core.dart';
import 'realm_library.dart';
import 'scheduler_handle.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 Down Expand Up @@ -51,10 +53,12 @@ class AppHandle extends HandleBase<realm_app> {
});
}

@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 Expand Up @@ -333,7 +355,7 @@ Pointer<Void> createAsyncFunctionCallbackUserdata(Completer<String> completer) {
final userdata = realmLib.realm_dart_userdata_async_new(
completer,
callback.cast(),
scheduler.handle.pointer,
schedulerHandle.pointer,
);

return userdata.cast();
Expand Down Expand Up @@ -361,7 +383,7 @@ Pointer<Void> createAsyncCallbackUserdata(Completer<void> completer) {
final userdata = realmLib.realm_dart_userdata_async_new(
completer,
callback.cast(),
scheduler.handle.pointer,
schedulerHandle.pointer,
);

return userdata.cast();
Expand Down
Loading

0 comments on commit 461fecd

Please sign in to comment.