watchCollection static method

Stream watchCollection({
  1. required String collectionName,
  2. required String databaseName,
  3. List<String>? ids,
  4. String? filter,
  5. bool asObjectIds = true,
})

Implementation

static Stream watchCollection({
  required String collectionName,
  required String databaseName,
  List<String>? ids,
  String? filter,
  bool asObjectIds = true,
}) {
  Stream nativeStream;

  if (kIsWeb) {
//      Stream<Event> jsStream =
//          document.on["watchEvent.$databaseName.$collectionName"];

    var jsStream = StreamInterop.getNativeStream(
        "watchEvent.$databaseName.$collectionName");

    // ignore: close_sinks
    var controller = StreamController<String>.broadcast();

    // migrating events from the js-event to a dart event
    jsStream.listen((event) {
      Object? eventDetail = (event as CustomEvent).detail;

      var map = json.decode("${eventDetail ?? '{}'}");

      // if (map['_id'] is Map == true) {
      //   map['_id'] = ObjectId.parse(map['_id']);
      // }
      print(jsonEncode(map));
      controller.add(jsonEncode(map));
    });

    nativeStream = controller.stream;
  } else {
    nativeStream = StreamInterop.getNativeStream({
      "handler": "watchCollection",
      "db": databaseName,
      "collection": collectionName,
      "filter": filter,
      "ids": ids,
      "as_object_ids": asObjectIds,
    });
  }

  return nativeStream;

  // continuous stream of events from platform side
//    return _streamsChannel.receiveBroadcastStream({
//      "handler": "watchCollection",
//      "db": databaseName,
//      "collection": collectionName,
//      "filter": filter,
//      "ids": ids,
//      "as_object_ids": asObjectIds,
//    });
}