From e474daebf79979cd1f9b30656fdc1f63fc1bf078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Tue, 20 Sep 2022 11:30:59 +0200 Subject: [PATCH] Refactor handling of state error in indexOf --- lib/src/list.dart | 5 ++++- lib/src/realm_class.dart | 6 +----- lib/src/realm_object.dart | 13 ++++++++----- test/list_test.dart | 11 +++++++++-- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/src/list.dart b/lib/src/list.dart index e7ce5bda5..c7cb4980e 100644 --- a/lib/src/list.dart +++ b/lib/src/list.dart @@ -130,7 +130,10 @@ class ManagedRealmList with RealmEntity, ListMixin impleme void clear() => realmCore.listClear(handle); @override - int indexOf(Object? element, [int start = 0]) { + int indexOf(covariant T element, [int start = 0]) { + if (element is RealmObject && !element.isManaged) { + throw RealmStateError('Cannot call indexOf on a managed list with an element that is an unmanaged object'); + } if (start < 0) start = 0; final index = realmCore.listFind(this, element); return index < start ? -1 : index; // to align with dart list semantics diff --git a/lib/src/realm_class.dart b/lib/src/realm_class.dart index cc023a6bc..51e555783 100644 --- a/lib/src/realm_class.dart +++ b/lib/src/realm_class.dart @@ -482,11 +482,7 @@ abstract class NotificationsController implements Finalizable { } void stop() { - if (handle == null) { - return; - } - - handle!.release(); + handle?.release(); handle = null; } } diff --git a/lib/src/realm_object.dart b/lib/src/realm_object.dart index 65da24e3a..834615a50 100644 --- a/lib/src/realm_object.dart +++ b/lib/src/realm_object.dart @@ -391,15 +391,18 @@ extension RealmObjectInternal on RealmObject { return object; } - RealmObjectHandle get handle { - final h = _handle; - if (h != null) return h; - throw RealmStateError('$this is not managed'); - } + RealmObjectHandle get handle => _handle ?? failUnmanaged(this); RealmAccessor get accessor => _accessor; } +Never failUnmanaged(RealmEntity entity) { + final message = '$entity is not managed'; + assert(false, message); + // in production we fall back to throwing state error as a precaution + throw RealmStateError(message); +} + /// An exception being thrown when a `Realm` operation or [RealmObject] access fails. /// {@category Realm} class RealmException implements Exception { diff --git a/test/list_test.dart b/test/list_test.dart index 8be843e78..99b9409f6 100644 --- a/test/list_test.dart +++ b/test/list_test.dart @@ -810,10 +810,17 @@ Future main([List? args]) async { expect((dartList as List).indexOf("abc"), -1); // ignore: unnecessary_cast // .. but realm list behaves differently in this regard. - expect(() => (players as List).indexOf(1), throwsA(isA())); // ignore: unnecessary_cast + expect(() => (players as List).indexOf(1), throwsA(isA())); // ignore: unnecessary_cast // .. Also it is a state error to lookup an unmanaged object in a managed list, // even if the static type is right. - expect(() => players.indexOf(Person('10')), throwsA(isA())); + expect( + () => players.indexOf(Person('10')), + throwsA(isA().having( + (e) => e.message, + 'message', + 'Cannot call indexOf on a managed list with an element that is an unmanaged object', + )), + ); }); }