Implemented open/close isar listener
ADD
packages/isar_test/test/open_close_isar_listener_test.dart
+import 'dart:async';
+
+import 'package:isar/isar.dart';
+import 'package:test/test.dart';
+
+import 'user_model.dart';
+import 'util/common.dart';
+import 'util/listener.dart';
+
+void main() {
+ group('open / close isar listener', () {
+ isarTest('Open listener', () async {
+ final streamController = StreamController<Isar>();
+ void openListener(Isar isar) => streamController.add(isar);
+ Isar.addOpenListener(openListener);
+ final listener = Listener(streamController.stream);
+
+ final isar1 = await openTempIsar([UserModelSchema]);
+ final listenedIsar1 = await listener.next;
+ expect(isar1, listenedIsar1);
+
+ final isar2 = await openTempIsar([UserModelSchema]);
+ final listenerIsar2 = await listener.next;
+ expect(isar2, listenerIsar2);
+
+ Isar.removeOpenListener(openListener);
+ await listener.done();
+ await streamController.close();
+ });
+
+ isarTest('Close listener', () async {
+ final streamController = StreamController<String>();
+ void closeListener(String name) => streamController.add(name);
+ Isar.addCloseListener(closeListener);
+ final listener = Listener(streamController.stream);
+
+ final isar1 = await openTempIsar([UserModelSchema]);
+ await isar1.close();
+ final listenedName1 = await listener.next;
+ expect(isar1.name, listenedName1);
+
+ final isar2 = await openTempIsar([UserModelSchema]);
+ await isar2.close();
+ final listenedName2 = await listener.next;
+ expect(isar2.name, listenedName2);
+
+ Isar.removeCloseListener(closeListener);
+ await listener.done();
+ await streamController.close();
+ });
+ });
+}

ADD
packages/isar_test/test/util/listener.dart
+import 'dart:async';
+
+import 'package:test/test.dart';
+
+class Listener<T> {
+ Listener(Stream<T> stream) {
+ subscription = stream.listen((event) {
+ if (_completer != null) {
+ _completer!.complete(event);
+ _completer = null;
+ } else {
+ _unprocessed.add(event);
+ }
+ });
+ }
+ late StreamSubscription<void> subscription;
+ final List<T> _unprocessed = <T>[];
+ Completer<T>? _completer;
+
+ Future<T> get next {
+ if (_unprocessed.isEmpty) {
+ expect(_completer, null);
+ _completer = Completer<T>();
+ return _completer!.future;
+ } else {
+ return Future.value(_unprocessed.removeAt(0));
+ }
+ }
+
+ Future<void> done() async {
+ await Future<void>.delayed(const Duration(milliseconds: 100));
+ await subscription.cancel();
+ expect(_completer, null);
+ expect(_unprocessed, <dynamic>[]);
+ }
+}

MODIFY
packages/isar_test/test/watcher_test.dart
packages/isar_test/test/watcher_test.dart
@@ -4,6 +4,7 @@ import 'package:isar/isar.dart';
import 'package:test/test.dart';
import 'util/common.dart';
+import 'util/listener.dart';
import 'util/sync_async_helper.dart';
part 'watcher_test.g.dart';
@@ -22,38 +23,7 @@ class Value {
other is Value && id == other.id && value == other.value;
}
-class Listener<T> {
- Listener(Stream<T> stream) {
- subscription = stream.listen((event) {
- if (_completer != null) {
- _completer!.complete(event);
- _completer = null;
- } else {
- _unprocessed.add(event);
- }
- });
- }
- late StreamSubscription<void> subscription;
- final List<T> _unprocessed = <T>[];
- Completer<T>? _completer;
-
- Future<T> get next {
- if (_unprocessed.isEmpty) {
- expect(_completer, null);
- _completer = Completer<T>();
- return _completer!.future;
- } else {
- return Future.value(_unprocessed.removeAt(0));
- }
- }
- Future<void> done() async {
- await Future<void>.delayed(const Duration(milliseconds: 100));
- await subscription.cancel();
- expect(_completer, null);
- expect(_unprocessed, <dynamic>[]);
- }
-}
void main() {
group('Watcher', () {

Implemented open/close isar listener