retrieveAndRecursivelyFindReferences function

Future<EntityBase> retrieveAndRecursivelyFindReferences(
  1. String appId,
  2. ModelBase model,
  3. List<ModelReference> callerReferencedModels,
  4. List<ModelReference> referencedModels,
)

Implementation

Future<EntityBase> retrieveAndRecursivelyFindReferences(
    String appId,
    ModelBase model,
    List<ModelReference> callerReferencedModels,
    List<ModelReference> referencedModels) async {
  var entity = model.toEntity(
    appId: appId,
  );
  List<ModelReference> newReferences =
      await model.collectReferences(appId: appId);
  List<ModelReference> newReferences2 = [];

  List<ModelReference> newCallerReferencedModels = [];
  newCallerReferencedModels.addAll(callerReferencedModels);
  newCallerReferencedModels.addAll(newReferences);
  for (var newReferencedModel in newReferences) {
    // make sure we're not calling infinite recursively
    var found = false;
    for (var callerReferencedModel in callerReferencedModels) {
      if (callerReferencedModel.key() == newReferencedModel.key()) {
        found = true;
      }
    }
    if (!found) {
      await retrieveAndRecursivelyFindReferences(
          appId, newReferencedModel.referenced, newReferences, newReferences2);
    }
  }
  referencedModels.addAll(newReferences);
  referencedModels.addAll(newReferences2);
  return entity;
}