Skip to content

Commit

Permalink
Refactor handling of state error in indexOf
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Sep 20, 2022
1 parent 8c56e3a commit e474dae
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
5 changes: 4 additions & 1 deletion lib/src/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ class ManagedRealmList<T extends Object?> with RealmEntity, ListMixin<T> 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
Expand Down
6 changes: 1 addition & 5 deletions lib/src/realm_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,7 @@ abstract class NotificationsController implements Finalizable {
}

void stop() {
if (handle == null) {
return;
}

handle!.release();
handle?.release();
handle = null;
}
}
Expand Down
13 changes: 8 additions & 5 deletions lib/src/realm_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 9 additions & 2 deletions test/list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -810,10 +810,17 @@ Future<void> main([List<String>? args]) async {
expect((dartList as List<Object>).indexOf("abc"), -1); // ignore: unnecessary_cast

// .. but realm list behaves differently in this regard.
expect(() => (players as List<Object>).indexOf(1), throwsA(isA<RealmException>())); // ignore: unnecessary_cast
expect(() => (players as List<Object>).indexOf(1), throwsA(isA<TypeError>())); // 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<RealmStateError>()));
expect(
() => players.indexOf(Person('10')),
throwsA(isA<RealmStateError>().having(
(e) => e.message,
'message',
'Cannot call indexOf on a managed list with an element that is an unmanaged object',
)),
);
});
}

0 comments on commit e474dae

Please sign in to comment.