StrapiObjectListener constructor

StrapiObjectListener({
  1. required String id,
  2. required dynamic listener(
    1. Map<String, dynamic>,
    2. bool
    ),
  3. required Map<String, dynamic> initailData,
})

listen to a Strapi collection object, listening for now means any new instance of the collection object with id is received will be delivered to the listner, no server changes are are streamed as of now

Implementation

factory StrapiObjectListener({
  required String id,
  required Function(Map<String, dynamic>, bool) listener,
  required Map<String, dynamic> initailData,
}) {
  sPrint("Startingto listen for object $id");
  final existingListeners = _allListeners[id];
  var length = 0;
  if (existingListeners is List) {
    if (existingListeners?.length == Strapi.i.maxListenersForAnObject) {
      ///exception will be thrown if the max number of listners crosses [Strapi.i.maxListenersForAnObject], set
      ///it as per requirement
      throw Exception(
        "no more listeners for object id $id can be added this, set Strapi.maxListenersForAnObject to higher value",
      );
    }
    length = existingListeners?.length ?? 0;
  }

  final l = StrapiObjectListener._private(
    id + "-" + length.toString(),
    listener,
    initailData,
  );
  _allListeners.update(
    id,
    (value) => [...value, l],
    ifAbsent: () => [
      l,
    ],
  );
  return l;
}